Sie sind auf Seite 1von 29

Department of Computer Science and Engineering

Department Of Computer Science & Engineering Compiler Design Lab Manual

BALAJI INSTITUTE OF TECHNOLOGY AND SCIENCE


Department of computer science & engineering

In-charge

HOD

Principal

Prepared b !

"ppro#ed & $e#ie%ed b !

Issued b !

%&e&f Date!

Balaji Institute of Technology and Science

Department of Computer Science and Engineering

BALAJI INSTITUTE OF TECHNOLOGY AND SCIENCE


Laknepally(V), Narsampet, Warangal DEP"$'ME(' O) COMP*'E$ SCIE(CE & E(+I(EE$I(+

Lab Manual for the Academic Year !""#" ($n accordance %ith &N'( syllabus)

)(*&+,' )'/+AM

,ompiler .esign LA* ,)+

0121.

S.

TOPIC

Page
2

Balaji Institute of Technology and Science

Department of Computer Science and Engineering

No
1 System Requirements

No.

a! "!jecti#es Design a le%ical analy&er for gi#en language and the le%ical analy&er should ignore redundant spaces' ta!s and ne( lines Implement the le%ical analy&er using * e%' fle% or other le%ical analy&er generating tools+ Design predicti#e parser for the gi#en language

Design a . R !ottom up parser for the gi#en language Con#ert the B01 rules into 2.CC form and (rite code to generate a!stract synta% tree+ . program to generate machine code

S stem $e,uirements
Balaji Institute of Technology and Science $

Department of Computer Science and Engineering

1+ Intel !ased dest4top 5C of 1--67& or faster processor (ith at least -) 6B R.6 and 188 6B free dis4 space+ 2+ C99 compiler and *D: 4it+

Lab Ob-ecti#es
Balaji Institute of Technology and Science )

Department of Computer Science and Engineering

1+ To pro#ide an ;nderstanding of the language translation peculiarities !y designing complete translator for mini language+ 2+ To pro#ide an understanding of the design aspect of operating system+

Program No.1:
Balaji Institute of Technology and Science ,

Department of Computer Science and Engineering

Write a Program to Design Lexical Analyzer.


#include<string.h> #include<ctype.h> #include<stdio.h> void key ord!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!)s itch)*str&++$,,strcmp!)case)*str&++$& print(!).n/s is a key ord)*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&& ' tokenvalue+c<=$=0 c+getc!(#&0 hile!isdigit!c&& ' tokenvalue5+#$>c<=$=0
Balaji Institute of Technology and Science -

Department of Computer Science and Engineering

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=& lineno>>0 else putc!c*(7&0 1 (close!(6&0 (close!(7&0 (close!(#&0 print(!).nAhe no=s in the program are)&0 (or!8+$08<i08>>& print(!)/d)*num"8%&0 print(!).n)&0 (6+(open!)identi(ier)*)r)&0
Balaji Institute of Technology and Science /

Department of Computer Science and Engineering

k+$0 print(!)Ahe key ords and identi(iersareB)&0 hile!!c+getc!(6&&:+4;2& ' i(!c:+= =& str"k>>%+c0 else ' str"k%+=.$=0 key ord!str&0 k+$0 1 1 (close!(6&0 (7+(open!)specialchar)*)r)&0 print(!).nCpecial characters are)&0 hile!!c+getc!(7&&:+4;2& print(!)/c)*c&0 print(!).n)&0 (close!(7&0 print(!)Aotal no. o( lines areB/d)*lineno&0 1

Output:
4nter the D program a>-5c
Balaji Institute of Technology and Science 3

Department of Computer Science and Engineering

Dtrl<D Ahe noEs in the program areB Ahe key ords 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 Aotal no. o( lines areB #

Program No. 2: Write a program 3mplement the Lexical Analyzer Fsing L4G Aool.
95 program name is lexp.l 59
Balaji Institute of Technology and Science <

Department of Computer Science and Engineering

/' 95 program to recognize a c program 59 int D;HH4IA+$0 /1 identi(ier "a<zA<J%"a<zA<J$<K%5 // #.5 ' print(!).n/s is a PL4PL;D4CC;L D3L4DA3M4)*yytext&01 int , (loat , char , dou-le , hile , (or , do , i( , -reak , continue , void , s itch , case , long , struct , const , typede( , return , else , goto 'print(!).n.t/s is a N4OW;LD)*yytext&01 )95) 'D;HH4IA + #01 95'print(!).n.n.t/s is a D;HH4IA.n)*yytext&0159 )59) 'D;HH4IA + $01 95 print(!).n.n.t/s is a D;HH4IA.n)*yytext&0159 'identi(ier1.! 'i(!:D;HH4IA&print(!).n.n2FIDA3;I.n.t/s)*yytext&01 .' 'i(!:D;HH4IA& print(!).n PL;DN P4Q3IC)&01 .1 'i(!:D;HH4IA& print(!).n PL;DN 4IDC)&01 'identi(ier1!.""$<K%5.%&R 'i(!:D;HH4IA& print(!).n /s 3D4IA3234L)*yytext&01
Balaji Institute of Technology and Science 18

Department of Computer Science and Engineering

.).5.) 'i(!:D;HH4IA& print(!).n.t/s is a CAL3IQ)*yytext&01 "$<K%> 'i(!:D;HH4IA& print(!).n.t/s is a IFHP4L)*yytext&01 .&!.0&R 'i(!:D;HH4IA& print(!).n.t)&04DS;0print(!).n)&01 .! 4DS;0 + 'i(!:D;HH4IA&print(!).n.t/s is an ACC3QIH4IA ;P4LAA;L)*yytext&01 .<+ , .>+ , .< , ++ , .> 'i(!:D;HH4IA& print(!).n.t/s is a L4LAA3;IAL ;P4LAA;L)*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(!).n.n)&0 return $0 1 int yy rap!& ' return $0 1

Input:
@vi var.c #include<stdio.h> main!&
Balaji Institute of Technology and Science 11

Department of Computer Science and Engineering

' int a*-0 1

Output:
@lex lex.l @cc lex.yy.c @.9a.out var.c #include<stdio.h> is a PL4PL;D4CC;L D3L4DA3M4 2FIDA3;I main ! & PL;DN P4Q3IC int is a N4OW;LD a 3D4IA3234L - 3D4IA3234L PL;DN 4IDC

Program No. 3: Write a Program for Implementation of Predictive Parser.


#include<stdio.h> #include<ctype.h> #include<string.h>
Balaji Institute of Technology and Science 12

Department of Computer Science and Engineering

#include<stdli-.h> #de(ine C3J4 #6T #de(ine I;I4 <# #de(ine 4;C =.$= #de(ine IFH 6UV #de(ine N4OW;LD 6UT #de(ine 3D 6UK #de(ine D;I4 6W$ #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 key ords"%+')i()*N4OW;LD*)else)*N4OW;LD*)(or)*N4OW;LD*)int)*N4OW;LD* )(loat)*N4OW;LD*)dou-le)*N4OW;LD*)char)*N4OW;LD*)struct)*N4OW;LD*)ret urn)*N4OW;LD*$*$10 void 4rror?Hessage!char 5m& ' (print(!stderr*)line /d* /s .n)*lineno*m&0 exit!#&0 1 int look?up!char s" %& ' int k0 (or!k+lastentry0k>$0k<<&
Balaji Institute of Technology and Science 1$

Department of Computer Science and Engineering

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(!lastchar>len>#>+HAG& 4rror?Hessage!)Lexemes array is (ull)&0 lastentry+lastentry>#0 symta-le"lastentry%.token+tok0 symta-le"lastentry%.lexptr+Xlexemes"lastchar>#%0 lastchar+lastchar>len>#0 strcpy!symta-le"lastentry%.lexptr*s&0 return lastentry0 1 95void 3nitialize!& ' struct entry 5ptr0 (or!ptr+key ords0ptr<>token0ptr>#& insert!ptr<>lexptr*ptr<>token&0 159 int lexer!& ' int t0 int val*i+$0 hile!#& ' t+getchar!&0 i(!t++= =,,t++=.t=&0 else i(!t++=.n=&
Balaji Institute of Technology and Science 1)

Department of Computer Science and Engineering

lineno+lineno>#0 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+i>#0 i(!i>+C3J4& 4rror?Hessage!)Dompiler 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 i(!t++4;2& return D;I40 else ' tokenval+I;I40 return t0
Balaji Institute of Technology and Science 1,

Department of Computer Science and Engineering

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 Aoken /d tokenval /d)*t*tokenval&0 1 void 2!& ' 99void 4!&0 s itch!lookahead& ' case =!= B Hatch!=!=&0 4!&0 Hatch!=&=&0 -reak0 case IFH B display!IFH*tokenval&0 Hatch!IFH&0 -reak0 case 3D B display!3D*tokenval&0 Hatch!3D&0
Balaji Institute of Technology and Science 1-

Department of Computer Science and Engineering

-reak0 de(ault B 1 1 void A!& ' int t0 2!&0 hile!#& ' s itch!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 A!&0 hile!#& ' s itch!lookahead& ' case =>= B t+lookahead0 Hatch!lookahead&0
Balaji Institute of Technology and Science 1/

4rror?Hessage!)Cyntax error)&0

Department of Computer Science and Engineering

A!&0 display!t*I;I4&0 continue0 case =<= B t+lookahead0 Hatch!lookahead&0 A!&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 end.n)&0 print(!)Press Dtrl<J to terminate.n)&0 parser!&0 1

Output:
Program (or recursive decent parsing 4nter the expression And place 0 at the end Press Dtrl<J to terminate a>-5c0
Balaji Institute of Technology and Science 13

Department of Computer Science and Engineering

3denti(ierB a 3denti(ierB 3denti(ierB c Arithmetic ;peratorB 5 Arithmetic ;peratorB > 6570 Ium-erB 6 Ium-erB 7 Arithmetic ;peratorB 5 >70 line U*Cyntax error Dtrl<J

Program No: 4 Write a program to Design LALL Pottom up Parser.


<parser.l> /' #include<stdio.h> #include )y.ta-.h)
Balaji Institute of Technology and Science 1<

Department of Computer Science and Engineering

/1 // "$<K%> 'yylval.dval+ato(!yytext&0 return D3Q3A0 1 .n,. return yytext"$%0 // <parser.y> /' 95Ahis OADD speci(ication (ile generates the LALL parser (or the program considered in experiment Y.59 #include<stdio.h> /1 /union ' dou-le dval0 1 /token <dval> D3Q3A /type <dval> expr /type <dval> term /type <dval> (actor // lineB expr =.n= ' print(!)/g.n)*@#&0 1 0 exprB expr =>= term '@@+@# > @7 01 , term 0 termB term =5= (actor '@@+@# 5 @7 01 , (actor 0 (actorB =!= expr =&= '@@+@6 01 , D3Q3A 0
Balaji Institute of Technology and Science 28

Department of Computer Science and Engineering

// 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 6>7 U.$$$$

Program No: 5 Donvert Ahe PI2 rules into Oacc (orm and rite code to generate a-stract syntax tree.
<int.l> /' #include)y.ta-.h)
Balaji Institute of Technology and Science 21

Department of Computer Science and Engineering

#include<stdio.h> #include<string.h> int LineIo+#0 /1 identi(ier "a<zA<J%"?a<zA<J$<K%5 num-er "$<K%>,!"$<K%5.."$<K%>& // main.!.& return HA3I0 i( return 320 else return 4LC40 hile return WS3L40 int , char , (loat return AOP40 'identi(ier1 'strcpy!yylval.var*yytext&0 return MAL01 'num-er1 'strcpy!yylval.var*yytext&0 return IFH01 .< , .> , .>+ , .<+ , ++ 'strcpy!yylval.var*yytext&0 return L4L;P01 " .t% 0 .n LineIo>>0 . return yytext"$%0 // <int.y> /' #include<string.h> #include<stdio.h> struct [uad ' char op"U%0
Balaji Institute of Technology and Science 22

Department of Computer Science and Engineering

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*t3nd0 extern int LineIo0 /1 /union ' char var"#$%0 1 /token <var> IFH MAL L4L;P /token HA3I 32 4LC4 WS3L4 AOP4 /type <var> 4GPL ACC3QIH4IA D;ID3A3;I 32CA 4LC4CA WS3L4L;;P /le(t =<= =>= /le(t =5= =9= // PL;QLAH B HA3I PL;DN 0 PL;DNB ='= D;D4 =1= 0 D;D4B PL;DN , CAAA4H4IA D;D4 , CAAA4H4IA 0 CAAA4H4IAB D4CDA =0= , ACC3QIH4IA =0= , D;IDCA , WS3L4CA 0
Balaji Institute of Technology and Science 2$

Department of Computer Science and Engineering

D4CDAB AOP4 MALL3CA 0 MALL3CAB MAL =*= MALL3CA , MAL 0 ACC3QIH4IAB MAL =+= 4GPL' strcpy!\FAD"3ndex%.op*)+)&0 strcpy!\FAD"3ndex%.arg#*@7&0 strcpy!\FAD"3ndex%.arg6*))&0 strcpy!\FAD"3ndex%.result*@#&0 strcpy!@@*\FAD"3ndex>>%.result&0 1 0 4GPLB 4GPL =>= 4GPL 'Add\uadruple!)>)*@#*@7*@@&01 , 4GPL =<= 4GPL 'Add\uadruple!)<)*@#*@7*@@&01 , 4GPL =5= 4GPL 'Add\uadruple!)5)*@#*@7*@@&01 , 4GPL =9= 4GPL 'Add\uadruple!)9)*@#*@7*@@&01 , =<= 4GPL 'Add\uadruple!)FH3I)*@6*))*@@&01 , =!= 4GPL =&= 'strcpy!@@*@6&01 , MAL , IFH 0 D;IDCAB 32CA' 3nd+pop!&0 sprint(!\FAD"3nd%.result*)/d)*3ndex&0 3nd+pop!&0 sprint(!\FAD"3nd%.result*)/d)*3ndex&0 1 , 32CA 4LC4CA 0 32CAB 32 =!= D;ID3A3;I =&= ' strcpy!\FAD"3ndex%.op*)++)&0 strcpy!\FAD"3ndex%.arg#*@7&0 strcpy!\FAD"3ndex%.arg6*)2ALC4)&0 strcpy!\FAD"3ndex%.result*)<#)&0
Balaji Institute of Technology and Science 2)

Department of Computer Science and Engineering

push!3ndex&0 3ndex>>0 1 PL;DN ' strcpy!\FAD"3ndex%.op*)Q;A;)&0 strcpy!\FAD"3ndex%.arg#*))&0 strcpy!\FAD"3ndex%.arg6*))&0 strcpy!\FAD"3ndex%.result*)<#)&0 push!3ndex&0 3ndex>>0 10 4LC4CAB 4LC4' t3nd+pop!&0 3nd+pop!&0 push!t3nd&0 sprint(!\FAD"3nd%.result*)/d)*3ndex&0 1 PL;DN' 3nd+pop!&0 sprint(!\FAD"3nd%.result*)/d)*3ndex&0 10 D;ID3A3;IB MAL L4L;P MAL 'Add\uadruple!@6*@#*@7*@@&0 CtIo+3ndex<#0 1 , MAL , IFH 0 WS3L4CAB WS3L4L;;P' 3nd+pop!&0 sprint(!\FAD"3nd%.result*)/d)*CtIo&0 3nd+pop!&0 sprint(!\FAD"3nd%.result*)/d)*3ndex&0 1 0 WS3L4L;;PB WS3L4 =!= D;ID3A3;I =&= '
Balaji Institute of Technology and Science 2,

Department of Computer Science and Engineering

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 3ndex>>0 1 PL;DN ' strcpy!\FAD"3ndex%.op*)Q;A;)&0 strcpy!\FAD"3ndex%.arg#*))&0 strcpy!\FAD"3ndex%.arg6*))&0 strcpy!\FAD"3ndex%.result*)<#)&0 push!3ndex&0 3ndex>>0 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 print(!).n.n.t.t <<<<<<<<<<<<<<<<<<<<<<<<<<<<)).n.t.t Pos ;perator Arg# Arg6 Lesult) ).n.t.t <<<<<<<<<<<<<<<<<<<<)&0
Balaji Institute of Technology and Science 2-

Department of Computer Science and Engineering

(or!i+$0i<3ndex0i>>& ' print(!).n.t.t /d.t /s.t /s.t /s.t /s)*i*\FAD"i%.op*\FAD"i%.arg#*\FAD"i%.arg6*\FAD"i%.result&0 1 print(!).n.t.t <<<<<<<<<<<<<<<<<<<<<<<)&0 print(!).n.n)&0 return $0 1 void push!int data& ' stk.top>>0 i(!stk.top++#$$& ' print(!).n Ctack over(lo .n)&0 exit!$&0 1 stk.items"stk.top%+data0 1 int pop!& ' int data0 i(!stk.top++<#& ' print(!).n Ctack under(lo .n)&0 exit!$&0 1 data+stk.items"stk.top<<%0 return data0 1 void Add\uadruple!char op"U%*char arg#"#$%*char arg6"#$%*char result"#$%& ' strcpy!\FAD"3ndex%.op*op&0 strcpy!\FAD"3ndex%.arg#*arg#&0 strcpy!\FAD"3ndex%.arg6*arg6&0
Balaji Institute of Technology and Science 2/

Department of Computer Science and Engineering

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<-& ' a+a>-0 1 hile!a<-& ' a+a>-0 1 i(!a<+-& ' c+a<-0 1 else ' c+a>-0 1 1 ;utputB @lex int.l @yacc Zd int.y @gcc lex.yy.c y.ta-.c Zll Zlm @.9a.out test.c OUTPUT:
Balaji Institute of Technology and Science 23

Department of Computer Science and Engineering

Balaji Institute of Technology and Science

2<

Das könnte Ihnen auch gefallen