www.alljntuworld.in
JNTU World
COMPILER DESIGN DESIGN LAB MANUAL
Downloaded From JNTU World (http://www.alljntuworld.in)
COMPUTER SCIENCE AND ENGINEERING
Program Outcomes PO1 PO, PO. PO/ PO PO3 PO5 PO6 PO7 PO1 PO11 PO1,
Engineering knowledge : Apply the knowledge of !the!t"#$% $#"
eng"nee&"ng f'nd!ent!l$% !nd !n eng"nee&"ng $pe#"!l"(!t"on to the $ Problem analysis : Ident"fy% fo&'l!te% &e-"ew &e$e!h l"te&!t'&e% #ople) eng"nee&"ng p&o*le$ &e!#h"ng $'*$t!nt"!ted #on#l'$"on$ p&"n#"ple$ of !the!t"#$% n!t'&!l $#"en#e$% !nd eng"nee&"ng $#"en#e$ Design/development of solutions : De$"gn $ol't"on$ fo& #ople) p&o*le$ !nd de$"gn $y$te #oponent$ o& p&o#e$$e$ th!t eet t need$ w"th !pp&op&"!te #on$"de&!t"on fo& the p'*l"# he!lth !nd $!f Conduct investigations investigations of complex problems : U$e &e$e!h0*!$e !nd &e$e!h ethod$ "n#l'd"ng "n#l'd"ng de$"gn of e)pe&"e e)pe&"ent$% nt$% ! "nte&p&et!t "nte&p&et!t"on "on of d!t!% !nd $ynthe$"$ $ynthe$"$ of the "nfo&!t"on "nfo&!t"on to p& Modern tool usage : C&e!te% $ele#t% !nd !pply !pp&op&"!te &e$o'e$% !nd ode&n eng"nee&"ng !nd I2 tool$ "n#l'd"ng p&e odel"ng to #ople) eng"nee&"ng !#t"-"t"e$ w"th !n 'nde&$t!nd Te engineer and society : Apply &e!$on"ng "nfo&ed *y the knowledge to !$$e$$ $o#"et!l% he!lth% $!fety% leg!l !nd #'lt'&!l "$$' #on$e4'ent &e$pon$"*"l"t"e$ &ele-!nt to the p&ofe$$"on!l eng"nee&"ng Environment and sustainability : Unde&$t!nd the "p!#t of the eng"nee&"ng $ol't"on$ "n $o#"et!l !nd en-"&onent!l #onte)t$% !nd the knowledge of% !nd need fo& $'$t!"n!*le de-elopent+ Etics: Apply eth"#!l p&"n#"ple$ !nd #o"t to p&ofe$$"on!l eth"#$ !n &e$pon$"*"l"t"e$ !nd no&$ of the eng"nee&"ng p&!#t"#e+ 8'n#t"on e9e#t"-ely !$ !n "nd"-"d'!l% !nd !ndividual and team work : 8'n#t"on e*e& o& le!de& "n d"-e&$e te!$% !nd "n 'lt"d"$#"pl"n!&y $ett"ng$+ Communication: Co'n"#!te e9e#t"-ely on #ople) eng"nee&"n w"th the eng"nee&"ng #o'n"ty !nd w"th $o#"ety !t l!&ge% $'#h !$% to #op&ehend !nd w&"te e9e#t"-e &epo&t$ !nd de$"gn do#'ent Deon$t&!te knowl Pro"ect management and #nance : 'nde&$t!nd"ng of the eng"nee&"ng !;d $ ow; wo&k% !$ !
Program &peci#c Outcomes PSO1 PSO, PSO.
Professional &kills' 2he !*"l"ty
to &e$e!h% &e$e!h% 'nde&$t!nd !nd #op'te& p&og&!$ "n the !&e!$ &el!ted to !lgo&"th$% $y$te 'lt"ed"!% we* de$"gn% *"g d!t! !n!lyt"#$% !nd netwo&k"ng fo& e#" Problem%&olving &kills' 2he !*"l"ty to !pply $t!nd!&d p&! $t&!teg"e$ "n $oftw!&e p&o@e#t de-elopent '$"ng open0ended p en-"&onent$ to del"-e& ! 4'!l"ty p&od'#t fo& *'$"ne$$ $'##e$$+ &uccessful Career and Entrepreneursip' 2he !*"l"ty to epl #op'te& l!ng'!ge$% en-"&onent$% !nd pl!tfo&$ "n #&e!t"ng "nnop!th$% to *e !n ent&ep&ene'&% !nd ! (e$t fo& h"ghe& $t'd"e$+
COMPILER DESIGN LAB SLLABUS
Sl. No.
List of Experiments
1
! # ' (
11 1!
* "rite a program to identify whether a given line is a comment or not.
$"rite a program to recognize strings under %a%, %a$b&%, %abb%. $"rite a program to test whether a given identifier is valid or not. $"rite a program to simulate lexical analyzer for validating operators.
)
Implement the lexical analyzer using *+ex, flex or other lexical analyzer generating tools.
"rite a program for implementing the functionalities of predictive parser for the mi language specified in -ote 1.
a) $"rite $"rite a program for constructing of ++ /10 parsing. b) $"rite $"rite a program for constructing recursive descent parsing. parsing .
13
Design a lexical analyzer for given language and the lexical analyzer should ignore redundant redundant spaces, tabs and new lines. It should also ignore comments. Although the syntax specification states that identifiers can be arbitrarily long, you may restrict the length to some reasonable value. Simulate the same in language.
"rite a program to implement +A+2 parsing. $"rite a program to implement operator precedence parsing. parsing. a) $"rite ite a pro progra gram to to imp imple leme men nt 4r 4rogr ogram sema semant ntic ic rule ruless to to b) $"rite
calc calcul ulat atee t expressi expression on that that ta5es ta5es an express expression ion with digits, digits, & and $ and comput computes es the value. value. onvert the 6-7 rules into 8acc 8acc form and write code to generate abstract syntax tree for the mini language specified in -ote 1.
"rite a program to generate machine code from abstract syntax t ree generated by th parser. 9he instruction instruction set specified specified in -ote ! may be considered as the target code.
*ontent beyond the :niversity prescribed syllabi
Note 1
A simple language written in this language is ;int a<#=,t1, t!> 91?!> A<3=?1>a<1=?!>a
9!?@/ a I f t ! B ( t h e n 4 r i n t / t ! 0 C l s e ; I n t t # > 9
# ? > 9 ! ? ! ( >
4rint/@t1&t!$t#0>$this is a comment on ! lines$ endif omments/zero or more characters enclosed between the standard *AEA Style comment brac5ets$F$0can be inserted .9he language has rudimentary support for1@dimenstional array,the declaration int a<#= declares an array of three elements,referenced as a<3=,a<1= and a
onsider the following mini language, a simple procedural high Glevel language, only operating on integer data, with a syntax loo5ing vaguely li5e a simple crossed with pascal. 9he syntax of the language is defined by the following grammar. HprogramB?Hbloc5B Hbloc5B?;Hvariable definitionBHslistB J;HslistB HvariabledefinitionB?int HvardeflistB HvardecB?HidentifierBJHidentifierBHslistB HstatementB?HassignmentBJHifstamentBJHwhilestatementB JHbloc5BJHprintstamentBJHemptyB HassignmentB?HidentifierB?HexpressionB JHidentifierB
statementB?ifHbexpressionBthenHslistBelseHslistBendif
JifHbexpressionBthenHslisiBendif HwhilestatementB?whileHbexpreessionBdoHslisiBenddo HprintstatementB>?print/HexpressionB0 HexpressionB?HexpressionB?HexpressionBHaddingopBHtermBJHtermBJHaddingopB HtermB HbexprssionB?HexpressionBHrelopBHexpressionB HrelopB?HJH?J??JB?JBJK? HaddingopB?&J@
HtermB?HtermBHmultopBHfactorBJHfactorB HLultopB?$J HfactorB?HconstantBJHidentifierBJHidentifierB
HidentifierB?HidentifierBHletter or digitBJHletterB Hletter or digitB?HletterBJHdigitB HletterB>?aJbJcJdJeJfJgJhJIJMJ5JlJmJnJoJpJNJrJsJtJuJvJwJxJyJz HdigitB?3J1J!J#J'J(JOJJJ HemptyB?has the obvious meaning
A22AINMEN2 O8 PROGRAM OU2COMES PROGRAM SPECI8IC OU2COMES
Exp. No.
1
! # '
Experiment
Design a lexical analyzer for given language and the lexical analyzer should ignore redundant spaces, tabs and new lines. It should also ignore comments. Although the syntax specification states that identifiers can be arbitrarily long, you may restrict the length to some reasonable value. Simulate the same in language. * "rite a program to identify whether a given line is a comment or not.
$"rite a program to recognize strings under %a%, %a$b&%, %abb%. $"rite a program to test whether a given identifier is valid or not.
Pro"r#m O$t%omes Att#ine&
4P1, 4P!, 4P#
4P1 4P1, 4P! 4P1
(
$"rite a program to simulate lexical analyzer for validating operators.
4P1, 4P!, 4P#
)
Implement the lexical analyzer using *+ex, flex or other lexical analyzer generating tools.
4P1, 4P!
"rite a program for implementing the functionalities of predictive parser for the mini language specified in -ote 1.
4P1, 4P!, 4P', 4P(
a) $"rite a program for constructing of ++ /10 parsing. b) $"rite a program for constructing recursive descent parsing.
4P1, 4P!, 4P#, 4P'
"rite a program to implement +A+2 parsing.
4P1, 4P!, 4P'
13
11 1!
a) $"rite a program to implement operator precedence parsing. b) $"rite a program to implement 4rogram semantic rules to calculate the expression that ta5es an expression with digits, & and $ and computes the value. onvert the 6-7 rules into 8acc form and write code to generate abstract syntax tree for the mini language specified in -ote 1. "rite a program to generate machine code from abstract syntax tree generated by the parser. 9he instruction set specified in -ote ! may be considered as the target code.
*ontent beyond the :niversity prescribed syllabi
4P1, 4P!
4P1, 4P! 4P1, 4P!, 4P#, 4P', 4P(
COMPILER DESIGN LABORA2OR O()ECT!*E'
2h"$ l!*o&!to&y #o'&$e "$ "ntended to !ke the $t'dent$ e)pe&"ent on the *!$"# te#hn"4'e$ of #op"le& #on$t&'#t"on !nd tool$ th!t #!n '$ed to pe&fo& $ynt!)0d"&e#ted t&!n$l!t"on of ! h"gh0le-el p&og&!"ng l!ng'!ge "nto !n e)e#'t!*le #ode+ St'dent$ w"ll de$"gn !nd "pleent l!ng'!ge p&o#e$$o&$ "n C *y '$"ng tool$ to !'to!te p!&t$ of the "pleent!t"on p&o#e$$+ 2h"$ w"ll p&o-"de deepe& "n$"ght$ "nto the o&e !d-!n#ed $e!nt"#$ !$pe#t$ of p&og&!"ng l!ng'!ge$% #ode gene&!t"on% !#h"ne "ndependent opt""(!t"on$% dyn!"# eo&y !llo#!t"on% !nd o*@e#t o&"ent!t"on+
O+TCOME&'
Upon the #oplet"on of Cop"le& De$"gn p&!#t"#!l #o'&$e% the $t'dent w"ll *e !*le to: 1. +nderstand the wo&k"ng of le) !nd y!## #op"le& fo& de*'gg"ng of
p&og&!$+
2. +nderstand !nd dene the &ole of le)"#!l !n!ly(e&% '$e of
&eg'l!& e)p&e$$"on !nd t&!n$"t"on d"!g&!$+
3. +nderstand !nd '$e Conte)t f&ee g&!!&% !nd p!&$e t&ee #on$t&'#t"on+ 4. $earn '$e the new tool$ !nd te#hnolog"e$ '$ed fo& de$"gn"ng ! #op"le&+ 5. Develop p&og&! fo& $ol-"ng p!&$e& p&o*le$+ 6. $earn how to w&"te p&og&!$ th!t e)e#'te f!$te&+
E'PERIMENT( 1 O)ECTI+E
1.1
Design a lexical analyzer for given language and the lexical analyzer should ignore redundant spaces, tabs and new lines. It should also ignore comments. Although the syntax specification states that identifiers can be arbitrarily long, you may restrict the l ength to some reasonable value. Simulate the same in language. 1.2
RESOURCE
9urbo && 1.3
PROGRAM LOGIC
1. 2ead the input Cxpression 2. hec5 whether input is alphabet or digits then store it as identifier 3. If the input is is operator store it as symbol 4. hec5 the input for 5eywords
1.4
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program 1.5
PROGRAM
RincludeHstring.hB RincludeHctype.hB RincludeHstdio.hB void 5eyword/char str<13=0 ; if/strcmp/for,str0??3JJstrcmp/while,str0??3JJstrcmp/do,str0??3JJstrcmp/int,str0??3JJstr cmp/float,str0??3JJstrcmp/char,str0??3JJstrcmp/double,str0??3JJstrcmp/static,str0??3JJ strcmp/switch,str 0??3JJstrcmp/case,str0??30 printf/TnUs is a 5eyword,str0> else
printf/TnUs is an identifier,str0>
main/0 ;
7I+C $f1,$f!,$f#> char c,str<13=,st1 <13=> int num<133=,lineno?3,to5envalue?3,i?3,M?3, 5?3> printf/TnCnter the c
program0>$gets/st10>$
f
ut,w0> while//c?getchar/00K?CP70
1
putc/c,f10>
?
fclose/f10>
f
f1?fopen/input,r
o
0>
p
f!?fopen/identifie
e
r,w0>
n
f#?fopen/specialch
/
ar,w0>
while//c?getc/f100K
i
?CP70 ;
n
if/isdigit/c00
p
; to5envalue?c@%3%>
1
c?getc/f10> while/isdigit/c00 ; to5envalue$?13&
c@%3%> c?getc/f10> num
&=?to5envalue
else
> ungetc/c,f10>
if/isalpha/c00 ; p u t c / c , f ! 0 > c ? g e t c / f 1 0 >
while/isdigit/c0JJisalpha/c0JJc??%V%JJc??%W%0 ; p u t c / c , f !
0 > c ? g e t c / f 1 0 >
putc/% %,f!0> unget
else c/c,f1 0>
if/c??% %JJc??%Tt%0 printf/ 0> else
if/c??%Tn%0 lineno&&> else
fclose/ f!0> fclose/ f#0> fclose/ f10> printf/Tn9he no%s in the program are0> for/M?3>MHi>M& &0 printf/Ud,num printf/Tn0> f!?fopen/identif ier,r0> 5?3>
printf/9he 5eywords and identifiersare0> while//c?getc/f!00K?CP70
; i f /
putc/c,f#0>
c K ? % % 0 s t r < 5 & & = ? c > e l s e ; str<5=?%T3 %> 5eyword /str0> 5?3>
,
fclose/f!0> f#?fopen/specialchar,r 0> printf/TnSpecial characters are0> while//c?getc/f#00K?CP70 printf/Uc,c0> printf/Tn 0> fclose/f# 0> printf/9otal no. of lines areUd,lineno0> 1.6
PRE LA) ,UESTIONS
1. "hat is to5enX 2. "hat is lexemeX 3. "hat is the difference between to5en and lexemeX 4. Define phase and passX 5. "hat is the difference between phase and passX 6. "hat is the difference between compiler and interpreterX
1.7
LA) ASSIGNMENT
1. "rite a program to recognize identifiers. 2. "rite a program to recognize constants. 3. "rite a program to recognize 5eywords and identifiers. 4. "rite a program to ignore the comments in the given input source program. 1.8
POST LA) ,UESTIONS
1. "hat is lexical analyzerX 2. "hich compiler is used for lexical analyzerX 3. "hat is the output of +exical analyzerX 4. "hat is +CY source 4rogramX
1.9
INPUT OUTPU T Inp$t
Cnter 4rogram W for termination ; int a<#=,t1,t!> t1?!> a<3=?1> a<1=?!> a t!?@/a i f
t ! B ( t h e n p r i n t / t ! 0 > e l s e ; i n t t # > t # ? > t ! ? @ ! ( > print/@t1&t!$t#0> $ this is a comment on ! lines $ endif
W O$tp$t
Eariables a<#= t1 t! t# Pperator @ & $ B onstants ! 1 # ) ( @!( Zeywords int if then else endif Special Symbols , > / 0; omments this is a comment on ! lines
.
E'PERIMENT(! 2.1
O)ECTI+E * "rite a program to identify whether a given line i s a comment or not.
2.2
RESOURCE
9urbo && 2.3
PROGRAM LOGIC
2ead the input string. hec5 whether the string is starting with [\ and chec5 next character is [\ or\$\. If condition satisfies print comment. Clse not a comment. 2.4
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program. 2.5
PROGRAM
RincludeHstdio.hB Rinclud eHconio .hB void main/0 ; ch ar co m< #3 => int i? !, a? 3> clr scr /0> printf/Tn Cnter comment0> gets/com0> if/com<3=??%%0 ; if/com<1=??%%0 printf/Tn It is a comment0> else if/com<1=??%$%0 ; for/i?!>iH?#3>i&&0 ;
if/com a?1> brea5>
else
else con tinu e> if/a??30 printf/Tn It is not a comment0>
printf/Tn It is not a comment0> else
2.6
printf/Tn It is not a comment0> getch/0> INPUT - OUTPUT Inp$t Cnter comment hello O$tp$t It is a comment Inp$t Cnter comment hello O$tp$t It is not a comment
/
E'PERIMENT( 3.1
O)ECTI+E
$"rite a program to recognize strings under %a$%, %a$b&%, %abb%.
3.2
RESOURCE
9urbo &&
3.3
PROGRAM LOGIC
6y using transition diagram we verify input of the state. If the state recognize the given pattern rule. 9hen print string is accepted under a$ a$b& abb. Clse print string not accepted. 3.4
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program.
3.5
PROGRAM
RincludeHstdio.hB RincludeHconio.hB RincludeHstring.hB RincludeH stdlib.hB void main/0 ; char s int state? 3,i?3> clrscr/ 0> printf/Tn Cnter a string0> gets/s0> while/s
;
state?1> case 3 c?s if/c??%a%0 else if/c??%b%0 state?!> else state?)> b rea5> case 1 c?s if/c??%a%0 state?#>
else if/c??%b%0 state?'> e l state?)> s e
b rea5> case ! c?s if/c??%a%0 s tate? )> else if/c? ?%b%0 state?!> e l state?)> s e
brea5>
case # c?s if/c??%a%0 s tate?# > else
if/c? ?%b%0 state?!> e ls e
b rea5> case ' c?s
state?)> if/c??%a%0 state?)>
else if/c??%b%0 state?(> el s state?)> e
b state?)>
rea5> case ( c?s if/c??%a%0
else if/c??%b%0 state?!> else
st at e ? )> br e a 5> case ) printf/Tn Us is not recognised.,s0> exit/30>
3
I
f/state??10 printf/Tn Us is accepted under rule %a%,s0> else if//state??!0JJ/state??'00 printf/Tn Us is accepted under rule %a$b&%,s0> else if/state??(0 printf/Tn Us is accepted under rule %abb%,s0> getch/0>
3.6
INPUT OUTPU T Inp$t
Cnter a String aaaabbbbb
O$tp$t
aaaabbbbb is accepted under rule %a$b&% Cnter a string cdgs cdgs is not recognize d
5
E'PERIMENT(/ 4.1
O)ECTI+E
$"rite a program to test whether a given identifier is valid or not 4.2
RESOURCE
9urbo && 4.3
PROGRAM LOGIC
2ead the given input string. hec5 the initial character of the string is numerical or any special character except [V\ then print it is not a valid identifier. Ptherwise print it as valid identifier if remaining characters of string doesn\t contains any special characters except [V\. 4.4
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program.
4.5
PROGRAM
RincludeHstdio.hB RincludeHconio.hB Rinclude Hctype.h B void main/0 ; c h a r a < 1 3 = > i n t f l a g , i
? 1 > c l r s c r / 0 > printf/Tn Cnter an identifier0> gets/a0> if/isalpha/a<3=00 flag?1> else
printf/Tn -ot a valid identifier0>
while/a
i & & >
f l a g ? 3 > b r e a 5 >
if/flag??10 printf/Tn Ealid identifier0> getch/0> 4.6
INPUT - OUTPUT Inp$t Cnter an identifier first O$tp$t
Ealid identifier Cnter an identifier1aN w -ot a valid identifier
6
E'PERIMENT(0 5.1
O)ECTI+E
$"rite a program to simulate lexical analyzer for validating operators.
5.2
RESOURCE
9urbo &&
5.3
PROGRAM LOGIC
2ead the given input. If the given input matches with any operator symbol. 9hen display in terms of words of the particular symbol. Clse print not a operator. 5.4
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program.
5.5
PROGRAM
RincludeHstdio.hB Rinclude Hconio.h B void main/0 ; char s<(=> clrscr/0> printf/Tn Cnter any operator0> gets/s0> switch/s<3=0 ; case%B% if/s<1=??%?%0 printf/Tn Qreater than or eNual0> else printf/Tn Qreater than0> brea5> case%H% if/s<1=??%?%0 printf/Tn +ess than or eNual0> else printf/T n+ess than0>
brea5> case%?% if/s<1=??%?%0 printf/TnC Nual to0> else printf/TnA ssignment0 > brea5> case%K% if/s<1=??%?%0 printf/Tnot CNual0> else printf/Tn 6it -ot0> brea5> case%]% if/s<1=??%]%0 printf/Tn+ogica l A-D0> else printf/Tn 6itwise A-D0> brea5> case%J% if/s<1=??%J%0 printf/Tn+ogical P20>
7
else printf/Tn6i twise P20> brea5> case%&% printf/Tn Addition 0> brea5> case%@% printf/TnSub straction0> brea5> case%$% printf/TnLultipli cation0> brea5> case%% printf/TnDivision0> brea5> case%U% printf/Lodulus0> brea5> default printf/Tn -ot a operator0> getch/0>
5.6
INPUT OUTPU T Inp$t
Cnter any operator $ O$tp$t Lultiplic ation
1
E'PERIMENT(
6.1
O)ECTI+E
Implement the lexical analyzer using *+ex, flex or other lexical analyzer generating tools.
6.2
RESOURCE
+inux using 4utty
6.3
PROGRAM LOGIC
2ead the input string. hec5 whether the string is identifier 5eyword symbol by using the rules of identifier and 5eywords using +CY 9ool
6.4
PROCEDURE
Qo to terminal .Ppen vi editor ,+ex lex.l , cc lex.yy.c , .a.out
6.5
PROGRAM
$ program name is lexp.l $ U; $ program to recognize a c program $ int PLLC-9?3> U identifier int Jfloat Jchar Jdouble Jwhile Jfor Jdo Jif Jbrea5 Jcontinue Jvoid Jswitch Jcase Jlong Jstruct Jconst Jtypedef Jreturn Jelse Jgoto ;printf/TnTtUs is a ZC8"P2D,yytext0> $ ;PLLC-9 ? 1> $;printf/TnTnTtUs is a PLLC-9Tn,yytext0>$ $ ;PLLC-9 ? 3> $ printf/TnTnTtUs is a PLLC-9Tn,yytext0>$ ;identifierT/ ;if/KPLLC-90printf/TnTn7:-9IP-TnTtUs,yytext0> ; ;if/KPLLC-90 printf/Tn 6+PZ 6CQI-S0> ;if/KPLLC-90 printf/Tn 6+PZ C-DS0> ;identifier/T<<3@=$T=0X ;if/KPLLC-90 printf/Tn Us IDC-9I7IC2,yytext0> .$T ;if/KPLLC-90 printf/TnTtUs is a S92I-Q,yytext0> <3@=& ;if/KPLLC-90 printf/TnTtUs is a -:L6C2,yytext0> ;if/KPLLC-90 printf/TnTt0>C_P>printf/Tn0> /
C_P> ;if/KPLLC-90printf/TnTtUs is an ASSIQ-LC-9 P4C2A9P2,yytext0> H? JB? JH J?? JB ;if/KPLLC-90 printf/TnTtUs is a 2C+A9IP-A+ P4C2A9P2,yytext0> UU int main/int argc,char $$argv0 ; if /argc B 10 ; 7I+C $file> file ? fopen/argv<1=,r 0> if/Kfile0 ; printf/could not open Us Tn,argv<1=0> exit/30> yyin ? file> yylex/0>
11
6.6
6.7
6.8
p ri nt f/ T nT n 0 > re tu r n 3> int yywrap/0 ; return 3> PRE LA) ,UESTIONS 1.
+ist the different sections available in +CY compil erX
2.
"hat is an auxiliary definitionX
3.
_ow can we define the translation rulesX
4.
"hat is regular expressionX
5.
"hat is finite automatonX
LA) ASSIGNMENT
1.
"rite a program that defines auxiliary definitions and translation rules of 4ascal to5ensX
2.
"rite a program that defines auxiliary definitions and translation rules of to5ensX
3.
"rite a program that defines auxiliary definitions and translation rules of *AEA to5ens
POST LA) ,UESTIONS
1.
"hat is *lexX
2.
"hat is 7lexX
3.
"hat is lexical analyzer generatorX
4.
"hat is the input for +CY ompilerX
5.
"hat is the output of +CY compilerX
1.1
INPUT OUTPU T Inp$t
Wvi var.c RincludeH stdio.hB main/0 ; int a,b>
O$tp$t
Wlex lex.l Wcc lex.yy.c W.a.out var.c RincludeHstdio.hB is a 42C42PCSSP2 DI2C9IEC 7:-9IPmain / 0 6+PZ 6CQI-S int is a ZC8" P2D a IDC-9I 7IC2 b IDC-9I7I C2 6+PZ C-DS
1,
E'PERIMENT(2
7.1
O)ECTI+E
"rite a program for implementing the functionaliti es of predictive parser for the mini language specified in -ote 1. 7.2
RESOURCE
9urbo && 7.3
PROGRAM LOGIC
2ead the input string. 6y using the 7I2S9 A-D 7P++P" values. Eerify the 7I2S9 of non terminal and insert the production in the 7I2S9 value If we have any ` terms in 7I2S9 then insert the productions in 7P++P" values onstructing the predictive parser table 7.4
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program.
7.5
PROGRAM
RincludeHstdio.hB RincludeHconio.hB RincludeHstring.hB char
prol<=<13=?;S,A,A,6,6,,>
char
pror<=<13=?;A,6b,d,a6,`,c,`>
char
prod<=<13=?;S@BA,A@B6b,A@Bd,6@Ba6,6@B`,@Bc,@B`>
char
first<=<13=?;abcd,ab,cd,a`,`,c`,`>
char
follow<=
<13=?;W,W,W,aW,bW,cW,dW> char
table<(=<)=<13=> numr/char c0 ; switch/c0 ; case %S% retur n 3> case %A% retur n 1>
case %6% retur n !> case %% retur n #> case %a% retur n 3> case %b% retur n 1> case %c% retur n !> case %d% retur n #> case %W% retur n '>
1.
return/!0> void main/0 ; int i,M,5> clrscr/ 0> for/i?3 >iH(>i& &0 for/M?3 >MH)>M& &0
strcpy/table printf/Tn9he following is the predictive parsing table for the following grammarTn0> for/i?3>iH>i&&0 printf/UsTn,prod printf/Tn4redictive parsing table isTn0> fflush/stdin0> for/i?3>iH>i&&0 ; 5?str len/f irst for/M ?3>MH 13>M& &0
if/fir st for/i?3>iH>i&&0 ; if/strlen/pror for/M?3>M H5>M&&0 strcpy/table
1/
strcpy/table<3=<3=, 0> strcpy/table<3=<1=,a0> strcpy/table<3= strcpy/table<3=<#=,c0> strcpy/table<3=<'=,d0> strcpy/table<3=<(=,W0> strcpy/table<1=<3=,S0> strcpy/table strcpy/table<#=<3=,60> strcpy/table<'=<3=,0> printf/Tn@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Tn0> for/i?3 >iH(>i& &0 for/M?3 >MH)>M& &0 ; printf/U@ 13s,table
if/M??(0 printf/Tn@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Tn0> getch/0>
7.6
PRE LA) ,UESTIONS
1.
"hat is top@down parsingX
2.
"hat are the disadvantages of brute force methodX
3.
"hat is context free grammarX
4.
"hat is parse treeX
5.
"hat is ambiguous grammarX
6.
"hat are the derivation methods to generate a string for the given grammarX
7.
"hat is the output of parse treeX
1
7.7
LA) ASSIGNMENT
1.
"rite a program to compute 7I2S9 for the following grammarX C 9C% C %
& 9 C % 9
7 9 \ 9 %
$ 7 9 % 7
/ C 0 i 2.
"rite a program to compute 7I2S9 for the following grammarX SitSS\ S\eS
3.
"rite a program to construct predictive parsing table for the following grammarX SitSS\ S\eS
7.8
POST LA) ,UESTIONS
1.
"hat is 4redictive parserX
2.
_ow many types of analysis can we do using 4arserX
7.9
3.
"hat is 2ecursive Decent 4arserX
4.
_ow many types of 4arsers are thereX
5.
"hat is +2 4arserX
INPUT - OUTPUT
9he following is the predictive parsing table for the following grammar S @ B A A @ B 6 b A @ B d 6 @ B a 6 6 @ B ` @ B c @ B ` 4redictive parsing table is @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ a
b
c
d
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
W
@@@@@@@@@@@@@@@ S S@BA
S@BA
S@BA
S@BA
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@ A A@Bd
A@B6b
A@B6b
A@Bd
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@
6
6@B`
6@Ba6
6@B`
6@B`
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@
@B`
@B`
@B` @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
13
E'PERIMENT(34#5 8.1
O)ECTI+E
$"rite a program for constructing of ++ /10 parsing. 8.2
RESOURCE
9urbo && 8.3
PROGRAM LOGIC
2ead the input string. :sing predictive parsing table parse the given input using stac5 . If stac5
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program. 8.5
PROGRAM
RincludeHstdio.hB RincludeHconio.hB RincludeH string.hB char s void main/0 ; char m<(=<)=<#=?;tb, , ,tb, , , ,&tb, , ,n,n,fc, , ,fc, , , ,n,$fc, a ,n,n,i, , ,/e0, , > int
size<(=<)=?;!,3,3,!,3,3,3,#,3,3,1,1,!,3,3,!,3,3,3,1,#,3,1,1,1,3,3,#,3,3>
int i,M,5,n, str1,str !> clrscr/ 0> printf/Tn Cnter the input string 0> scanf/Us,s0> strcat/ s,W0>
n?strl en/s0> stac5<
3=?%W%> s t a c 5 < 1 = ? % e % >
i ? 1 > M?3> printf/TnStac printf/TnStac5 5
InputTn0> printf/ printf/ Tn0 > while//stac5
M & & >
15
switch/stac5 b rea5> case %b% str1?1> b rea5> case %t% str1?!> b rea5> case
%c% str1?#> b
switch/s
rea5> case %f% str1?'> brea5>
case %i% str!?3> b rea5> case %&% str!?1> b rea5> case %$%
str!?!> b rea5> case %/% str!?#> b rea5> case %0% str!?'> b
r
> case %W%
e
str!?(>
a
brea5>
5 if/m exit/30> else if/m else if/m
16
s t a c 5 < i = ? % i % >
e l s e ; for/5?size5B?3>5@@0 ; stac5 i&&> i@@>
for/5?3>5 H?i>5&&0 printf/ Uc,stac5 <5=0> printf/
0> for/5?M>5 H?n>5&&0
printf/U c,s<5=0> printf/ Tn 0> printf/Tn S:CSS0> getch/0> 8.6
INPUT - OUTPUT
Cnter the input stringi$i&i Stac5
I-4:9
Wbt
i$i&iW
Wbcf
i$i&iW
Wbci
i$i&iW
Wbc
$i&iW
Wbcf$
$i&iW
Wbcf
i&iW
Wbci
i&iW
Wbc
&iW
Wb
&iW
Wbt&
&iW
Wbt
iW
Wbcf
iW
W bci
iW
Wbc
W
Wb
W
W
W
success
17
E'PERIMENT(3465
8.1
O)ECTI+E
onstruction of recursive descent parsing for the following grammar C@B9C% C%@B&9C`
` represents
null character 9@B79% 9 @ B $ 7 9 % `
7 @ B / C 0 I D
8.2
RESOURCE
9urbo && 8.3
PROGRAM LOGIC
2ead the input string. "rite procedures for the non terminals Eerify the next to5en eNuals to non terminals if it satisfies match the non terminal. If the input string does not match print error. 8.4
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program. 8.5
PROGRAM
RincludeHstdio.hB
RincludeHconio.hB Rinclu deHstr ing.hB char input< 133=> int i,l> void main/0 ; clrscr/0> printf/Tn2ecursive descent parsing for the following grammarTn0> printf/TnC@B9C%TnC%@B&9C%`Tn9@ B79%Tn9%@B$79%`Tn7@B/C0IDTn0> printf/TnCnter the string to be chec5ed0> g e t s / i n p u t 0 > i f / C / 0 0 ; if/input els e
printf/TnString is not accepted0>
else
printf/TnString not accepted0>
,
getch/ 0>
C / 0 ;
if/9/00 ;
if/C4/00 r e
else t else
u r n / 1 0 > r e t u r n / 3
0
>
return/30> C 4 / 0 ; if/input if/ 9/ 00
; else
i f / C 4 / 0 0 ret ur n/ 10 > el se ret ur n/ 30 >
;
return/30>
r n /
e
1
l
0
s
>
e if/7/00 r e t
;
u
if/94/00 r et u else
r n / 1 0> el s e r et u r n / 3 0>
,1
return/30> 9 4 /0
if/input
;
i & & > i f
/
else
7 / 0 0 ; if/94/00 r e t u r n / 1 0 > e l s e r e t u r n /
3
>
0 return/30> else
return/10> 7/0 ;
if/input i f / C / 0 0 ; if/input return/10> else
return/30> e l s e r e t u r
n / 3 0 > else
if/input
; i&&> return/10>
,,
e l s e r e t u r n / 3 0 >
8.6
INPUT - OUTPUT
2ecursive descent parsing for the following grammar C@B9C% C % @ B & 9 C % ` 9 @ B 7 9 % 9 % @ B
$ 7 9 % ` 7 @ B / C 0 I D Cnter the string to be chec5ed /a&b0$c String is accepted 2ecursive descent parsing for the following grammar C@B9C% C % @ B & 9 C % ` 9 @ B 7 9 % 9 % @ B $ 7
9 % ` 7 @ B / C 0 I D Cnter the string to be chec5edac&d String is not accepted
,.
E'PERIMENT(7
9.1
O)ECTI+E
"rite a program to Design +A+2 6ottom up 4arser.
9.2
RESOURCE
9:26P &&
9.3
PROGRAM LOGIC
2ead the input string. 4ush the input symbol with its state symbols in to the stac5 by referring loo5aheads "e perform shift and reduce actions to parse the grammar. 4arsing is completed when we reach W symbol.
9.4
PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program.
9.5
PROGRAM
$+A +2 4A2 SC2 C@ BC& 9 C@B9 9 @ B 9 $ 7 9 @ B 7 7
@ B / C 0 7 @ B i $ RincludeHstdio.hB RincludeHconio.hB RincludeHstdlib.hB RincludeHstring.hB void push/char $,int $,char0> char stac5top/char $0> void isproduct/char, char0> int ister/char0> int isnte r/cha r0> int isstat e/ch ar0> void error /0> void isreduce/char,c har0> char pop/char $,int $0> void printt/char $,int $,char <=,int0> void rep/char <=,int0> struct action ; char row<)=<(=> >
,/
const struct action A<1!=?;
;sf,emp,emp,se,emp,emp, ;emp,sg,emp,emp,emp,acc, ;emp,rc,sh,emp,rc,rc, ;emp,re,re,emp,re,re, ;sf,emp,emp,se,emp,emp, ;emp,rg,rg,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, ;emp,rf,rf,emp,rf,rf
>
struct gotol ; char r<#=<'=> > ;b,c,d,
const struct gotol Q<1!=?;
;emp,emp,emp, ;emp,emp,emp, ;emp,emp,emp, ;i,c,d, ;emp,emp,emp, ;emp,M,d, ;emp,emp,5, ;emp,emp,emp, ;emp,emp,emp,
>
char ter<)=?;%i%,%&%,%$%,%0%,%/%,%W%> char nter<#=?;%C%,%9%,%7%>
char states<1!=?;%a%,%b%,%c%,%d%,%e%,%f%,%g%,%h%,%m%,%M%,% 5%,%l%> char stac5<133=>
int to p? @1> ch ar te m p< 13 => str uc t gr a m m ar ;
,
c h a r l e f t > c h a r r i g h t < ( = > > const struct grammar rl<)=?; ;%C%,e&9, ;%C%,9, ;%9%,9$7, ;%9%,7, ;%7%,/C0, ;%7%,i, > void main/0 ; char inp<3=,x,p,dl<3=,y, bl?%a%> int i?3,M,5,l,n,m,c,len> clrscr/0> printf/ Cnter the input 0> scanf/Us,inp
0> len?strlen/inp0 > inp inp push/stac5,]top, bl0> printf/Tn stac5 TtTtTt input0> printt/stac5,]top ,inp,i0> do ; x?inp< i=> p?stac 5top/st ac50> isproduct/x,p0> if/strcmp/temp,em p0??30 error/0> if/strcmp/temp,acc 0??30
brea5> else ;
if/temp<3=??%s%0 ; push/stac5,]to p,inp push/stac5,]to p,temp<1=0> i& &>
,3
else ;
if/temp<3=??%r%0 ; M?isstate/tem p<1=0> strcpy/temp, rl dl<3=?rl dl<1=?%T3%> n?strlen/temp0> for/5?3>5H!$n>5& &0 pop/stac5,]top0>
for/m?3>dlm&&0 push/stac5,]top,dl l ? t o p > y ? s t a c 5 < l @ 1 = >
isreduce/y,dl<3=0> for/m?3>tempm&&0 push/stac5,]top,temp printt/stac5,]top,inp,i0> while/inp if/strcmp/temp,acc0??30
printf/ Tn accept the input 0> else printf/ Tn do not accept the input 0> getch/0>
void push/char $s,int $sp,char item0 ; if/$sp??1330 printf/ stac5 is full 0> else ;
$sp?$sp&1>
,5
s<$sp=?item> char stac5top/char $s0 ; c h a r i> i ? s
r e t u r n i> void isproduct/char x,char p0 ; i n t
5 , l >
5 ? i s t e r / x 0 >
l ? i s s t a t e / p 0 >
strcpy/temp,A int ister/char x0 ; int i> for/i?3 >iH)>i& &0
if/x??ter return 3>
int isnter/char x0 ; int i> for/i?3 >iH#>i& &0
if/x??nter return 3> int isstate/char p0 ; int i> for/i?3 >iH1!>i &&0
if/p??states
,6
return i&1> return 3> void error/0 ; printf/ error in the input 0> exit/30> void isreduce/char x,char p0 ; i n t
5 , l >
5 ? i s s t a t e / x 0 >
l ? i
s n t e r / p 0 > strcpy/temp,Q<5@1=.r
char pop/char $s,int $sp0 ; c h a r
i t e m >
i f / $ s p ? ? @ 1 0
printf/ stac5 is empty 0>
else ;
item?s<$sp=> $sp?$sp@1>
return item> void printt/char $t,int $p,char inp<=,int i0 ; i n t
r >
p r i n t f / T n 0 >
for/r?3>rH?$p>r&&0 rep/t,r0> printf/TtTtTt0> for/r?i>inpr&&0
,7
printf/Uc,inp void rep/char t<=,int r0 ; c h a r
c >
c ? t < r = >
s w i t c h / c 0 ; case %a% printf/30> brea5> case %b% printf/10> brea5> case %c% printf/!0> brea5>
case %d% printf/#0> brea5> case %e% printf/'0> brea5> case %f% printf/(0> brea5> case %g% printf/)0> brea5> case %h% printf/0> brea5> case %m% printf/0> brea5> case %M% printf/0> brea5> case %5% printf/130> brea5> case %l% printf/110> brea5> default printf/ Uc,t brea5>
.
9.6
PRE(LA) ,UESTIONS
1 2 3 4 5
9.7
"hy bottom@up parsing is also called as shift reduce parsingX "hat are the different types of bottom up parsersX "hat is mean by +2 /30 itemsX "rite the general form of +2/10 itemX "hat is 8AX
LA) ASSIGNMENT
1
"rite a program to compute 7P++P" for the following grammarX C 9C% C%& 9C% 97 9\ 9%$ 79% 7/ C0i
2
"rite a program to construct +A+2 parsing table for the following grammar. SitSS\ S\eS
9.8
POST(LA) ,UESTIONS
1. 2. 3. 4. 5.
9.9
"hat is +A+2 parsingX "hat is Shift reduced parserX "hat are the operations of 4arserX "hat is the use of parsing tableX "hat is bottom up parsingX
INPUT
-
OUTPU
T Cnter
the input i$i&1
O$tp$t
St#%8
inp$t
3
i$i&iW
3i(
$i&iW
37#
$i&iW
39!
$i&iW
39!$
i&iW
39!$i(
&iW
39!$i(713
&iW
39!
&iW
3C1
&iW
3C1&)
iW
3C1&)i(
W
3C1&)7#
W
3C1&)9
W
3C1
W
accept the input$ .1
E'PERIMENT(194#5
10.1 O)ECTI+E
$"rite a program to implement operator precedence parsing.
10.2 RESOURCE
9urbo &&
10.3 PROGRAM
LOGIC
2ead the arithmetic input string. Eerify the precedence between terminals and symbols 7ind the handle enclosed in H . B and reduce it to production symbol. 2epeat the process till we reach the start node. 10.4 PROCEDURE
Qo to debug @B run or press 92+ & 7 to run the program.
10.5 PROGRAM
Rinclude Hstdio.hB char str<(3=,op str<(=> int f
int col,col 1,col!> char c> swt/0 ; switch/c0 ;
case%&%col
?
col?1>brea
3
5>
>
case%$%col?
b
!>brea5>
r
case%%col?
e
#>brea5>
a
case%O%col?
5
'>brea5>
>
case%/%col? (>brea5>
c
case%0%col?
a
)>brea5>
s
case%d%col?
e
>brea5>
%
case%W%col?
@
>brea5>
%
defaultprintf/Tn9C2LI-A+
LISSLA9_Tn0> exit/10>
.,
brea5>
return 3> main/0 ; int i?3,M?3,col1 ,cn,5?3> int t1?3,foundg ?3> char tem p clrsc r/0> printf/TnCnter arithmetic expression0> scanf/Us,]str0> while/str str str<&&i=?%T3%> printf/ UsTn ,str0> come i?3> o p s t r < 3 =
? % W % > M ? 1 > c?%W%> s w t / 0 >
c o l 1 ? c o l >
c ? s t r < i = > s w
t / 0 >
c o l ! ? c o l >
if/f<1= M & & > else if/f<1=
r< M= ?% H% > M & & >
..
else ;
opstrM&&>
while/str s w t/ 0> c ol 1 ? c ol > c ? st r< & & i= > s w t/ 0> c ol ! ? c
ol > opst r M&&> if/f<3= M& &> else if/f<3=
M & & >
else ; opstrM&&>
opstr
i & & >
opstr<&&M=?%T3%> printf/Tn4recedence Input UsTn,opstr0> i?3> M?3> while/opstr
./
; foundg?3> while/found gK?10 ; if/opstr if/opstr t1?i> i&&> if/ fou nd g? ?10 for /i? t1>i B3> i@@0 if/opstr if/i??30 ;printf/TnC22P2Tn0>exit/ 10> cn?i> M ? 3 >
i ? t 1 & 1 >
while/opstr M&
&>i& &> temp opstr opstr<& &cn=?%T3%> strcat/opst r,temp0> printf/Tn Us,opstr 0> i?1> redone5?3> while/opstr< 5=K?%T3%0 ; 5&&> if/opstr<5=??%H%0 ; 4rintf/ TnCrr or0> exit/1 0>
.
if//opstr<3=??%W%0]]/opstr
sue> i?1
while/opstr if/c??%&%JJ c??%$%JJc??%%JJc??%W%0 ; tem pM &&>
i&&> temp strcpy/s tr,temp0 > goto come> sue printf/Tn success0> return 3>
10.6 INPUT
- OUTPUT
Cnter the arithmetic expression /d$d0&dW
O$tp$t
/d$d0&dW 4recedence inputWH/HdB$HdB0B&HdBW WH/C$HdB0B&HdBW WH/C$C0B&HCBW WC&HCBW
WC&CW 4recedence inputWH&BW W C W
s u c c e s s
.3
E'PERIMENT(19465
10.1 O)ECTI+E
4rogram to implement semantic rules to calculate the expression that ta5es an expression with digits, & and $ and computes the value.
10.2 RESOURCE
+inux using putty
10.3 PROCEDURE
2eading an input file alculate the sum or multiplication of given expression. :sing expression rule print the result of the given values.
10.4 PROGRAM
Hparser.lB
U; RincludeHstdio.hB Rinclude y.tab.h U UU <3@=& ;yylval.dval?atof/yytext0 > return DIQI9> TnJ. return yytext<3=> UU Hparser.yB U; $9his 8A specification file generates the +A+2 parser for the program considered in experiment '.$ RincludeHstdio.hB U
Uunion ; double dval> Uto5en HdvalB DIQI9 Utype HdvalB expr Utype HdvalB term Utype HdvalB factor UU line expr %Tn% ;
.5
printf/UgTn,W10> > expr expr %&% term ;WW?W1 & W# > J term > term term %$% factor ;WW?W1 $ W# > J factor > factor %/% expr %0% ;WW?W! > J DIQI9 > UU int main/0 ; yyparse/0> yyerror/char $s0 ; printf/Us,s0> 19. INPUT - OUTPUT
Wlex parser.l Wyacc Gd parser.y Wcc lex.yy.c y.tab.c Gll Glm W . a . o
u t
! & # (.3333
.6
E'PERIMENT(11
11.1 O)ECTI+E
onvert 9he 6-7 rules into 8acc form and write code to generate abstract syntax tree. 11.2 RESOURCE
linux using putty 11.3 PROGRAM
LOGIC
2eading an input file line by line. onvert it in to abstract syntax tree using three address code. 2epresent three address code in the form of Nuadruple tabular form. 11.4 PROCEDURE
Qo to terminal .Ppen vi editor ,+ex lex.l , cc lex.yy.c , .a.out 11.5 PROGRAM
:int.l; U; Rincludey.tab.h
RincludeHstdio.hB Rinclu deHstri ng.hB int +ineo?1> U identifier if return I7> else return C+SC> while return "_I+C> int J char J float return 984C> ;identifier ;strcpy/yylval.var,yytext0> return EA2> ;number ;strcpy/yylval.var,yytext0>
return -:L> H JB JB? JH? J?? ;strcpy/yylval.var,yytext0> return 2C+P4> < Tt= > Tn +ine-o&&> . return yytext<3=> UU Hint.<; U; RincludeHstring.hB RincludeH stdio.hB struct Nuad; ch ar op <(= > ch ar ar g1 <1 3=> ch ar ar g! <1 3=> ch ar res ult <1 3=> :AD<#3=> struct stac5; int ite ms <1 33 => int to p> st5> int Index?3,tIndex?3,St-o,Ind,tInd>
.7
extern int +ine-o> U Uunion; char var<13=> Uto5en HvarB -:L EA2 2C+P4 Uto5en LAI- I7 C+SC "_I+C 984C Utype HvarB CY42 ASSIQ-LC-9 P-DI9IP- I7S9 C+SCS9 "_I+C+PP4 Uleft %@% %&% Uleft %$% %% UU 42PQ2AL LAI- 6+PZ > 6+PZ %;% PDC %% > PDC 6+PZ J S9A9CLC-9 PDC J S9A9CLC-9 > S9A9CLC-9 DCS9 %>% J ASSIQ-LC-9 %>% J P-DS9 J "_I+CS9 > DCS9 984C EA2+IS9 > EA2+IS9 EA2 %,% EA2+IS9 J EA2 > ASSIQ-LC-9 EA2 %?% CY42; strcpy/:AD strcpy/:AD strcpy/:AD strcpy/:AD strcpy/WW,:AD > CY42 CY42 %&% CY42 ;Adduadruple/&,W1,W#,WW0> J CY42 %@% CY42 ;Adduadruple/@,W1,W#,WW0> J CY42 %$% CY42 ;Adduadruple/$,W1,W#,WW0> J CY42 %% CY42 ;Adduadruple/,W1,W#,WW0> J %@% CY42 ;Adduadruple/:LI-,W!,,WW0> J %/% CY42 %0% ;strcpy/WW,W!0> J EA2 J -:L > P-DS9 I7S9; Ind?pop/0> sprintf/:AD Ind?pop/0>
sprintf/:AD J I7S9 C+SCS9 > I7S9 I7 %/% P-DI9IP- %0% ; strcpy/:AD strcpy/:AD strcpy/:AD strcpy/:AD push/Index0> push/Index0>
/
Index&&> 6+PZ ; strcpy/:AD strcpy/:AD strcpy/:AD strcpy/:AD 10> push/Index0> Index&&> > C+SCS9 C+SC; t I n d ? p o p / 0 > I n d ? p o p / 0 > p u s h / t I n d 0 > sprintf/:AD ,Index0> 6 +
P Z ; Ind?pop/0> sprintf/:AD
> P-DI9IP- EA2 2C+P4 EA2 ;Adduadruple/W!,W1,W#,WW0> St-o?Index@1> J EA2 J -:L > "_I+CS9 "_I+C+PP4; Ind?pop/0> sprintf/:AD Ind?pop/0> sprintf/:AD > "_I+C+PP4 "_I+C %/% P-DI9IP- %0% ; strcpy/:AD strcpy/:AD strcpy/:AD strcpy/:AD push/Index0> push/Index0> Index&&> 6+PZ ; strcpy/:AD strcpy/:AD strcpy/:AD strcpy/:AD 10> push/Index0> Index&&> > UU extern 7I+C $yyin> int main/int argc,char $argv<=0 ; 7I+C $fp> i n t i
> i f / a r g c B 1 0 ; fp?fopen/ argv<1=,r 0> if/Kfp0 ; printf/Tn 7ile not found0> exit/30>
/1
yyin?fp> yyparse/0> printf/TnTnTtTt @@@@@@@@@@@@@@@@@@@@@@@@@@@@TnTtTt 4os Pperator Arg1 Arg! 2esult TnTtTt @@@@@@@@@@@@@@@@@@@@0> for/i?3>iHIndex>i&&0 ; printf/TnTtTt UdTt UsTt UsTt UsTt Us,i,:AD printf/TnTtTt @@@@@@@@@@@@@@@@@@@@@@@0> printf/TnTn0> return 3> void push/int data0; st5.top&&> if/st5.top? ?1330 ; printf/Tn Stac5 overflowTn0> exit/30> st5.items int pop/0 ; int dat a> if/s t5.t op ??@ 10; printf/Tn Stac5 underflowTn0> exit/30> data?st5.items return data> void Adduadruple/char op<(=,char arg1<13=,char arg!<13=,char result<13=0 ; strcpy/:AD strcpy/:AD strcpy/:AD
sprintf/:AD strcpy/result,:AD yyerror/0 ; printf/Tn Crror on line noUd,+ine-o0> Inp$t W v i t e s t . c m a i n / 0 ; i n t a , b , c > i f / a H b 0 ; a?a&b> w hi le /a H
b 0; a ? a & b> i f / a H ? b 0 ; c ? a @ b >
/,
else ; c?a&b>
11.7 PRE(LA)
11.8
,UESTIONS
1
"hat are the functions we use to construct a syntax treeX
2
"hat is Leta dataX
3
_ow list of identifiers are represented using 6-7 rulesX
4
"hat is three address codeX
5
"hat are the record structures we use to represent three address codeX
LA) ASSIGNMENT
1
"rite 8A for the des5top calculatorX
2
"rite 6-7 rules for the following grammarX CC&99 9 9$7 7 7/C 0id
11.9
POST(LA) ,UESTIONS
1.
"hat is Abstract Syntax treeX
2.
"hat are 6-7 2ulesX
3.
"hat is DAQ representationX
4.
_ow +A+2 /10 states are generatesX
5.
In which condition the user has to suppl y more information to 8AX
INPUT - OUTPUT
11.10
Wlex int.l Wyacc Gd int.y Wgcc lex.yy.c y.tab.c Gll GlmW.a.out test.c
OUTPUT
4os 9
Pperator H
Arg1 a
Arg! b
2esult t3
1 ! /
== ? == GOTO
t3 # t1
>ALSE 6
0 t1 0