Compiler lab manual PDF

Title Compiler lab manual
Author Anonymous User
Course Product design
Institution Anna University
Pages 21
File Size 325.8 KB
File Type PDF
Total Downloads 373
Total Views 549

Summary

CS8602 COMPILER DESIGN LABLIST OF EXPERIMENTS:1. Develop a lexical analyzer to recognize a few patterns in C. (Ex.identifiers, constants, comments, operators etc.). Create a symbol table,while recognizing identifiers.2. Implement a Lexical Analyzer using Lex Tool3. Implement an Arithmetic Calculator...


Description

CS8602 COMPILER DESIGN LAB

LIST OF EXPERIMENTS: 1. Develop a lexical analyzer to recognize a few patterns in C. (Ex.

identifiers, constants, comments, operators etc.). Create a symbol table, while recognizing identifiers. 2. Implement a Lexical Analyzer using Lex Tool 3. Implement an Arithmetic Calculator using LEX and YACC 4. Implement simple code optimization techniques (Constant folding, Strength

reduction andAlgebraic transformation) 5. Implement back-end of the compiler for which the three address code is given as

input andthe 8086 assembly language code is produced as output 6. Generate three address code for a simple program using LEX and YACC.

PRACTICALS 30 PERIODS

1

Develop a lexical analyzer to recognize a few patterns in C. (Ex. identifiers, constants, comments, operators etc.) Ex. No: 1 a Date : AIM: To Create a Lexical Analyzer using C programming and to create lexemes that is grouped into various categories. ALGORITHM: Step 1: Start the program. Step 2: Create some character arrays to store and manipulate the characters. Step 3: Create a file pointer and get the name of the input file to read the input from. Step 4: Open the input file using the read category. Step 5: Copy the content of the file into a string and then copy it to a character array for processing. Step 6: Use a while loop and browse the content of the file till the end. Step 7: Using a if condition, Separate some symbols like ;{}()#,& and print them as Special Characters. Step 8: Using a if condition, print the letters/words following the int, char or float as the Variables. Step 9: Using a if condition, print the words printf, scanf, main, void etc as the keywords. Step 10: Terminate the program. PROGRAM: #include #include int main() { char tot[100][20]; char a[100],temp[100]; int i=0,j; FILE *p; p=fopen("input1.txt","r"); fscanf(p,"%s",a); strcpy(tot[i],a); strcpy(temp,"Null"); printf("\nLexeme\tToken\n\n"); i=1; while(strcmp(a,"END")!=0) { if((strcmp(a,";")==0)||(strcmp(a,"")==0)||(strcmp(a,",")==0)||(strcmp(a,"&")= =0)) { printf ("\n%s\t", a); printf("Special Character\t"); } else if((strcmp(temp,"int")==0)||(strcmp(temp,"float")==0)||(strcmp(temp,"char")==0)) { printf("\n%s\t",a); printf("variable\t"); }

2

else if((strcmp(a,"scanf")==0)||(strcmp(a,"printf")==0)||(strcmp(a,"main")==0)||(strcmp(a,"void")==0)) { printf("\n%s\t",a); printf("keywords\t"); } strcpy(temp,a); strcpy(tot[i],a); i++; fscanf(p,"%s",a); } printf("\t%d",i); printf("\n%s",tot[i-1]); } input1.txt # include < stdio.h > void main ( ) { int a = 100 ; } END OUTPUT: [root@sys70 2]# cc ex2.c [root@sys70 2]# ./a.out LexemeToken

# < > void main ( ) { a ; }

Special Character Special Character Special Character keywords keywords Special Character Special Character Special Character variable Special Character Special Character

RESULT: Thus the C program to implement the Lexical Analyzer is done and the required Lexemes are obtained and the output is verified.

3

IMPLEMENTATION OF SYMBOL TABLE Ex.No: 1 b Date:

AIM: To write a C program to implement a symbol table.

1. 2. 3. 4. 5. 6. 7.

8.

ALGORITHM Start the Program. Get the input from the user with the terminating symbol ‘$’. Allocate memory for the variable by dynamic memory allocation function. If the next character of the symbol is an operator then only the memory is allocated. While reading , the input symbol is inserted into symbol table along with its memoryaddress. The steps are repeated till”$”is reached. To reach a variable, enter the variable to the searched and symbol table has been checkedfor corresponding variable, the variable along its address is displayed as result. Stop the program.

PROGRAM:( IMPLEMENTATION OF SYMBOL TABLE) #include #include #include #include #include #includeSpecial Characters",yytext);} %% int main() { FILE *fp; fp=fopen("input.txt","r"); yyin=fp; yylex(); return 0; } int yywrap() { return 1; 7

}

input.txt: int main() { int a,b; printf("hello"); float c; char d; return 0; } OUTPUT: [root@sys70 3]# lex ex3.l [root@sys70 3]# cc lex.yy.c [root@sys70 3]# ./a.out int=> Keywords main=>functions (=>Special Characters )=>Special Characters {=>Special Characters int=> Keywords a=>Identifiers ,=>Special Characters b=>Identifiers ;=>Special Characters printf=>functions (=>Special Characters "=>Special Characters hello =>Identifiers "=>Special Characters )=>Special Characters ;=>Special Characters float=> Keywords c=>Identifiers ;=>Special Characters char=> Keywords d=>Identifiers ;=>Special Characters return=> Keywords 0=>Keywords ;=>Special Characters }=>Special Characters

RESULT: Thus the required Lexical Analyzer is designed and the required output is obtained and verified. 8

IMPLEMENT AN ARITHMETIC CALCULATOR USING LEX AND YACC Ex.No: 3 Date : AIM: To create a calculator using Lex and Yacc programs. ALGORITHM: Step 1: Start the program. Step 2: Include the necessary header files. Step 3: Use a function for printing the error message. Step 4: Get the input from the user and parse it. Step 5: Check the input is a valid expression or not. Step 6: Write separate operations for addition, subtraction, multiplication and division using the expr and matching it with the operators in the in the input. Step 7: Print the error messages for the invalid operators. Step 8: Print the output of the expression. Step 9: Terminate the program. PROGRAM: 4d.l %{ #include #include #include "y.tab.h" void yyerror(char*); extern int yylval; %} %% [ \t]+ ; [0-9]+ {yylval = atoi(yytext); return INTEGER;} [-+*/] {return *yytext;} "(" {return *yytext;} ")" {return *yytext;} \n {return *yytext;} . {char msg[25]; sprintf(msg,"%s ","invalid character",yytext); yyerror(msg); } 4d.y %{ #include #include int yylex(void); #include "y.tab.h" %} %token INTEGER %% expr: expr '\n' { printf("%d\n",$1); } 9

| 'n' expr: expr '+' mulex { $$ = $1 + $3; } | expr '-' mulex { $$ = $1 - $3; } | mulex { $$ = $1; } mulex: mulex '*' term { $$ = $1 * $3; } | mulex '/' term { $$ = $1 / $3; } | term { $$ = $1; } term: '(' expr ')' { $$ = $2; } | INTEGER { $$ = $1; } %% void yyerror(char *s) { fprintf(stderr,"%s\n",s); return; } yywrap() { return(1); } int main(void) { /*yydebug=1;*/ yyparse(); return 0; } OUTPUT: [root@sys70 4d]# lex 4d.l [root@sys70 4d]# yacc -d 4d.y [root@sys70 4d]# cc y.tab.c lex.yy.c -ll [root@sys70 4d]# ./a.out 10+20-50 -20 12*10 120 1/1 1 100+100 200 10-10 0

RESULT: Thus a calculator is formed using Yacc and the expressions are checked and the output of the valid expressions are obtained and verified successfully.

10

Ex.No: 4

IMPLEMENT SIMPLE CODE OPTIMIZATION TECHNIQUES

Date : AIM: To write a C program to implement Code Optimization Techniques. ALGORITHM: Step 1: Start the program. Step 2: Include the necessary header files. Step 3: Declare necessary character arrays for input and output and also a structure to include it. Step 4: Get the Input: Set of ‘L’ values with corresponding ‘R’ values and Step 4: Implement the principle source of optimization techniques. Step 5: The Output should be of Intermediate code and Optimized code after eliminating common expressions. Step 6: Terminate the program. PROGRAM: #include #include #include struct op { char l; char r[20]; } op[10],pr[10]; void main() { int a,i,k,j,n,z=0,m,q; char *p,*l; char temp,t; char *tem; clrscr(); printf("Enter the Number of Values:"); scanf("%d",&n); for(i=0;i...


Similar Free PDFs