7/24/2019 CD Lab Manual-1
1/49
Department of Computer Science and Engineering
Department
Of
Computer Science & Engineering
Compiler Design Lab Manual
CHIRALA ENGINEERING
COLLEGEApproved by AICTE :: Afliated to JNTU, Kakinada,(Sponsored by GS ! TS
Ed"#ational society),
Ramapuram Beach Road, Vetapalem Mandal, CHIRALA 52 !5"# $ra%asam&istrict, A#$# India
Department of computer science & engineering
In-charge HOD Principal
Prepared b! "ppro#ed & $e#ie%ed
b!
Issued b! %ef Date!
1
7/24/2019 CD Lab Manual-1
2/49
Department of Computer Science and Engineering
CHIRALA ENGINEERING
COLLEGEApproved by AICTE :: Afliated to JNTU, Kakinada,(Sponsored by GS ! TS
Ed"#ational society),
Ramapuram Beach Road, Vetapalem Mandal, CHIRALA 52 !5"# $ra%asam&istrict, A#$# India
DEP"$'ME(' O) COMP*'E$ SCIE(CE & E(+I(EE$I(+
Lab Manual for the Academic Year 2014-15
(In accordance with JN!" #$llabu#%
&!'J) * )om+iler ,e#in LA'
&.AM * )&
/,
2
7/24/2019 CD Lab Manual-1
3/49
Department of Computer Science and Engineering
S$No
T%&IC&a'eNo$
1 System Reuirements
2 La! O!"ecti#es
$Design a %e&ica% ana%y'er for gi#en %anguage andt(e %e&ica% ana%y'er s(ou%d ignore redundantspaces) ta!s and ne* %ines
+Imp%ement t(e %e&ica% ana%y'er using ,Le&) -e& orot(er %e&ica% ana%y'er generating too%s.
/ Design predicti#e parser for t(e gi#en %anguage
0Design a LALR !ottom up parser for t(e gi#en%anguage
Con#ert t(e N3 ru%es into 4ACC form and *ritecode to generate a!stract synta& tree.
5
$
7/24/2019 CD Lab Manual-1
4/49
Department of Computer Science and Engineering
Syste e)"ireents
1. Inte% !ased dest6top 7C of 1008H' or faster processor *it( at %east
0+ 8 RA8 and 199 8 free dis6 space.
2. C:: compi%er and ,D; 6it.
+
7/24/2019 CD Lab Manual-1
5/49
Department of Computer Science and Engineering
*ab %b+e#tives
1.
7/24/2019 CD Lab Manual-1
6/49
Department of Computer Science and Engineering
Program No.1:
Write a Program to Design Lexical Analyzer.
#include
#include
#include
void keyord!char str"$%&
'
i(!strcmp!)(or)*str&++$,,strcmp!)hile)*str&++$,,strcmp!)do)*str&++$,,strcmp!)int)*str &++$,,strcmp!)(loat)*str&++$,,strcmp!)char)*str&++$,,strcmp!)dou-le)*str&++$,,
strcmp!)static)*str&++$,,strcmp!)sitch)*str&++$,,strcmp!)case)*str&++$&
print(!)n/s is a keyord)*str&0
else
print(!)n/s is an identi(ier)*str&0
1
main!&
'
23L4 5(*5(6*5(70
char c*str"$%*st"$%0
int num"$$%*lineno+$*tokenvalue+$*i+$*8+$*k+$0
print(!)n4nter the c program)&095gets!st&059
(+(open!)input)*))&0
hile!!c+getchar!&&:+4;2&
putc!c*(&0
(close!(&0
(+(open!)input)*)r)&0
(6+(open!)identi(ier)*))&0
(7+(open!)specialchar)*))&0
hile!!c+getc!(&&:+4;2&
'
i(!isdigit!c&&
'
0
7/24/2019 CD Lab Manual-1
7/49
Department of Computer Science and Engineering
tokenvalue+c=$=0
c+getc!(&0
hile!isdigit!c&&
'
tokenvalue5+$c=$=0
c+getc!(&0
1
num"i%+tokenvalue0
ungetc!c*(&0
1
else
i(!isalpha!c&& '
putc!c*(6&0
c+getc!(&0
hile!isdigit!c&,,isalpha!c&,,c++=?=,,c++=@=&
'
putc!c*(6&0
c+getc!(&0
1
putc!= =*(6&0
ungetc!c*(&0
1
else
i(!c++= =,,c++=t=&
print(!) )&0
else
i(!c++=n=&
lineno0
else
putc!c*(7&0
1
(close!(6&0
(close!(7&0
(close!(&0
7/24/2019 CD Lab Manual-1
8/49
Department of Computer Science and Engineering
print(!)nhe no=s in the program are)&0
(or!8+$08
7/24/2019 CD Lab Manual-1
9/49
Department of Computer Science and Engineering
Output:
4nter the program
a-5c
trlD
he noEs in the program areB
he keyords and identi(iers areB
a is an identi(ier and terminal
- is an identi(ier and terminal
c is an identi(ier and terminal
Cpecial characters areB
5
otal no. o( lines areB
>
7/24/2019 CD Lab Manual-1
10/49
Department of Computer Science and Engineering
Program No. 2:
Write a program 3mplement the Lexical Analyzer Fsing L4G ool.
95 program name is lexp.l 59
/'
95 program to recognize a c program 59
int ;HH4I+$0
/1
identi(ier "azAJ%"azAJ$K%5
//
#.5 ' print(!)n/s is a P4P;4CC; D343M4)*yytext&01
int ,(loat ,
char ,
dou-le ,
hile ,
(or ,
do ,
i( ,
-reak ,
continue ,
void ,
sitch ,
case ,
long ,
struct ,
const ,
typede( ,
return ,
else ,
goto 'print(!)nt/s is a N4OW;D)*yytext&01
)95) ';HH4I + 01
95'print(!)nnt/s is a ;HH4In)*yytext&0159
)59) ';HH4I + $01
95 print(!)nnt/s is a ;HH4In)*yytext&0159
19
7/24/2019 CD Lab Manual-1
11/49
Department of Computer Science and Engineering
'identi(ier1! 'i(!:;HH4I&print(!)nn2FI3;Int/s)*yytext&01
' 'i(!:;HH4I& print(!)n L;N 4Q3IC)&01
1 'i(!:;HH4I& print(!)n L;N 4IDC)&01
'identi(ier1!""$K%5%&R 'i(!:;HH4I& print(!)n /s 3D4I3234)*yytext&01
).5) 'i(!:;HH4I& print(!)nt/s is a C3IQ)*yytext&01
"$K% 'i(!:;HH4I& print(!)nt/s is a IFH4)*yytext&01
&!0&R 'i(!:;HH4I& print(!)nt)&04S;0print(!)n)&01
! 4S;0
+ 'i(!:;HH4I&print(!)nt/s is an ACC3QIH4I ;P4A;)*yytext&01
+ ,
< ,++ ,
> 'i(!:;HH4I& print(!)nt/s is a 4LA3;IAL ;P4A;)*yytext&01
//
int main!int argc*char 55argv&
'
i( !argc > &
'
23L4 5(ile0
(ile + (open!argv"%*)r)&0
i(!:(ile&
'
print(!)could not open /s n)*argv"%&0
exit!$&0
1
yyin + (ile0
1
yylex!&0
print(!)nn)&0
return $0
1 int yyrap!&
'
return $0
1
11
7/24/2019 CD Lab Manual-1
12/49
Department of Computer Science and Engineering
Input:
@vi var.c
#include
main!&
'
int a*-0
1
Output:
@lex lex.l
@cc lex.yy.c
@.9a.out var.c
#include is a P4P;4CC; D343M42FI3;I
main !
&
L;N 4Q3IC
int is a N4OW;D
a 3D4I3234
- 3D4I3234
L;N 4IDC
12
7/24/2019 CD Lab Manual-1
13/49
Department of Computer Science and Engineering
Program No. 3:
Write a Program for Implementation of Predictive Parser.
#include
#include
#include
#include
#de(ine C3J4 6T
#de(ine I;I4
#de(ine 4;C =$=
#de(ine IFH 6UV
#de(ine N4OW;D 6UT#de(ine 3D 6UK
#de(ine D;I4 6$
#de(ine HAG KKK
char lexemes"HAG%0
char -u((er"C3J4%0
int lastchar+0
int lastentry+$0
int tokenval+D;I40
int lineno+0
int lookahead0
struct entry
'
char 5lexptr0
int token0
1
symta-le"$$%0
struct entry
keyords"%+')i()*N4OW;D*)else)*N4OW;D*)(or)*N4OW;D*)int)*N4OW;D*
)(loat)*N4OW;D*)dou-le)*N4OW;D*)char)*N4OW;D*)struct)*N4OW;D*)ret
urn)*N4OW;D*$*$10
void 4rror?Hessage!char 5m&
'
(print(!stderr*)line /d* /s n)*lineno*m&0
1$
7/24/2019 CD Lab Manual-1
14/49
Department of Computer Science and Engineering
exit!&0
1
int look?up!char s" %&
'
int k0
(or!k+lastentry0k>$0k&
i(!strcmp!symta-le"k%.lexptr*s&++$&
return k0
return $0
1
int insert!char s" %*int tok&
' int len0
len+strlen!s&0
i(!lastentry>+HAG&
4rror?Hessage!)Cym-pl ta-le is (ull)&0
i(!lastcharlen>+HAG&
4rror?Hessage!)Lexemes array is (ull)&0
lastentry+lastentry0
symta-le"lastentry%.token+tok0
symta-le"lastentry%.lexptr+Xlexemes"lastchar%0
lastchar+lastcharlen0
strcpy!symta-le"lastentry%.lexptr*s&0
return lastentry0
1
95void 3nitialize!&
'
struct entry 5ptr0
(or!ptr+keyords0ptr>token0ptr&
insert!ptr>lexptr*ptr>token&0
159
int lexer!&
'
int t0
int val*i+$0
1+
7/24/2019 CD Lab Manual-1
15/49
Department of Computer Science and Engineering
hile!&
'
t+getchar!&0
i(!t++= =,,t++=t=&0
else
i(!t++=n=&
lineno+lineno0
else
i(!isdigit!t&&
'
ungetc!t*stdin&0
scan(!)/d)*Xtokenval&0 return IFH0
1
else
i(!isalpha!t&&
'
hile!isalnum!t&&
'
-u((er"i%+t0
t+getchar!&0
i+i0
i(!i>+C3J4&
4rror?Hessage!)ompiler error)&0
1
-u((er"i%+4;C0
i(!t:+4;2&
ungetc!t*stdin&0
val+look?up!-u((er&0
i(!val++$&
val+insert!-u((er*3D&0
tokenval+val0
return symta-le"val%.token0
1
else
1/
7/24/2019 CD Lab Manual-1
16/49
Department of Computer Science and Engineering
i(!t++4;2&
return D;I40
else
'
tokenval+I;I40
return t0
1
1
1
void Hatch!int t&
'
i(!lookahead++t& lookahead+lexer!&0
else
4rror?Hessage!)Cyntax error)&0
1
void display!int t*int tval&
'
i(!t++==,,t++==,,t++=5=,,t++=9=&
print(!)nArithmetic ;peratorB /c)*t&0
else i(!t++IFH&
print(!)n Ium-erB /d)*tval&0
else i(!t++3D&
print(!)n 3denti(ierB /s)*symta-le"tval%.lexptr&0
else
print(!)n oken /d tokenval /d)*t*tokenval&0
1
void 2!&
'
99void 4!&0
sitch!lookahead&
'
case =!= B Hatch!=!=&0
4!&0
Hatch!=&=&0
10
7/24/2019 CD Lab Manual-1
17/49
Department of Computer Science and Engineering
-reak0
case IFH B display!IFH*tokenval&0
Hatch!IFH&0
-reak0
case 3D B display!3D*tokenval&0
Hatch!3D&0
-reak0
de(ault B 4rror?Hessage!)Cyntax error)&0
1
1
void !&
' int t0
2!&0
hile!&
'
sitch!lookahead&
'
case =5= B t+lookahead0
Hatch!lookahead&0
2!&0
display!t*I;I4&0
continue0
case =9= B t+lookahead0
Hatch!lookahead&0
display!t*I;I4&0
continue0
de(ault B return0
1
1
1
void 4!&
'
int t0
!&0
1
7/24/2019 CD Lab Manual-1
18/49
Department of Computer Science and Engineering
hile!&
'
sitch!lookahead&
'
case == B t+lookahead0
Hatch!lookahead&0
!&0
display!t*I;I4&0
continue0
case == B t+lookahead0
Hatch!lookahead&0
!&0 display!t*I;I4&0
continue0
de(ault B return0
1
1
1
void parser!&
'
lookahead+lexer!&0
hile!lookahead:+D;I4&
'
4!&0
Hatch!=0=&0
1
1
main!&
'
char ans"$%0
print(!)n Program (or recursive decent parsing )&0
print(!)n 4nter the expression )&0
print(!)And place 0 at the endn)&0
print(!)Press trlJ to terminaten)&0
parser!&0
15
7/24/2019 CD Lab Manual-1
19/49
Department of Computer Science and Engineering
1
Output:
Program (or recursive decent parsing
4nter the expression And place 0 at the end
Press trlJ to terminate
a-5c0
3denti(ierB a
3denti(ierB -
3denti(ierB c
Arithmetic ;peratorB 5
Arithmetic ;peratorB
6570Ium-erB 6
Ium-erB 7
Arithmetic ;peratorB 5
70
line U*Cyntax error
trlJ
Program No: 4
1>
7/24/2019 CD Lab Manual-1
20/49
Department of Computer Science and Engineering
Write a program to Design LAL ottom up Parser.
/'
#include
#include )y.ta-.h)
/1
//
"$K% 'yylval.dval+ato(!yytext&0
return D3Q30
1
n,. return yytext"$%0
//
/'
95his OA speci(ication (ile generates the LAL parser (or the program
considered in experiment Y.59
#include
/1
/union
'
dou-le dval0
1
/token D3Q3
/type expr
/type term
/type (actor
//
lineB expr =n= '
print(!)/gn)*@&0
1
0
exprB expr == term '@@+@ @7 01
, term
0
termB term =5= (actor '@@+@ 5 @7 01
29
7/24/2019 CD Lab Manual-1
21/49
Department of Computer Science and Engineering
, (actor
0
(actorB =!= expr =&= '@@+@6 01
, D3Q3
0
//
int main!&
'
yyparse!&0
1
yyerror!char 5s&
'print(!)/s)*s&0
1
Output:
@lex parser.l
@yacc Zd parser.y
@cc lex.yy.c y.ta-.c Zll Zlm
@.9a.out
67
U.$$$$
Program No: 5
onvert he I2 rules into Oacc (orm and rite code to generate a-stract syntax tree.
7/24/2019 CD Lab Manual-1
22/49
Department of Computer Science and Engineering
/'
#include)y.ta-.h)
#include
#include
int LineIo+0
/1
identi(ier "azAJ%"?azAJ$K%5
num-er "$K%,!"$K%5."$K%&
//
main!& return HA3I0
i( return 320
else return 4LC40hile return WS3L40
int ,
char ,
(loat return OP40
'identi(ier1 'strcpy!yylval.var*yytext&0
return MA01
'num-er1 'strcpy!yylval.var*yytext&0
return IFH01
< ,
> ,
>+ ,
7/24/2019 CD Lab Manual-1
23/49
Department of Computer Science and Engineering
'
char op"U%0
char arg"$%0
char arg6"$%0
char result"$%0
1\FAD"7$%0
struct stack
'
int items"$$%0
int top0
1stk0
int 3ndex+$*t3ndex+$*CtIo*3nd*t3nd0extern int LineIo0
/1
/union
'
char var"$%0
1
/token IFH MA 4L;P
/token HA3I 32 4LC4 WS3L4 OP4
/type 4GP ACC3QIH4I ;ID33;I 32C 4LC4C WS3L4L;;P
/le(t == ==
/le(t =5= =9=
//
P;QAH B HA3I L;N
0
L;NB ='= ;D4 =1=
0
;D4B L;N
, CA4H4I ;D4
, CA4H4I
0
CA4H4IB D4C =0=
, ACC3QIH4I =0=
, ;IDC
2$
7/24/2019 CD Lab Manual-1
24/49
Department of Computer Science and Engineering
, WS3L4C
0
D4CB OP4 MAL3C
0
MAL3CB MA =*= MAL3C
, MA
0
ACC3QIH4IB MA =+= 4GP'
strcpy!\FAD"3ndex%.op*)+)&0
strcpy!\FAD"3ndex%.arg*@7&0
strcpy!\FAD"3ndex%.arg6*))&0
strcpy!\FAD"3ndex%.result*@&0strcpy!@@*\FAD"3ndex%.result&0
1
0
4GPB 4GP == 4GP 'Add\uadruple!))*@*@7*@@&01
, 4GP == 4GP 'Add\uadruple!))*@*@7*@@&01
, 4GP =5= 4GP 'Add\uadruple!)5)*@*@7*@@&01
, 4GP =9= 4GP 'Add\uadruple!)9)*@*@7*@@&01
, == 4GP 'Add\uadruple!)FH3I)*@6*))*@@&01
, =!= 4GP =&= 'strcpy!@@*@6&01
, MA
, IFH
0
;IDCB 32C'
3nd+pop!&0
sprint(!\FAD"3nd%.result*)/d)*3ndex&0
3nd+pop!&0
sprint(!\FAD"3nd%.result*)/d)*3ndex&0
1
, 32C 4LC4C
0
32CB 32 =!= ;ID33;I =&= '
strcpy!\FAD"3ndex%.op*)++)&0
strcpy!\FAD"3ndex%.arg*@7&0
2+
7/24/2019 CD Lab Manual-1
25/49
Department of Computer Science and Engineering
strcpy!\FAD"3ndex%.arg6*)2ALC4)&0
strcpy!\FAD"3ndex%.result*))&0
push!3ndex&0
3ndex0
1
L;N '
strcpy!\FAD"3ndex%.op*)Q;;)&0
strcpy!\FAD"3ndex%.arg*))&0
strcpy!\FAD"3ndex%.arg6*))&0
strcpy!\FAD"3ndex%.result*))&0
push!3ndex&0
3ndex010
4LC4CB 4LC4'
t3nd+pop!&0
3nd+pop!&0
push!t3nd&0
sprint(!\FAD"3nd%.result*)/d)*3ndex&0
1
L;N'
3nd+pop!&0
sprint(!\FAD"3nd%.result*)/d)*3ndex&0
10
;ID33;IB MA 4L;P MA 'Add\uadruple!@6*@*@7*@@&0
CtIo+3ndex0
1
, MA
, IFH
0
WS3L4CB WS3L4L;;P'
3nd+pop!&0
sprint(!\FAD"3nd%.result*)/d)*CtIo&0
3nd+pop!&0
sprint(!\FAD"3nd%.result*)/d)*3ndex&0
1
2/
7/24/2019 CD Lab Manual-1
26/49
Department of Computer Science and Engineering
0
WS3L4L;;PB WS3L4 =!= ;ID33;I =&= '
strcpy!\FAD"3ndex%.op*)++)&0
strcpy!\FAD"3ndex%.arg*@7&0
strcpy!\FAD"3ndex%.arg6*)2ALC4)&0
strcpy!\FAD"3ndex%.result*))&0
push!3ndex&0
3ndex0
1
L;N '
strcpy!\FAD"3ndex%.op*)Q;;)&0
strcpy!\FAD"3ndex%.arg*))&0strcpy!\FAD"3ndex%.arg6*))&0
strcpy!\FAD"3ndex%.result*))&0
push!3ndex&0
3ndex0
1
0
//
extern 23L4 5yyin0
int main!int argc*char 5argv"%&
'
23L4 5(p0
int i0
i(!argc>&
'
(p+(open!argv"%*)r)&0
i(!:(p&
'
print(!)n 2ile not (ound)&0
exit!$&0
1
yyin+(p0
1
yyparse!&0
20
7/24/2019 CD Lab Manual-1
27/49
Department of Computer Science and Engineering
print(!)nntt ))ntt Pos ;perator Arg Arg6 esult) )ntt
)&0
(or!i+$0i
7/24/2019 CD Lab Manual-1
28/49
Department of Computer Science and Engineering
strcpy!\FAD"3ndex%.arg*arg&0
strcpy!\FAD"3ndex%.arg6*arg6&0
sprint(!\FAD"3ndex%.result*)t/d)*t3ndex&0
strcpy!result*\FAD"3ndex%.result&0
1
yyerror!&
'
print(!)n 4rror on line noB/d)*LineIo&0
1
3nputB
@vi test.c
main!&'
int a*-*c0
i(!a
7/24/2019 CD Lab Manual-1
29/49
Department of Computer Science and Engineering
@.9a.out test.c
O"#P"#:
2>
7/24/2019 CD Lab Manual-1
30/49
Department of Computer Science and Engineering
Intermediate code generation
#include
#include
#include
int i+*8+$*no+$*tmpch+K$0
char str"$$%*le(t"U%*right"U%0
void (indopr!&0
void explore!&0
void (le(t!int&0
void (right!int&0
struct exp
'
int pos0
char op0
1k"U%0
void main!&
'
clrscr!&0
print(!)tt3I4H4D3A4 ;D4 Q4I4A3;Inn)&0
print(!)4nter the 4xpression B)&0
scan(!)/s)*str&0
print(!)he intermediate codeBtt4xpressionn)&0
(indopr!&0
explore!&0
getch!&0
1
void (indopr!&
'
$9
7/24/2019 CD Lab Manual-1
31/49
Department of Computer Science and Engineering
(or!i+$0str"i%:+=$=0i&
i(!str"i%++=B=&
'
k"8%.pos+i0
k"8%.op+=B=0
1
(or!i+$0str"i%:+=$=0i&
i(!str"i%++=9=&
'
k"8%.pos+i0
k"8%.op+=9=0
1
(or!i+$0str"i%:+=$=0i&
i(!str"i%++=5=&
'
k"8%.pos+i0
k"8%.op+=5=0
1
(or!i+$0str"i%:+=$=0i&
i(!str"i%++==&
'
k"8%.pos+i0
k"8%.op+==0
1
(or!i+$0str"i%:+=$=0i&
i(!str"i%++==&
'
k"8%.pos+i0
$1
7/24/2019 CD Lab Manual-1
32/49
Department of Computer Science and Engineering
k"8%.op+==0
1
1
void explore!&
'
i+0
hile!k"i%.op:+=$=&
'
(le(t!k"i%.pos&0
(right!k"i%.pos&0
str"k"i%.pos%+tmpch0
print(!)t/c B+ /s/c/stt)*str"k"i%.pos%*le(t*k"i%.op*right&0
(or!8+$08
7/24/2019 CD Lab Manual-1
33/49
Department of Computer Science and Engineering
1
void (le(t!int x&
'
int +$*(lag+$0
x0
hile!x:+ XXstr"x%:+ == XXstr"x%:+=5=XXstr"x%:+=+=XXstr"x%:+=$=XXstr"x%:
+==XXstr"x%:+=9=XXstr"x%:+=B=&
'
i(!str"x%:+=@=XX (lag++$&
'
le(t"%+str"x%0
le(t"%+=$=0
str"x%+=@=0
(lag+0
1
x0
1
1
void (right!int x&
'
int +$*(lag+$0
x0
hile!x:+ XX str"x%:+ ==XXstr"x%:+=5=XXstr"x%:+=$=XXstr"x%:+=+=XXstr"x%:
+=B=XXstr"x%:+==XXstr"x%:+=9=&
'
i(!str"x%:+=@=XX (lag++$&
'
right"%+str"x%0
right"%+=$=0
$$
7/24/2019 CD Lab Manual-1
34/49
Department of Computer Science and Engineering
str"x%+=@=0
(lag+0
1
x0
1
1
;utputB
$+
7/24/2019 CD Lab Manual-1
35/49
Department of Computer Science and Engineering
$ode generation:
#include)stdio.h)#include)conio.h)#include)string.h)#include)stdli-.h)struct [uadraple' int pos0 char op0 char arg"U%0 char arg6"U%0 char result"U%0
1[uad"U%0int n+$0void assignment!int&0void uminus!int &0void explore!&0void codegen!char op"U%*int&0char tuple"U%"U%0int main!void&'23L4 5src0int net3nd*i0
char str"U%0clrscr!&0src+(open!)int.txt)*)r)&0(scan(!src*)/s)*str&0hile!:(eo(!src&&' strcpy!tuple"n%*str&0 (scan(!src*)/s)*str&01print(!)3IPFBn3ntermiate codesBn)&0(or!i+$0i< n0i&
print(!)/sn)*tuple"i%&0explore!&0getch!&0clrscr!&0print(!);FPFBn)&0print(!)\uadrupleB n)&0print(!)postoprtargtarg6tresultn)&0(or!i+$0i< n0i& print(!)n/dt/ct/st/st/s)*[uad"i%.pos*[uad"i%.op*[uad"i%.arg*[uad"i%.arg6*[uad"i%.result&0i+$0print(!)nncode generated Bn)&0hile!i< n&'
$/
7/24/2019 CD Lab Manual-1
36/49
Department of Computer Science and Engineering
i(![uad"i%.op++==& codegen!)ADD)*i&0 i(![uad"i%.op++=+=& assignment!i&0 i(![uad"i%.op++==& i(!:strcmp![uad"i%.arg6*)$)&&
uminus!i&0 else codegen!)CF)*i&0 i(![uad"i%.op++=5=& codegen!)HFL)*i&0 i(![uad"i%.op++=9=& codegen!)D3M)*i&0i01getch!&0(closeall!&0
return $01void codegen!char op"U%*int t&'char str"6U%0print(!)H;M /s*$n)*[uad"t%.arg&0print(!)/s /s*$n)*op*[uad"t%.arg6&0print(!)H;M $*/sn)*[uad"t%.result&01void assignment!int t&'char str"6U%0print(!)H;M /s*/sn)*[uad"t%.result*[uad"t%.arg&01void uminus!int t&'char str"6U%0print(!)H;M $*$n)&0print(!)CF /s*$n)*[uad"t%.arg&0print(!)H;M $*/sn)*[uad"t%.result&01
void explore!&'int i*8*t*t*t60(or!i+$0i < n0i&' [uad"i%.pos+i0 (or!8+$*t+$08 < strlen!tuple"i%&XXtuple"i%"8%:+=+=08& ' [uad"i%.result"t%+tuple"i%"8%0
1 t+80 [uad"i%.result"t%+=$=0
$0
7/24/2019 CD Lab Manual-1
37/49
Department of Computer Science and Engineering
i(!tuple"i%"8%++=+=& ' [uad"i%.op+=+=0 1 i(!tuple"i%"8%++==,,tuple"i%"8%++==,,tuple"i%"8%++=5=,,tuple"i%"8%++=9=& '
[uad"i%.op+tuple"i%"8%0 t+80 1 (or!8+t*t+$08< strlen!tuple"i%&XXtuple"i%"8%:+==XXtuple"i%"8%:+==XXtuple"i%"8%:+=5=XXtuple"i%"8%:+=9=08& ' [uad"i%.arg"t%+tuple"i%"8%0 1 t6+80 [uad"i%.arg"t%+=$=0 i(!tuple"i%"8%++==,,tuple"i%"8%++==,,tuple"i%"8%++=5=,,tuple"i%"8%++=9=&
' [uad"i%.op+tuple"i%"8%0 1 (or!8+t6*t+$08< strlen!tuple"i%&08& ' [uad"i%.arg6"t%+tuple"i%"8%0 1 [uad"i%.arg6"t%+=$=011
3IPFB
3I.G
t$+cdt+t$5c
-+t$9cc+tt6+t7
;FPFB
$
7/24/2019 CD Lab Manual-1
38/49
Department of Computer Science and Engineering
$5
http://1.bp.blogspot.com/_j82bvWjy4n4/S2GGx62OFSI/AAAAAAAAADA/BGHxMhHo218/s1600-h/CODE2.JPGhttp://3.bp.blogspot.com/_j82bvWjy4n4/S2GGqh_V1KI/AAAAAAAAAC4/9DheDZBKA2s/s1600-h/CODE1.JPG7/24/2019 CD Lab Manual-1
39/49
Department of Computer Science and Engineering
To parse a string using Operator Precedence parsing in C++ Programming
#include#include#include
int get;peratorPosition!char &0
#de(ine node struct tree
int matrix"U%"U%+' '*$*$**1* '**$**1* '$*$*$*6*71* '**7**1* '$*$*$*7*6110int tos+0void matrix?value!void&099node create?node!char*5node&0void sho?tree! node 5&0int is;perator!char &0
struct tree' char data0 node 5lptr0 node 5rptr015(irst0
struct opr' char op?name0 node 5t01oprate"U$%0
char cur?op"U%+'==*=5=*=!=*=&=*="=10char stack?op"U%+'==*=5=*=!=*=&=*=%=10
void main!&' char exp"$%0
int ssm+$*ro+$*col+$0 node 5temp099 clrscr!&0
print(!)4nter 4xp B )&0 scan(!)/s)*exp&0
matrix?value!&0 hile!exp"ssm% :+ =$=&
$>
7/24/2019 CD Lab Manual-1
40/49
Department of Computer Science and Engineering
' i(!ssm++$& ' tos0 oprate"tos%.op?name + exp"tos%0 1
else ' i(!is;perator!exp"ssm%& ++ & ' oprate"tos%.t + !node5& malloc !sizeo(!node&&0 oprate"tos%.t>data + exp"ssm%0 oprate"tos%.t>lptr + =$=0 oprate"tos%.t>rptr + =$=0 1 else '
ro + get;peratorPosition!oprate"tos%.op?name&0 col + get;peratorPosition!exp"ssm%&0 i(!matrix"ro%"col% ++ $& ' tos0 oprate"tos%.op?name + exp"ssm%0 1 elsei(!matrix"ro%"col% ++ & ' temp + !node5& malloc !sizeo(!node&&0 temp>data + oprate"tos%.op?name0
temp>lptr + !oprate"tos%.t&0 temp>rptr + !oprate"tos%.t&0 tos0
oprate"tos%.t + temp0
ssm0 1 elsei(!matrix"ro%"col% ++ 6&
' 99temp + !node5& malloc !sizeo(!node&&0 temp + oprate"tos%.t0 tos0 oprate"tos%.t + temp0 1 elsei(!matrix"ro%"col% ++ 7& ' print(!)n4xpression is 3nvalid...n)&0 print(!)/c /c can not occur simultaneouslyn)*oprate"tos%.op?name*exp"ssm%&0 -reak0
1 1
+9
7/24/2019 CD Lab Manual-1
41/49
Department of Computer Science and Engineering
1
ssm0 1 print(!)sho tree nnn)&0 sho?tree!oprate"tos%.t&0
print(!);ver)&0 getch!&0 getch!&01
int is;perator!char c&' int i+$0 (or!i+$0ilptr :+ IFLL& sho?tree!start>lptr&0
i(!start>rptr :+ IFLL& sho?tree!start>rptr&0
print(!)/c n)*start>data&01
void matrix?value!void&' int i*80
+1
7/24/2019 CD Lab Manual-1
42/49
Department of Computer Science and Engineering
print(!);P4A; P44D4I4 HA3Gn)&0 print(!)+++++++++++++++++++++++++++n )&0
(or!i+$0 i < < > >5 > > < > >! < < < +& > > > >" < < < +sho tree
a
-c5
;ver4nter 4xp B "a!-5c&d%;P4A; P44D4I4 HA3G
+2
7/24/2019 CD Lab Manual-1
43/49
Department of Computer Science and Engineering
+++++++++++++++++++++++++++ 5 ! & %
> < < > >5 > > < > >! < < < +& > > > >
" < < < +sho tree
a-c5d
;ver4nter 4xp B "&%;P4A; P44D4I4 HA3G+++++++++++++++++++++++++++
5 ! & % > < < > >5 > > < > >! < < < +& > > > >" < < < +
4xpression is 3nvalid..." & can not occur simultaneouslysho tree.;ver
+$
7/24/2019 CD Lab Manual-1
44/49
Department of Computer Science and Engineering
P%O&%'( )O% $O(P"#'#ION O) )I%*#
#include#include#include
void main!&'char t"U%*nt"$%*p"U%"U%*(irst"U%"U%*temp0int i*8*not*nont*k+$*(+$0clrscr!&0
print(!)n4nter theno. o( Ionterminals in the grammerB)&0scan(!)/d)*Xnont&0
print(!)n4nter the Ionterminals in the grammerBn)&0(or!i+$0i
7/24/2019 CD Lab Manual-1
45/49
Department of Computer Science and Engineering
print(!)/c)*p"i%"8%&011(or!i+$0i )*(irst"i%"$%&0(or!8+0(irst"i%"8%:+=@=08&'
print(!)/ct)*(irst"i%"8%&01
1getch!&01;FPF4nter the no. o( Ionterminals in thegrammerB74nter the Ionterminals in the grammerB44nter the no. o( erminals inthe grammerB ! 4nter e (or a-siline & U4nter the erminals in the grammerB
ase54nter the productions B4nter the production (or 4 ! 4nd the production
+/
7/24/2019 CD Lab Manual-1
46/49
Department of Computer Science and Engineering
ith =@= sign & Bas@4nter the production (or ! 4nd the productionith =@= sign & Be@4nter the production (or ! 4nd the productionith =@= sign & Bs@he production (or 4 > as
he production (or > ehe production (or > she (irst o( 4 > ahe (irst o( > ehe (irst o( > e s
P%O&%'( )O% $O(P"#'#ION O) )O++OW
#include#includeint n*m+$*p*i+$*8+$0char a"$%"$%*("$%0
void (ollo!char c&0void (irst!char c&0int main!&'int i*z0char c*ch0print(!)4nter the no.o( productionsB)&0scan(!)/d)*Xn&0print(!)4nter the productions!epsilon+@&Bn)&0(or!i+$0i
7/24/2019 CD Lab Manual-1
47/49
Department of Computer Science and Engineering
' i(!a"i%"8%++c& ' i(!a"i%"8%:+=$=&(irst!a"i%"8%&0
i(!a"i%"8%++=$=XXc:+a"i%"$%&
(ollo!a"i%"$%&0
1 111void (irst!char c&' int k0 i(!:!isupper!c&&&("m%+c0 (or!k+$0k
7/24/2019 CD Lab Manual-1
48/49
Department of Computer Science and Engineering
/ Pro0lem *tatement :
'ssignment #o implement a simple $alculator using ,acc
////////////////////// calci.l /////////////////////////
include
7/24/2019 CD Lab Manual-1
49/49
Department of Computer Science and Engineering
////////////////////// Output ///////////////////////
8a49Jlocalost K;H le? calci.l
8a49Jlocalost K;H !acc d calci.!
8a49Jlocalost K;H cc le?.!!.c !.ta0.c ll
8a49Jlocalost K;H .a.out
Cnter C?pression = 19L
'nsFer = 1L