Pcd Lab Record

Embed Size (px)

Citation preview

1

EX. NO: 1 DATE:

CONSTRUCTION OF NFA FROM A GIVEN REGULAR EXPRESSIONAIM:To write a C program to convert the regular expression to NFA.

ALGORITHM:1. Start the program 2. Get the regular expression from the user 3. Build a finite automaton with one final state (and one start state) 4. Use the recursive definition of regular expressions to build a finite automaton for a compound expression from the finite automata for the base expressions 5. Repeat the procedure to construct the NFA for the given regular expression 6. Display the NFA structure. 7. Stop the program

PROGRAM:#include #include #include #include #include struct s1 { int from,to; }; typedef struct s1 s2; struct ed { int from[30],to[30],n;

2

char on[30]; }; typedef struct ed edg; int top=0,start,end; s2 s3[100]; edg edg1,edg2; s2 pop() { top--; return s3[top]; } void push(s2 a) { s3[top]=a; top++; } int f(char a) { if(a=='/') return 1; if(a=='.') return 3; if(isalnum(a)) return 5; if(a=='(') return 7; if(a==')') return 0; return -1; } int g(char a) { if(a=='/') return 2; if(a=='.') return 4; if(isalnum(a)) return 6; if(a=='(') return 0;

3

if(a==')') return 8; return -1; } char *postfix(char in1[]) { int i=0,j=0,top=0,a,l,t=0; char s[100],*out,*in; l=strlen(in1); in=malloc(100); for(;tnext; } return flag; } void del() { int a; char l[10]; struct symtab *p,*q; p=first;

30

printf("\n Enter the label to be deleted: \n"); scanf("%s",l); a=search(l); if(a==0) printf("\n The label is not found"); else { if(strcmp(first->label,l)==0) { first=first->next; } else if(strcmp(last->label,l)==0) { q=p->next; while(strcmp(q->label,l)!=0) { p=p->next; q=q->next; } p->next=NULL; last=p; } else { q=p->next; while(strcmp(q->label,l)!=0) { p=p->next; q=q->next; }

31

p->next=q->next; } size--; } } void dis(char le[]) { int i; struct symtab *p; p=first; printf("\n Index \t Next \t Token \t Attribute \t Position \n"); for(i=0;ilabel,le)==0) { printf("\n %d \t %d \t %s \t %s \t %d \n",i,i+1,p->label,p->att,p->addr); break; } else p=p->next; } }

OUTPUT:SYMBOL TABLE IMPLEMENTATION

32

1. Insert 2. Delete 3. Lookup 4. Exit Enter your choice: 1 Enter the token: c Enter the position: 4 Enter the attribute: identifier Index Next Token Attribute 0 1 2 3 4 1 2 3 4 5 a = b + c identifier operator identifier operator identifier Position 1 2 0 3 0

SYMBOL TABLE IMPLEMENTATION 1. Insert 2. Delete 3. Lookup 4. Exit Enter your choice: 2 Enter the label to be deleted: c

Index Next Token Attribute 0 1 2 1 2 3 a = b Identifier operator identifier

Position 1 2 0

33

3

4

+

operator

3

SYMBOL TABLE IMPLEMENTATION 1. Insert 2. Delete 3. Lookup 4. Exit Enter your choice: 3 Enter the label to be searched: a The label is present in the symbol table Index Next Token Attribute 0 1 a identifier Position 1

SYMBOL TABLE IMPLEMENTATION 1. Insert 2. Delete 3. Lookup 4. Exit Enter your choice: 4

RESULT:Thus the program for implementation of symbol table is executed and verified.

EX. NO: 5 DATE:

IMPLEMENTATION OF OPERATOR PRECENDENCE PARSING

34

AIM:To write a C program to implement operator precedence parsing algorithm.

ALGORITHM:

Input: An input string w and a table of precedence relations. Output: If w is well formed, a skeletal parse tree, with a placeholder nonterminal E labeling all interior nodes; otherwise, an error indication. Method: Initially, the stack contains $ and the input buffer the string w$. set ip to point to the first symbol of w$; repeat forever if $ is on top of the stack and ip points to $ then return else begin let a be the topmost terminal symbol on the stack and let b be the symbol pointed to by ip; if a b then repeat pop the stack until the top stack terminal is related by item[++(ps->top)]=x; return; } pop(ps) struct stack *ps; { ps->item[--(ps->top)]; return; }

OUTPUT:Enter the input language i*i+(i/i) Stack $ $i $ $* $*i $* $ Input i*i+(i/i)$ *i+(i/i)$ *i+(i/i)$ i+(i/i)$ +(i/i)$ +(i/i)$ +(i/i)$ Actions Initial push($*) push($+)

40

$+ $+( $+(i $+( $+(/ $+(/i $+(/ $+( $+ $ $) $ ACCEPTED

(i/i)$ i/i)$ /i)$ /i)$ i)$ )$ )$ )$ )$ )$ $ $

push($)) push($$)

RESULT:Thus the C program for implementation of operator precedence parsing is executed and verified.

EX. NO: 6 DATE:

IMPLEMENTATION OF SYNTAX ANALYSIS USING YACC TOOLAIM:Write a program to implement a syntax analysis using Yacc tool.

ALGORITHM:1. Start the program. 2. Yacc program consists of three parts namely Declarations %%

41

Transition Rule %% Supporting C routines. 3. Declaration part consists of two sections, first section contains only include statements and the second statements contains declaration of the grammar tokens. 4. Each rule in set of transition rules consists of grammar production and semantic action. The set of productions are of the form : {semantic action 1} | {semantic action 2} .. | {semantic action n} ; 5. In the third part, error recovery routines are added. 6. The program is typed using vi editor, and saved with .y extension. 7. It is first compiled with the yacc compiler to produce the C code for C compiler (yacc filename.y).8. After that compile that program with C compiler (cc y.tab.c ly ).

9. See the output using ./a.out. 10. Stop the program.

PROGRAM:%{ #include #include #define YYSTYPE double %} %token number %left '+''-' %left '*''/' %right uminus %%

42

lines: expr'\n'{printf("The Result is : \t %g\n",$$);} expr:expr'+'expr{$$=$1+$3;} |expr'-'expr{$$=$1-$3;} |expr'*'expr{$$=$1*$3;} |expr'/'expr{$$=$1/$3;} |'('expr')'{$$=$2;} |'-'expr%prec uminus{$$=-$2;} |number ; %% int yylex() { char c; while((c=getchar())=='\0'); if((c=='.')||(isdigit(c))) { ungetc(c,stdin); scanf("%lf",&yylval); return number; } return c; } main() { printf("Enter The Expression : \t"); yyparse(); } void yyerror() { printf("\n Error....Invalid expression"); return;

43

}

OUTPUT:[cse4@localhost ~]$ yacc cse1.y [cse4@localhost ~]$ cc y.tab.c [cse4@localhost ~]$ ./a.out Enter The Expression : 2+4*7-4/2 The Result is : 28

RESULT:Thus the program for syntax analysis using YACC tool is executed and verified.

EX. NO: 7 DATE:

IMPLEMENTATION OF SHIFT REDUCE PARSINGAIM:To write a C program to implement the shift reduce parsing.

ALGORITHM:1. Start the program. 2. Get the input string from the user. 3. Push $ onto top of the stack. 4. Set ip to point to the first input symbol. 5. If there is any production which can be used to reduce the input symbol reduce the string otherwise push it to the top of the stack. 6. Set ip to point to next input symbol. 7. Repeat the above steps until the top of the stack contains the $ and the starting symbol. If so, then the string is valid, otherwise the string is invalid, return an error message.

44

8. Stop the program.

PROGRAM:#include #include void pus(char); void reduce(); char inp1[5][10]={"E+E","E*E","(E)","a"},stk[50],inpt1[10],stk1[50]; int ct=0; void main() { int k,len; char inpt[10]; clrscr(); printf("Enter the input string\n\n"); gets(inpt); printf("\n\n"); printf("STACK\tINPUT \tACTION"); printf("\n\n"); len=strlen(inpt); inpt[len++]='$'; inpt[len]='\0'; strcpy(inpt1,inpt); len=strlen(inpt); inpt[len++]='$'; inpt[len++]='\0'; pus('$'); pus(inpt[0]); for(k=1;k0) { for(i=0;ii Reduced by T->T*F Reduced by E->T Shift Reduced by F->(E) Reduced by T->F Reduced by E->T Shift Shift Reduced by F->i Reduced by T->F Reduced by E->E+T

RESULT:Thus the program for construction of LR parsing is executed and verified.

EX. NO: 9

55

DATE:

IMPLEMENTATION OF CODE GENERATION AIM:To write a C program to implement the code generation algorithm.

ALGORITHM:1. The code generation algorithm takes as input a sequence of three address statements constituting a basic block. 2. For each three address statement of the form x := y op z we perform the following actions: Invoke a function getreg to determine the location L where the result of the computation y op z should be stored. L will usually be a register, but it could also be a memory location. We shall describe getreg shortly.

Consult the address descriptor for y to determine y , (one of) the current location(s) of y. prefer the register for y if the value of y is currently both in memory and a register. If the value of y is not already in L, generate the instruction MOV y , L to place a copy of y in L.

Generate the instruction OP z , L where z is a current location of z. Again, prefer a register to a memory location if z is in both. Update the address descriptor of x to indicate that x is in location L. If L is a register, update its descriptor to indicate that it contains the value of x, and remove x from all other register descriptors.

If the current values of y and/or z have no next users, are not live on exit from the block, and are in register descriptor to indicate that, after execution of x := y op z, those registers no longer will contain y and/or z, respectively.

4. Stop the program.

PROGRAM:#include #include #include

56

char exp[10][10],*ope; int i,j,n,s[10],flag2,flag3,r1,r2; void check(); void oper(); void main() { clrscr(); printf("\nEnter no of Expression:\n"); scanf("%d",&n); printf("\nEnter the expression(In Three Address code):\n"); for(i=0;i