CD Lab2
CD Lab2
OBJECTIVE:
Wite a C program for constructing of LL (1) parsing
RESOURCE:
Turbo C++
PROGRAM LOGIC:
Read the input string
Using predictive parsing table parse the given input using stack.
If stack [] matches With token nput string pop the token else shift it repeat the process until it reaches to S.
PROCEDURE:
Go to debug-> run or press CTRL + F9 to run the program.
PROGRAM
#include<stdio h>
#includecconio h>
#include<string h>
char s[20],stack[20]
void mainO
"
char m[5][613]={"tb"," "," ","tb"," "," "," ","+tb"," "," ","n", "n", "fc"," "," ","fc","
""n,"n,"1," "." "."(e)",""." "}
int size[5]|[6={2,0,0,2,0,0,0,3,0,0,1,1,2,0,0,2,0,0,0,1,3,0,1,1,1,0,0,3,0,0}
int ij.kn,strl,str2,
clrscrO;
printf("in Enter the input string: ")
scanf("%s",s);
streat(s,"S")
n=strlen(s)
stack[0]="S';
stack[1J='e,
F,
JFO,
printf("'nStack Inputin");
printf(" \n");
while(stack[J-s)&&<0]=s)
ifstack[i)=sUD
17
switch(stack[1])
break;
case 'b': str1=1;
break;
case t: strl=2;,
break,
case e. strl=s,
break,
case 't: strl=4;
break
Switch(sG1)
break,
case+str2=1
break
case * str2=2,
break,
case'(: str2=3;
break,
case ): str2=4;
break;,
case'S' str2=5;
break,
ifm[str1][str2][0]-=10')
printf"nERROR");
exit(0);
else if(m[strl][str2][0]==i)
18
stack[=1;
else
for(k=size[str1][str2]-1;k>=0;k-)
stack[i)=m[str1 ][str2][k];
for(k-0,k<=ikt+)
printf(" 6c",stack[k]);
printf(" ");
for(k=j.k<=n;k++)
printf(%c"s[k]);
printf(" n ");
printf("n SUCCESS");:
getch0:
INPUT&OUTPUT:
Enter the input string i*j+i
Stack INPUT
$bt ii+is
Sbef iiis
Sbei ii+is
Sbe i+is
Sbcf* i+iS
Sbct
Sbei itis
Sbe 1S
$b 15
Sbt+ 15
sbt 1S
Sbef S
bei
Sbe
Sb
Success
19
EXPERIMENT-7
OBJECTIVE:
Write a C program for implementing the fünctionalities of predictive parser for the mini language specified in
Note 1
RESOURCE:
Turbo C++
PROGRAM LOGIC:
Read the input string
By using the FIRST AND FOLLOW values.
Verify the FIRST of non terminal and insert the production in the FIRST value
f we have any @ termms in FIRST then insert the productions in FOLLOW values
Constructing the predictive parser table
PROCEDURE:
Go to debug -> run or press CTRL + F9 to run the program.
PROGRAM:
#include<stdio.h>
include<conio.h>
#inchude<string h>
har prol[7][10]={"S","A","A","B","B","C","C"}
char pror[7][10]={"A","Bb","Cd","aB","@""Ce","@");
har prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@""C->Ce","C>@")
char
first[7][10]=("abed", "ab","cd","'a@""@""c@""@"}
char follow[7][10]={"S","S", "S","aS","bs","eS","ds"};
char table[516][10];
umr(char c)
Switch(c)
13
return(2);
vOid maino
int 1j.
clrserO:
for(i-0,i5;i++)
for(j-0j<6j+)
strepy(table[G1." ");
print("nThe following is the predictive parsing table for the following grammar: \n");
for(i-0,i<7;i++)
printf"%sin" prodli|)D
printf("nPredictive parsing table isin")
ffush(stdin)
for(i-0,i<7,it+)
k=strlen(first[i])
for(j=0.j<10j++)
iffirst[0=@)
strepy(table(nunr(prol[i|[0])+1][num(first[JG)+1]prod[i)
for(i=0,i<7,i++)
ifstrlen(pror[il)=1)
ifprorfi[0]-@
=strlen(follow)
for(j-0j<kj++
strepy(table[aumr(prol[g][O)+1][mumr(follow[bD+1]prodi),
strepy(table[0O][O],"*")
strepy(tableo])"a):
strepy(table[0][2],"b");
strepy(table[0]I3]"e");
strepy(table[0]4],"d");
strepy(table| 0]I5],"5")
strepy(table[1[O],"s");
strepy(table[2]1O]."A"):
strepy(table[3][0],"B");
strepy(table[4][0],"C");
printt("r \n");
for(i=0;i<S;i++)
forý=0 j<6j++)
printf("6-10s", table[iJG):
ifj==5)
printf("
getch0
1. What is
top-down parsing?
2. What are the disadvantages of brute force method?
3. What is context free grammar?
4. What 1s parse tree?
5. Whatis ambiguous grammar?
6. What are the derivation methods to generate a string tor the given grammar?
7. What is the output of parse tree?
15
INPUT & OUTPUT:
The followWing is the predictive parsing table for the tollowing grammarr
S->A
>Bb
A->Cd
B->aB
B->@
C->Ce
C->
Predictive parsing table is
16
3.Write a C program to generate three address code.
#include<stdio.h>
#include<string.h>
void pm);
void plus(O:
void div);
int i,ch.j.l.addr-100;
clrscr);
while(1)
case 2
printf("nEnter the expression with arithmetic operator:");
scanf("6s",ex);
strcpy(exp,ex);
Fstrlen(exp);
expl[0]=\0';
for(i-0;i<l;i++)
if(explil+"||exp[il)
ifexpli+2]-="|exp[i+2]=*)
pm)
break;
else
plus)
break;
else ifexp[i--"|lexpli]-=*)
div);
break;
break;
case 3:
printf("Enter the expression with relational operator");
scanf("%sos%s",&idl,&op.&id2);
if(strcmp(op,"e")-0}|(stremp(op,">")0)|(stremp(op,"<=")=0J|(stremp(op,">=")=-0}|(st
remp(op,"-")=0)}|(strcmp(op,"!=")--0)-0)
printf("Expression is error");
else
break;
case 4:
exit(0);
void pm)
strrev(exp);
ji-l;
strncat(exp1,exp.j);
strrev(expl);
printf("Three address code:\ntemp-%s\ntempl-%c%octempn",expl,expi+1].exp[ilD:
void div)
strncat(exp1,exp.i+2);
printf("Three address code:Intemp-6s\ntempl-temp%c%c\n",expl,exp[i+2].exp[i+3]):
void plus)
strncat(expl,exp.i+2);
printf("Three address code:ntemp-6sIntempl-temp%cocln",expl,exp[i+2].expli+3]);
Output
Output
I. assignment
2. arithmetic
3. relational
41Exit
Enter the choice:1
Enter the expression with assignment operator:
a=b
Three address code:
temp=b
a-temp
1.assignment
2.arithmetic
.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a+b-c
Three address code:
temp-a+b
templ=temp-c
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator
a-b/c
Three address code:
temp=b/c
templ=a-temp
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a"b-c
Three address code:
temp-a"b
templ=temp-c
1.assignment
2.arithmetic
3 .relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:a/b*c
Three address code:
temp=a/b
templ=temp"c
I.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:3
Enter the expression with relational operator
EXPERIMENT-9
OBJECTIVE:
Write a program to Design LALR Bottom up Parser.
RESOURCE:
TURBO C++
PROGRAMLOGIC:
Push the input symbol with its state symbols in to the stack by referring lookaheads
PROCEDURE:
/LALR PARSER
E->E+1
E->1
T->T*F
T->F
F-(E)
F-N
#include<stdio h>
#include<conio.h>
include<stdlib.h>
#include<string.h>
void push(char *,int *,char)
char stacktop(char *);
void isproduct(char,char);
int ister(char);
int isnter(char);
int isstate(char);
oid error);
void isreduce(char, char)
char pop(char *int ):
void print(char i n t " char [.int);
struct action
char row[6]S];
24
const struct action A[12]={
("sf","emp","emp","se","emp","emp"),
("emp","sg","emp","emp","emp","ace"),
{"emp", "re","sh", "emp","re", "re"},
("emp""re", "re","emp", "re","re"},
{"sf","emp","emp","'se","emp","emp"
"emp","rg "Tg","emp","rg", "rg",
("sf","emp","emp","se""emp","emp",
{"sf","emp", "emp", "se","emp" "emp"}
("emp","sg","emp","emp","sl","emp").
("emp","rb","sh""emp","rb","rb"},
{"emp","rb","rd","emp","rd","rd"},
struct gotol
char r[3]14
"b""e","d"
"emp","emp","emp"),
("emp", "emp","emp"},
("emp", "emp", "emp"},
"""c","d"),
("emp","emp","emp"},
("emp""""d"),
f"emp","emp","k"},
"emp" "emp","emp"},
f"emp" "emp","emp"},
25
char left,
char right[5]
{'E',"e+T"}
(E,"T
(T"T*F"),
(T,"F"),
(F/"(E)"),
(F""),
void mainO
clrser0,
printf(" Enter the input ")
scanf("6s",inp);
len=strlen(iunp);
np[len]='$
inp[len+1]=F0;
push(stack,&top,bl);
printf("n stack ittit input")
printt(stack,& top.inp.i)
x=inp[il;
p=stacktop(stack),
1sproduct(x.P)
ifsircup(iemp,"eup"=0)
error),
ifstremp(temp, "acc"J==0)
break,
else
iftemp[0]==s)
26
else
f(temp[0]=)
j=isstate(temp[1]);
strepy(temp.lj-2]right);
dl[O]=rl[j-2] left,
dl[1]=\0';
n=strlen(temp);
for(k-0.k<2*nk++)
pop(stack, &top);
for(m-0;dl[m]=10"m++)
push(stack,&top, dl[m]);
top;
y=stack[1-1]
isreduce(y,dl[o);
for(m=0,temp[m]!=10';m+)
push(stack, &top,temp[m]
printt(stack,&topinp i);
}while(inpli]=0);
ifstremp(temp."ace")==0)
printf(" In accept the input ");
else
getchO
if("sp=100)
printf" stack is full ");
else
sp-sp+1;
27
s[sp]=item;
char 1,
1ss[top]
retum i,
inttk1;
k=ister(x)
=isstate(p),
strepy(tempA[L-1]row[k-1])
int ister(char x)
int i,
for(i=0,i<6i++)
if(x==ter[1])
return i+1
return 0,
int isnter(char x)
int i,
for(i-0i<3it++)
if(x=nter[i)
refurn i+l1
return 0,
int isstate(char p)
int 1,
for(i=0,i<12,i++)
if(p=states[i])
28
return 1+1,
returm o,
vOid error)
exit(0);
int k
k=isstate(x)
isnter(p):
strepy(temp,G[k-1]r[1-1]);
char item,
if( sp=-1)
printf(" stack 15 empty ");
lse
item=s["spPl
spsp-1
refun item,
int r,
printf(" n");
for(r=0rc=*p.r+*)
rep(t.r)
printf("ittr");
fortr=implr|l=0'r++)
29
printf(%c"inp[r]);
char C,
c=t[r]
switch(c)
break,
case b: printf("1");
break,
case 'c': printf("2");
break,
case 'd printt("3");
break
case 'e': printf("4");
break
case 'f pintt(">");
break,
case 'g printf("6");
break,
case h: printf("7");
break,
break;
case : printf("9");
break,
case k printf"10");
break,
break,
default printf("%c",t[r]);
break
30
INPUT & OUTPUT:
Output
Stack input
ii+1S
0is i+iS
OFS *i+is
OT2 j+1S
OT2*7 i+iS
T2*7i5 +iS
OT2*7iSF10 +15
OT2 +15
El +15
OE1+6
DE +6i5
OE1++6F3
OE1++6T9
OE1
accept the input/
31