Pcd Lab Manual

Embed Size (px)

DESCRIPTION

PCD LAB MANUAL ANNA UNIVERSITY

Citation preview

INDEX

EXP.NONAME OF THE EXPERIMENTPAGE NO

1SYMBOL TABLE IMPLEMENTATION3

2IMPLEMENTATION OF LEXICAL ANALYZER IN C8

3IMPLEMENTATION OF LEXICAL ANALYZER USING LEX TOOL14

4IMPLEMENTATION OF RECURSIVE DESCENT PARSER19

5IMPLEMENTATION OF PARSER USING YACC AND LEX TOOL27

6IMPLEMENTATION OF CALCULATOR USING YACC32

7IMPLEMENTATION OF FRONT END OF THE COMPILER36

8IMPLEMENTATION OF BACK END OF THE COMPILER42

9NON RECURSIVE PREDICTIVE PARSING46

10FINDING SLR CLOSURE51

EX.No:1SYMBOL TABLE IMPLEMENTATIONDate:

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

ALGORITHM:

1. Start the program.2. Get the input from the user with the terminating symbol $3. Allocate the memory for the variable by dynamic memory allocation function.4. If the next character of the symbol is an operator then only the memory is allocated.5. While reading , the input symbol are inserted into symbol table along with its memory address.6. The steps are repeated till $ is reached.7. To reach a variable, enter the variable to the searched and symbol table has been checked for corresponding variable, the variable along with its address is displayed as result.8. Stop the program.

/*SYMBOL TABLE IMPLEMENTATION*/

#include#include#include#include#include#includevoid main(){int i=0,j=0,x=0,n,flag=0;void *p,*add[5];char ch,srch,b[15],d[15],c;clrscr();printf("Expression terminated by $:");while((c=getchar())!='$'){b[i]=c;i++;}n=i-1;printf("Given expression:");i=0;while(ilist state | list error & action state->expr,& actionExpr->expr/exprExpr->expr+exprExpr->expr- exprExpr-> expr* exprExpr->LETTER|numberNumber->number DIGIT| DIGIT5. In the lex file, all the header files and global variable yyval are declared in the declaration part.6. The lex parse returns a symbol that is put on the top of the stack, so that YACC can access it.This symbol is returned in the variable yyval.7. Yywrap() returns 0, if it is EOF, it is useful during the inclusion, to return to the original file at the end of included file.

//IMPLEMENTATION OF PARSER USING YACC AND LEX TOOLPROGRAM:*********************PARSE.Y*****************************%{#include%}%start list%token DIGIT LETTER%left '|'%left '&'%left '+' '-'%left '*' '/' '%'%%list: | list stat '\n' | list error '\n' { yyerrok; } ;stat: expr { printf("given expression is correct\n"); } ;expr:expr '/' expr | expr '+' expr | expr '-' expr | expr '*' expr | LETTER | number ;number: DIGIT | number DIGIT ;%%main(){ printf("Enter the expression\n"); return(yyparse());}void yyerror(){ printf("Syntax error\n"); }

***********************PARSE.L***************************

%{#include#include"y.tab.h"int c;extern int yylval;%}%%" " ;[a-zA-Z][a-zA-Z0-9]* { c=yytext[0]; yylval=c-'a'; return LETTER; }[0-9]* { c=yytext[0]; yylval=c-'0'; return DIGIT; }[^a-z0-9\b] { c=yytext[0]; return c; }%%int yywrap(){ return(1);}

OUTPUT:

[student@localhost ~]$ yacc -d parse.y[student@localhost ~]$ lex parse.l[student@localhost ~]$ cc y.tab.c lex.yy.c[student@localhost ~]$ ./a.out

Enter the expressiona+b*cgiven expression is correcta--b*cSyntax error

RESULT:

Thus the program for implementing a parser using YACC and LEX has been written and executed successfully.

Ex.No:6IMPLEMENTATION OF CALCULATOR USING YACC Date:

AIM:To write a program to implement a calculator using YACC tool.

ALGORITHM:

1. Start the program.2. Declare all the operation of a calculator in the variable expression.3. Create an array for parsing the values and call yyparse() function.4. In the yylex function, get the character .5. If the character is q then quit from the execution phase else compute the calculation specified and return the result.6. In yyerror() function, return standard errors.7. Stop the program.

//IMPLEMENTATION OF CALCULATOR USING YACCPROGRAM:***********************calc.y***************************%{#define YYSTYPE double%}%token NUMBER%left '+''-'%left '*''/'%%list : |list '\n' |list expr '\n' { printf("\t%g\n",$2);}expr : NUMBER {$$=$1;} |expr '+' expr{$$=$1+$3;} |expr '-' expr{$$=$1-$3;} |expr '*' expr{$$=$1*$3;} |expr '/' expr{$$=$1/$3;} |'('expr ')' {$$=$2;}%%#include#includechar *progname;int lineno=1;int main(int argc,char *argv[]){progname=argv[0];yyparse();return 0;}int yylex(){int c;while((c=getchar()) == ' ' || c=='\t');if(c=='q')return 0;if(c=='.' || isdigit(c)){ungetc(c,stdin);scanf("%lf",&yylval);return NUMBER;}if(c=='\n')lineno++;return c;}int yyerror(char str[60]){printf("%s",str);exit(0); }

OUTPUT:

[student@localhost ~]$ yacc calc.y

[student@localhost ~]$ cc y.tab.c

[student@localhost ~]$ ./a.out

2+355*8405-324/22q

RESULT:

Thus the program for implementing a calculator using YACC tool has been written and executed successfully.

Ex.No:7IMPLEMENTATION OF FRONT END OF THE COMPILERDate:

AIM:To write a C program to implement the front end of the compiler.

ALGORITHM:

1. Start the program.2. Get the coding from the user.3. Find the expressions from the coding.4. Find operators, arguments and results from the expression.5. Display the values in the table.6. Stop the program.

//IMPLEMENTATION OF FRONT END OF THE COMPILER#include#include#includevoid main(){char pg[100][100],str1[24];int tem=-1,ct=0,i=-1,j=0,j1,pos=-1,t=-1,flag,flag1,tt=0,fg=0;clrscr();printf("Enter the coding:\n");while(i>-2){i++;lab1:t++;scanf("%s",&pg[i]);if((strcmp(pg[i],"getch();"))==0){i=-2;goto lab1;}}printf("\n pos \t oper \t arg1\t arg2\t result\n");while(j+TE'/e T->FT' T'->*FT'/e F->(E)/i Enter the input symbol: i*i

Sequence of production rules E'->TE' E'->FT' F->i T'->*FT' F->i T->e T'->e

RESULT:

Thus the C program for non recursive predictive parsing has been written and executed successfully.

Ex.No:10IMPLEMENTATION OF SLR CLOSURE Date:

AIM:To write a program to find the closure of the given productions.

ALGORITHM:

1. Start the program.2. Get the number of productions.3. Enter the left hand side productions4. Enter the right hand side productions.5. Find the augmented grammar for the given productions.6. Find the closures.7. Stop the program.

//FINDING SLR CLOSURE#include#include#include#includestruct prod{char left;char right[25];char closl;char closr;}prod[25];int q,n,j;char h[25];char d;int z=0;void closure(char k){int m;for(q=0;q