compiler design lab moster copy
TRANSCRIPT
-
8/18/2019 Compiler Design Lab Moster Copy
1/63
COMPILER DESIGN LAB MOSTER COPY
EX. NO: 1 CONSTRUCTION OF NFA
AIM:
To construct NFA for t! "#$!n r!"u%&r !'(r!ss#on.
ALGORITHM:
1. G!t t! r!"u%&r !'(r!ss#on.). M&r* t! #n#t#&% st&t! +#t & %#n! &n, f#n&% st&t! +#t ,ou-%! c#rc%!.. M&r* t! #nt!r/!,#&t! st&t!s +#t & s#n"%! c#rc%! &n, & %#n! conn!ct#n" (r!$#ous st&t!&n, curr!nt st&t! +#t &n &rro+ to+&r,s t! curr!nt st&t!.0. D#s(%& t! #n(ut &-o$! t! &rro+ for !$!r tr&ns#t#on.2. In#t#&% st&t! /ust &$! no #nco/#n" st&t!s &n, f#n&% st&t! sou%, not &$! &nout"o#n" st&t!s.
3. T! #n(ut #s4 A REGULAR EXPRESSION 5r6 O7ER AN ALP8ABET 5E6.9. T! out(ut #s4 AN NFA 5N6 ACCEPTING Lr;.
-
8/18/2019 Compiler Design Lab Moster Copy
2/63
outt!'t'1
-
8/18/2019 Compiler Design Lab Moster Copy
3/63
outt!'t'4)4F; &rc1
-
8/18/2019 Compiler Design Lab Moster Copy
4/63
&12412; -124)2; c#rc%!1)24)41; outt!'t'1)4194!; outt!'t'
-
8/18/2019 Compiler Design Lab Moster Copy
5/63
RESULT:
Tus t! (ro"r&/ for Construct#n" NFA for t! "#$!n r!"u%&r !'(r!ss#on #s !'!cut!,succ!ssfu%%.
-
8/18/2019 Compiler Design Lab Moster Copy
6/63
EX.NO:) CONSTRUCTION OF MINIMIQED DFA
AIM:
To construct DFA for t! "#$!n r!"u%&r !'(r!ss#on.
ALGORITHM:
1. St&rt t! (roc!ss.). G!t t! r!"u%&r !'(r!ss#on.. M&r* t! #n#t#&% st&t! of t! s!t of st&t!s +#t &cc!(t#n" st&t!s &n, non &cc!(t#n"st&t!s.0. A((% t! (roc!,ur! to to (&rt#t#on construct & n!+ (&rt#t#on n!+.2. If n!+ 4 %!t f#n&% &n, cont#nu! +#t st!( 0 ot!r+#s! r!(!&t st!( ) +#t : n!+.3. !n t! f#n&% (&rt#t#on &s -!!n construct!,4 (#c* & r!(r!s!nt&t#$! for !&c"rou(..9. St&rt st&t! &n, &cc!(t#n" st&t! of t! /#n#/#!, DFA &r! t! "rou( contn#n" t!
st&rt st&t! &n, &cc!(t#n" st&t!s of t! or#"#n&% DFA.#nc%u,!?st,#o.@>#nc%u,!?con#o.@>#nc%u,!?str#n".@>#nc%u,!?(roc!ss.@$o#, /n; #nt t4#4 c&r cV1W4%V1W4rV1W c%rscr; (r#ntfn REGULAR EXPRESSION TO DFAn; (r#ntfnEnt!r t! r!"u%&r !'(r!ss#on : ; sc&nfKs4c; tstr%!nc; +#%!#?t; #fcV#W&HHcV#W-; %VWcV#W
!%s! #fcV#WJ; rV1WcV#1W %V1W
-
8/18/2019 Compiler Design Lab Moster Copy
7/63
!%s! #fcV#W; rVWcV#1W
# (r#ntfn TRANSITION TABLE FOR DFAn; (r#ntfnSTATES t & t-n; for# #? #; #frV#W&; #f%V#W&; (r#ntfn K,t K,4K,4#4#4#1; !%s! #f%V#W-;
(r#ntfn K,tK,4 K,4#4#4#1; !%s! (r#ntfn K,t K,4#4#; !%s! #frV#W-; #f%V#W-; (r#ntfn K,ttK,4 K,4#4#4#1; !%s! #f%V#W&; (r#ntfn K,t K,tK,4#4#14#; !%s! (r#ntfn K,ttK,4#4#; !%s! #f%V#W&; (r#ntfn K,t K,4#4#1;
!%s! #f%V#W-; (r#ntfn K,ttK,4#4#1; !%s! (r#ntfn K,t t 4#; "!tc;
-
8/18/2019 Compiler Design Lab Moster Copy
8/63
OUTPUT:
REGULAR EXPRESSION TO DFA
Ent!r t! r!"u%&r !'(r!ss#on : &-J-
TRANSITION TABLE FOR DFA
STATES & -
1 1 14 ) ) )
RESULT:
Tus t! (ro"r&/ for Construct#n" DFA for t! "#$!n r!"u%&r !'(r!ss#on #s !'!cut!,succ!ssfu%%
-
8/18/2019 Compiler Design Lab Moster Copy
9/63
EX.NO: IMPLEMENTATION OF LEXICAL ANALYSER USING LEXTOOL
AIM: To +r#t! & C (ro"r&/ to #/(%!/!nt %!'#c&% &n&%s#s us#n" LEX too%.
ALGORITHM:
1. St&rt t! (ro"r&/.). L!' (ro"r&/ cons#sts of tr!! (&rts.&. D!c%&r&t#on KK -. Tr&ns%&t#on ru%!s KKc. Au'#%&r (roc!,ur!.. T! ,!c%&r&t#on s!ct#on #nc%u,!s ,!c%&r&t#on of $&r#&-%!s4 /nt!st4 const&nts &n,r!"u%&r ,!f#n#t#ons.0. Tr&ns%&t#on ru%! of %!' (ro"r&/ &r! st&t!/!nts of t! for/&. P1 &ct#on
-. P) &ct#onc. ,. !. Pn &ct#on2. r#t! & (ro"r&/ #n t! $# !,#tor &n, s&$! #t +#t .% !'t!ns#on.3. Co/(#%! t! %!' (ro"r&/ +#t %!' co/(#%!r to (ro,uc! out(ut f#%! &s %!'..c. !" Z%!' f#%!n&/!.%
Z cc %!'..c %%9. Co/(#%! t&t f#%! +#t C co/(#%!r &n, $!r#f t! out(ut.
PROGRAM:KK#,!nt#f#!rV&AQWV&AQ=WJKK>.J (r#ntfnKs #s PREPROCESSOR DIRECTI7En4 t!'t;#nt Hf%o&t H,ou-%! Hc&r Hfor H
#f (r#ntfKs #s & *!+or,n4t!'t;#,!nt#f#!r (r#ntfnn FUNCTION CALLn Ks4t!'t; (r#ntfBLOC[ BEGINSn; (r#ntfBLOC[ ENDSn;#,!nt#f#!rVV=WJW;\ (r#ntfKs #s #,!nt#f#!rn4t!'t; (r#ntfKs #s & ASSIGNMENT OPERATORn4t!'t;
-
8/18/2019 Compiler Design Lab Moster Copy
10/63
V=W (r#ntfKs #s NUMBERn4t!'t;? H@ H H@ H
? (r#ntfKs #s & RELATIONAL OPERATORn4t!'t; EC8O(r#ntfn;; EC8O; (r#ntfn; H HJ (r#ntfKs #s & ARIT8METIC OPERATOR n; (r#ntfKs #s & INCREMENTAL OPERATORn; EC8O (r#ntfn;KK/n;
%!';#nt +r&(;r!turn 1s&/1.% 0)L4 92C +r#tt!n
-
8/18/2019 Compiler Design Lab Moster Copy
11/63
OUTPUT:
V%oc&%ost]s!r$!r ^WZ %!' s&/1.%V%oc&%ost ]s!r$!r ^WZ cc %!'..c %%
V%oc&%ost ]s!r$!r ^WZ. &. out && #s #,!nt#f#!r
>
> #s PREPROCESSOR DIRECTI7E
#nc%u,!#nc%u,! #s #,!nt#f#!r
?? #s & RELATIONAL OPERATOR
st,#ost,#o #s #,!nt#f#!r
RESULT:
Tus t! %!'#c&% &n&%s!r #s #/(%!/!nt!, us#n" %!' too%.
-
8/18/2019 Compiler Design Lab Moster Copy
12/63
EX.NO:0 IMPLEMENTATION OF SYMBOL TABLE
AIM:
To #/(%!/!nt s/-o% t&-%! us#n" C L&n"u&"!.
ALGORITHM:
1. O(!n t! f#%! (o#nt!r for t! #n(ut f#%!.). R!&, !&c str#n" &n, s!(&r&t! t! to*!n.. For #,!nt#f#!rs us! 5#,646*6 for *!+or,s &n,5s6 for s/-o%s.0. r#t! !&c str#n" #n t! &-o$! for/ to &n out(ut f#%!.2. For !&c #,!nt#f#!r r!&, /&*! &n !ntr #n s/-o% t&-%!.3. For !&c s/-o% /!nt#on #ts t(!4 $&%u!4 r!%oc&t&-%! %oc&t#on &n, s#!.9. If t!r! #s &n &ss#"n/!nt !'(r!ss#on u(,&t!s t! t&-%!.
PROGRAM:
>#nc%u,!?con#o.@>#nc%,!?str#n".@>#nc%u,!?ct(!.@>#nc%u,!?(roc!ss.@struct #,!nt#f#!r c&r n&/!V1W4t(!V1W4$&%u!V1W#nt s#!$o#, /n;FILE Jfstruct #,!nt#f#!r #,V1Wc&r c4 strV1=W4nu/V)W__#ntn#,4*4f%&"4%&st,&t&4I4o(!r1.#,f%&"44sV1W14)404
-
8/18/2019 Compiler Design Lab Moster Copy
13/63
+#%!cEOF;s+#tcc;c&s! 55:
(r#ntf`n OB_; -r!&*c&s!66: (r#ntf`n CB_; -r!&*c&s!646: (r#ntf`n PM1_; -r!&*c&s!66: (r#ntf`n PM)_; -r!&*
c&s!6:6: (r#ntf`n PM_; -r!&*c&s!6_6: (r#ntf`n PM0_; -r!&*c&s!6 6: (r#ntf` _; -r!&*c&s!66: (r#ntf`n OPA_; -r!&*c&s!6;6: (r#ntf`n CPA_; -r!&*c&s!66: (r#ntf`nOP1_; -r!&*
c&s!66: (r#ntf`nOP)_; -r!&*c&s!6J6: (r#ntf`nOP_; -r!&*c&s!66: (r#ntf`nOP0_; -r!&*c&s!6@6: (r#ntf`nOP2_;
-
8/18/2019 Compiler Design Lab Moster Copy
14/63
-r!&*c&s!6?6: (r#ntf`nOP3_; -r!&*c&s!66:
(r#ntf`nOP9_; -r!&*c&s!66: (r#ntf`nOP
-
8/18/2019 Compiler Design Lab Moster Copy
15/63
#fstrc/(str4_c&r_;;%&st,&t& (r#ntf`n *!1_;
!%s! #fstrc/(str4_#nt_;;%&st,&t&1 (r#ntf`n *!)_;!%s! #fstrc/(str4_f%o&t_;;%&st,&t&1 (r#ntf`n *!_;
!%s! #fstrc/(str4_,ou-%!_;;%&st,&t& (r#ntf`n *!0_;!%s!#,f%&"for##?n#,#;#fstrc/(str4#,!nV#W;; -r!&*#f#n#,1; (r#ntf`Foun, #n$&%#, #,!nt#f#!r_;!%s! (r#ntf`Ks_4#,V#W.n&/!;#fc66;strc(str4#,V#W.$&%u!;o(!r&n,)for?str%!nstr;;o(!r&n,)o(!&rn,)J1strVW0
-
8/18/2019 Compiler Design Lab Moster Copy
16/63
#fo(!r&t#on6J6;o(!r&n,1o(!r&n,1Jo(!r&n,)#fo(!r&t#on66;o(!r&n,1o(!r&n,1o(!r&n,)
+#%!o(!r&n,@;strVWo(!&rn,1K1;0
-
8/18/2019 Compiler Design Lab Moster Copy
17/63
!%s!,!sI
!%s!
f%&"1*+#%!c66;c646;c66;;strV*Wccf"!tcf;f%&"1
strV*W66strc(#,!nVn#,W4str;strc(str4_#,_;strc&tstr4nu/;nu/VWstrc(#,Vn#,W.n&/!4str;#,Vn#,W.s#!sV%&st,&t&W (r#ntf`Ks_4str;#fc66;#,f%&"%&st,&t&0*#fc66; (r#ntf`OP=_;cf"!tcf;#f#s,#"#tc;;+#%!c66;c646;c6 5;;StrV*Wccf"!tcf;strV*W66 (r#ntf`Ks_4str;strc(#,Vn#,W.$&%u!4str;
-
8/18/2019 Compiler Design Lab Moster Copy
18/63
n#, -r!&*
#ff%&"1;cf"!tcf;f%&" (r#ntf`n t SYMBOL TABLE nn NAME TYPE SIQE LOC 7ALUE n_;for##?n#,#; (r#ntf`KstKstK,_4#,!nV#W4#,V#W.t(!4#,V#W.s#!; (r#ntf`tK,_4%oc;%oc%oc#,V#W.s#!
(r#ntf`tKsn_4#,V#W.$&%u!;
-
8/18/2019 Compiler Design Lab Moster Copy
19/63
OUTPUT:
L!'#c&%.t't:Mn;
F%o&t '44X12'
OB *! #, PM1 #,1 PM1 #,) PM) #, OP= 1 PM) #,1 OP= 2 PM) #,) #, OP) #,1 PM) CB
SYMBOL TABLE
NAME TYPE SIQE LOC 7ALUE ' f%o&t 0 1 1 f%o&t 0 10 2 f%o&t 0 1< 2
RESULT:
Tus t! s/-o% t&-%! (ro"r&/ +&s !'!cut!, &n, $!r#f#!, succ!ssfu%%.
-
8/18/2019 Compiler Design Lab Moster Copy
20/63
EX.NO:2 CONSTRUCTION OF OPERATOR PRECEDENCE PARSE TABLE
AIM:
To #/(%!/!nt t! construct#on of o(!r&tor (r!c!,!nc! (&rs! t&-%! us#n" C L&n"u&"!.
ALGORITHM:
1. St&rt t! (roc!ss.). G!t t! r!"u%&r !'(r!ss#on.. G!t t! no of t!r/#n&%s #nc%u,#n" Z.0. For #,!nt#f#!r 5#6 &n, o(!r&tors 5646J6 &r! us!,.2. Ent!r t! t!r/#n&%s #n ro+ &n, co%u/n +#s! for !&c !ntr.3. For !&c #,!nt#f#!r r!&, /&*! &n !ntr #n o(!r&tor (r!c!,!nc! (&rs! t&-%!.9. For !&c s/-o% /!nt#on #ts t(!4 $&%u!4 r!%oc&t&-%! %oc&t#on &n, s#!.#nc%u,!?st,#o.@>#nc%u,!?con#o.@
$o#, /n ; #nt #44n4#-4to(4nu/V1W4*4r4t4%4'444"4%# c&r t&-V1WV1W4sV1W4st&c*V)W4#nt!rV1W4(ro,V1WV1W c%rscr ; (r#ntf`n Ent!r t! #n(ut !'(r!ss#on:n_; sc&nf`Ks_4s; (r#ntf`n Ent!r t! nu/-!r of t!r/#n&%s INC Z:n_; sc&nf`K,_4n; for##?n#; for?n; t&-V#WVW6 5 (r#ntf`n G#$! T! !ntr#!s #n f#rst ro+ &n, co%u/n corr!s(on,#n" to t!r/#n&%:n_;nu/VW66for#1#?n#; sc&nf`Ks_4t&-VWV#W; nu/V#Wt&-VWV#W
t&-V#WVWt&-VWV#W (r#ntf`n Ent!r t! (r!c!,!nc! t&-%! ro+ +#s!:n_;for#1#?n#; for1?n;
-
8/18/2019 Compiler Design Lab Moster Copy
21/63
sc&nf`Ks_4t&-V#WVW; for##?n#;
for?n; (r#ntf`Kct_4t&-V#WVW; (r#ntf`nn_;st&c*Vto(W 5Z6%str%!ns;sV%W6Z6
sV%1W66 (r#ntf`nSt&c* t In(ut -uff!r t Po((!, E%!/!nts:n_; (r#ntf`ZtKsn_4s;for##?%#; for*1*?n*; #fst&c*Vto(W nu/V*W; '* #fsV#W nu/V*W; * #ft&-V'WVW 56 HH t&-V'WVW 5?6; st&c*Vto(1WsV#W
to( #-
!%s! #ft&-V'WVW 5@6; #nt!rV"Wst&c*Vto(W " st&c*Vto(WsV#W #- forrr?to(r; (r#ntf`Kc_4st&c*VrW; (r#ntf`t_;
-
8/18/2019 Compiler Design Lab Moster Copy
22/63
fort#-t?%t; (r#ntf`Kc_4sVtW; (r#ntf`tt_; #ft&-V'WVW 5@6;
(r#ntf`Kc_4 #nt!rV"1W; (r#ntf`n_;to(+#%!st&c*Vto(W6Z6; #nt!rV"Wst&c*Vto(W " to( forrr?to(r;
(r#ntf`Kc_4st&c*VrW; (r#ntf`ttt_; (r#ntf`Kc_4#nt!rV"1W; (r#ntf`n_;"!tc ;
OUTPUT:
Ent!r t! #n(ut !'(r!ss#on: ##J#
Ent!r t! nu/-!r of t!r/#n&%s #nc%u,#n" Z: 0G#$! t! !ntr#!s #n f#rst ro+ &n, co%u/n corr!s(on,#n" to t!r/#n&%: # J Z
Ent!r t! (r!c!,!nc! t&-%! ro+ +#s!: @ @ @ ? @ ?
-
8/18/2019 Compiler Design Lab Moster Copy
23/63
@ ? @ @ @
? ? ?
O(!r&tor Pr!c!,!nc! T&-%!:
# J Z
# @ @ @
? @ ? @
J ? @ @ @
Z ? ? ?
STAC[ INPUT BUFFER POPPED ELEMENTS:
Z ##J#Z
Z# #J#Z
Z #J#Z #
Z# J#Z
ZJ #Z #
ZJ# Z
ZJ #
Z J
Z
-
8/18/2019 Compiler Design Lab Moster Copy
24/63
RESULT:
Tus t! o(!r&tor (r!c!,!nc! (&rs! t&-%! (ro"r&/ +&s !'!cut!, &n, $!r#f#!,succ!ssfu%%.
-
8/18/2019 Compiler Design Lab Moster Copy
25/63
EX. NO: 3 SYNTAX ANALYSIS USING YACC
AIM:
To #/(%!/!nt s#/(%! ,!s* c&%cu%&tor us#n" Y&cc too%.
ALGORITHM:
1. St&rt t! (ro"r&/.). Y&cc (ro"r&/ cons#sts of tr!! (&rts n&/!%&. D!c%&r&t#ons KK -. Tr&ns#t#on Ru%!KKc. Su((ort#n" C a rout#n!s.. D!c%&r&t#on (&rt cons#sts of t+o s!ct#ons4 f#rst s!ct#on contns on% #nc%u,!st&t!/!nts &n, t! s!con, st&t!/!nts contns ,!c%&r&t#on of t! "r&//&r to*!ns.
0. E&c ru%! #n s!t of tr&ns#t#on ru%!s cons#sts of "r&//&r (ro,uct#on &n, s!/&nt#c&ct#on. T! s!t of (ro,uct#ons &r! of t! for/&. ?%!ft s#,!@: ?&%t 1@ s!/&nt#c &ct#on 11. H ?&%t )@ s!/&nt#c &ct#on )). ... H ?&%t n@ s!/&nt#c &ct#on n0. 2. In t! t#r, (&rt4 !rror r!co$!r rout#n!s &r! &,,!,.3. T! (ro"r&/ #s t(!, us#n" $# !,#tor4 &n, s&$!, +#t . !'t!ns#on.9. It #s f#rst co/(#%!, +#t t! &cc co/(#%!r to (ro,uc! t! C co,! for C co/(#%!r&cc s&/(.;.
-
8/18/2019 Compiler Design Lab Moster Copy
26/63
%#n!s : %#n!s !'(r n (r#ntfK%f4Z); H %#n!s n H
!'(r : !'(r !'(r ZZZ1ZH !'(r !'(r ZZZ1ZH !'(r J !'(r ZZZ1JZH !'(r !'(r #fZ; ZZZ1Z!%s! !rror,#$#,!nt sou%, -! & (os#t#$! no.n;H !'(r !'(r #nt #4su/1 for#1#?Z#; su/ J Z1 ZZsu/
H !'(r ; ZZZ)H !'(r K(r!c UMINUS ZZ Z)H NUMBER KK%!';#nt c+#%!c"!tc&r;; ;#fc.;HH#s,#"#tc;;;un"!tcc4st,#n;sc&nfK%f4%$& %;r!turn NUMBERr!turn c
-
8/18/2019 Compiler Design Lab Moster Copy
27/63
OUTPUT:
V%oc&%ost]s!r$!r ^WZ &cc s&/(.V%oc&%ost]s!r$!r ^WZ cc .t&-.c %
V%oc&%ost]s!r$!r ^.&.out==.3=.)21)1.
RESULT:
Tus & c&%cu%&tor #s #/(%!/!nt!, us#n" YACC too%.
-
8/18/2019 Compiler Design Lab Moster Copy
28/63
EX.NO:9 IMPLEMENTATION OF S8IFT REDUCE PARSING ALGORIT8M
AIM:
To +r#t! & C (ro"r&/ to #/(%!/!nt t! s#ft r!,uc! (&rs#n".
ALGORITHM:
1. St&rt t! (ro"r&/.). G!t t! #n(ut str#n" fro/ t! us!r.. Pus Z onto to( of t! st&c*.0. S!t #( to (o#nt to t! f#rst #n(ut s/-o%.2. If t!r! #s &n (ro,uct#on +#c c&n -! us!, to r!,uc! t! #n(ut s/-o% r!,uc! t!str#n" ot!r+#s! (us #t to t! to( of t! st&c*.3. S!t #( to (o#nt to n!'t #n(ut s/-o%.9. R!(!&t t! &-o$! st!(s unt#% t! to( of t! st&c* contns t! Z &n, t! st&rt#n"s/-o%. If so4 t!n t! str#n" #s $&%#,4 ot!r+#s! t! str#n" #s #n$&%#,4 r!turn &n !rror
/!ss&"!.
-
8/18/2019 Compiler Design Lab Moster Copy
29/63
*%str%!n#(.r;for?*;#f%;
ss#fts4%;%str%!n#(.r; sr!,uc!s4n; #f%str%!nst.r;1;st.rVW(rVW.%;; (r#ntftt&cc!(t!,;"!tc;!'#t;
!%s! #f%str%!nst.r;@);HHst.rVW(rVW.%;;(r#ntftt!rror;!'#t;"!tc;#nt s#ft#nt s4 #nt %;#nt #st.rVsW#(.rVWs%for##?%1#;#f#(.rV#1W;#(.rV#W#(.rV#1W
!%s! #f#(.rV#1W;#(.rV#W (r#ntftts#ftntZKsttKsZ4st.r4#(.r;r!turns;#nt r!,uc!#nt s4 #nt n;#nt &4-4c4#c&r cfor##?n#;cstr%!n(rV#W.r;
-
8/18/2019 Compiler Design Lab Moster Copy
30/63
cst.rVs1W#f(rV#W.rVWc;#s%o+!rc;;st.rVs1W(rV#W.% (r#ntfttr!,uc!ntZKsttKsZ4st.r4#(.r;
for##?n#;&str%!nst.r; -str%!n(rV#W.r;#f&-strc/(st.r4(rV#W.r;;;st.rVs&W(rV#W.%forcc?sc;st.rVc1Wss&;1
(r#ntfttr!,uc!ntZKsttKsZ4st.r4#(.r;r!turns;
-
8/18/2019 Compiler Design Lab Moster Copy
31/63
OUTPUT:
!nt!r t! no of (ro,uct#on
!nt!r t! (ro,uct#onE@EEE@EJEE@#!nt!r t! #n(ut ##J#
st&c* #n(ut &ct#on
Z ##J#Z s#ft Z# #J#Z r!,uc! ZE #J#Z s#ft ZE #J#Z s#ft
ZE# J#Z r!,uc! ZEE J#Z r!,uc! ZE J#Z s#ft ZEJ #Z s#ft ZEJ# Z r!,uc! ZEJE Z r!,uc! ZE Z &cc!(t!,
-
8/18/2019 Compiler Design Lab Moster Copy
32/63
RESULT:
Tus t! S#ft R!,uc! P&rs#n" A%"or#t/ +&s #/(%!/!nt!, &n, $!r#f#!, succ!ssfu%%.
-
8/18/2019 Compiler Design Lab Moster Copy
33/63
EX.NO:< CONSTRUCTION OF LR PARSING TABLE
AIM:
To +r#t! & C (ro"r&/ to #/(%!/!nt s#/(%! LR P&rs#n" &%"or#t/.
ALGORITHM:
b In(ut: An #n(ut str#n" + &n, &n LR (&rs#n" t&-%! +#t funct#ons &ct#on &n, "oto for &"r&//&r G.b Out(ut: If + #s #n LG;4 & -otto/ a u( (&rs! for + ot!r+#s! &n !rror #n,#c&t#on.b M!to,: In#t#&%%4 t! (&rs!r &s s on #ts st&c*4 s #s t! #n#t#&% st&t!4 &n, +Z #n t!#n(ut -uff!r. T! (&rs!r t!n !'!cut!s t! (ro"r&/ unt#% &cc!(t or !rror &ct#on #s!ncount!r!,. s!t #( to (o#nt to t! f#rst s/-o% of +Z r!(!&t for!$!r -!"#n
%!t s -! t! st&t! on t! to( of t! st&c* &n, & t! s/-o% (o#nt!, to - #(
t!n -!"#n′ #f &ct#on Vs4 &W s#ft s
on t! to( of t! st&c*′ (us & t!n s
&,$&nc! #( to t! n!'t #n(ut s/-o% !n,
t!n -!"#nβ → !%s! #f &ct#on Vs4 &W r!,uc! A
H s/-o%s off t! st&c*β (o( )JH
-! t! st&t! no+ on to( of t! st&c*′ %!t s
4 AW on to( of t! st&c*′ (us A t!n "oto Vs
β → out(ut t! (ro,uct#on A
!n, !%s! #f &ct#on Vs4 &W &cc!(t t!n r!turn !%s! !rror; !n,
PROGRAM:
>#nc%u,! ?#ostr!&/.@>#nc%u,! ?con#o.@>#nc%u,! ?str#n".@>,!f#n! MP, ) J M&' no of Pro,uct#ons J>,!f#n! Mt ) J M&' no of t!r/#n&% s/-o%s J
>,!f#n! MNt 1 J M&' no of Nont!r/#n&% s/-o%s J>,!f#n! MSt J M&' no of St&t!s Jc%&ss LRIt!/ (u-%#c: c&r It!/VMP,WVW
-
8/18/2019 Compiler Design Lab Moster Copy
34/63
-
8/18/2019 Compiler Design Lab Moster Copy
35/63
#n%#n! #nt IsC&( c&r c; #f c @ A c ? Q; r!turn 1 r!turn
$o#, ToStr#n" c&r cVW4 #nt #; #f # ? 1; cVW c&r;# ; cV1W !%s!
cVW c&r;# 1; ; cV1W c&r;# K 1; ; cV)W
$o#, A,,s c&r s1VW4 c&r s)VW; for #nt # s)V#W #; for #nt s1VW ; #f s1VW s)V#W; -r!&* #f s1VW ; s1VW s)V#W s1VW $o#, A,, c&r s1VW4 c&r c; c&r sV)W sVW c sV1W A,,s s14 s;#nt G!tT!r c&r c; for #nt # T!rV#W T!rV#W c #; #f T!rV#W ; r!turn 1 r!turn #
-
8/18/2019 Compiler Design Lab Moster Copy
36/63
#nt G!tNT!r c&r c; for #nt # NT!rV#W NT!rV#W c #; #f NT!rV#W ; r!turn 1 r!turn #
$o#, C%osur! LRIt!/ JIt; c&r sVMNtW c&r s1VW #nt #4 4 * for ?It @ NIt ; for # It @ It!/VWV#W #; #f It @ It!/VWV#W .; A,, s4 It @ It!/VWV# 1W;
for # sV#W #; for ?NP, ; #f Pr,VWVW sV#W; for * *? *; s1V*W Pr,VWV*W s1VW . #f G!tNT!r Pr,VWVW; 1; A,, s4 Pr,VWVW; for * Pr,VWV*W *; s1V* 1W Pr,VWV*W s1V*1W It @ A,, s1; $o#, ConstCo%% ; c&r sV)W4 S/VMNt MtW4 c strc( S/4 NT!r; strc&t S/4 T!r; for #nt # #? #; sV#W Pr,VWV#W sVW . sV0W Pr,VWVW sV2W S!tVW.NIt S!tVW.A,, s; C%osur! S!tVW;
-
8/18/2019 Compiler Design Lab Moster Copy
37/63
NSt 1 for # #?NSt #; for #nt ?S/VW ;
S!tVNStW.NIt for #nt * *?S!tV#W.NIt *; for #nt % S!tV#W.It!/V*WV%W . %; sV%W S!tV#W.It!/V*WV%W #f S!tV#W.It!/V*WV% 1W S/VW; sV%W S!tV#W.It!/V*WV% 1W sV%W . %
for S!tV#W.It!/V*WV%W %; sV%W S!tV#W.It!/V*WV%W sV%W S!tVNStW.A,, s; #f S!tVNStW.NIt @ ; C%osur! S!tVNStW; #nt Fn, 1 for * *?NSt *; #f S!tV*W S!tVNStW; Fn, * #f Fn, 1; Fn, NSt #nt n1 G!tNT!r S/VW; #nt n) G!tT!r S/VW; #f n1 1; S!tV#W.GotoVn1W Fn, !%s! #f n) 1; S!tV#W.GotoVMNt n)W Fn, $o#, In#t ; for #nt # #?MSt #; for #nt ?MNt ; A'nT-%V#WVWVW for ?Mt ; GotoT-%V#WVWVW
-
8/18/2019 Compiler Design Lab Moster Copy
38/63
for # #?MNt #; Fo%%o+V#WVW $o#, F#n,Fo%%o+ ;
#nt n1 A,, T!r4 Z; strc( Fo%%o+VW4 Z; for #nt Cnt NT!rVCntW Cnt; for #nt # #?NP, #; #nt n1 for #nt Pr,V#WVW ;
#f n1 G!tNT!r Pr,V#WVW;; 1; A,, Fo%%o+Vn1W4 Pr,V#WV1W; #f n1 G!tNT!r Pr,V#WV 1W;; 1; A,,s Fo%%o+Vn1W4 Fo%%o+VG!tNT!r Pr,V#WVW;W; for #nt # NT!rV#W #; c&r sVW for #nt Fo%%o+V#WVW ; #f G!tT!r Fo%%o+V#WVW; 1; A,, s4 Fo%%o+V#WVW; strc( Fo%%o+V#W4 s;$o#, ConstT&-%! ; c&r sVW4 s1V2W4 s)V2W #nt n for #nt # #?NSt #; for #nt ?S!tV#W.NIt ; for #nt * S!tV#W.It!/VWV*W . *; * #f S!tV#W.It!/VWV*W ; strc( s4 S!tV#W.It!/VW; sVstr%!n S!tV#W.It!/VW; 1W for #nt % %?NP, %;
-
8/18/2019 Compiler Design Lab Moster Copy
39/63
#f strc/( s4 Pr,V%W; ; #f % ;
strc( A'nT-%V#WVG!tT!r Z;W4 &cc; -r!&* n G!tNT!r sVW; for #nt / Fo%%o+VnWV/W /; strc( s14 r; ToStr#n" s)4 %; strc&t s14 s); strc( A'nT-%V#WVG!tT!r Fo%%o+VnWV/W;W4 s1;
!%s! #f n G!tT!r S!tV#W.It!/VWV*W;; 1; strc( s14 s; ToStr#n" s)4 S!tV#W.GotoVMNt nW; strc&t s14 s); strc( A'nT-%V#WVnW4 s1; !%s! #f n G!tNT!r S!tV#W.It!/VWV*W;; 1; ToStr#n" GotoT-%V#WVnW4 S!tV#W.GotoVnW; $o#, Pr#nts c&r sVW; cout ?? s for #nt # str%!ns; #?2 #; cout ?? $o#, /n ; c%rscr ; c&r sVWcout ?? Ent!r t! &u"/!nt!, "r&//&r (ro,uct#ons fo%%o+!, - !n,n +#%! 1; cout ?? ?? NP, ?? ; c#n @@ s #f strc/( s4 !n,;; -r!&* A,, NT!r4 sVW; strc( Pr,VNP,W4 s;
-
8/18/2019 Compiler Design Lab Moster Copy
40/63
A,, NT!r4 Pr,VNP,WVW; for #nt # Pr,VNP,WV#W #; #f IsC&( Pr,VNP,WV#W;; A,, T!r4 Pr,VNP,WV#W; NP,
In#t ; ConstCo%% ; F#n,Fo%%o+ ; ConstT&-%! ; c%rscr ; cout ?? ttSLR P&rs#n" T&-%!n cout ?? nt Act#on for #nt # T!rV#W #; cout ?? cout ?? Gotonnt for # T!rV#W #; cout ?? T!rV#W ??
cout ?? t for #1 NT!rV#W #; cout ?? NT!rV#W ?? cout ?? n for # #?NSt #; cout ?? n ?? # ?? t for #nt T!rVW ; Pr#nts A'nT-%V#WVW; cout ?? t for 1 NT!rVW ; Pr#nts GotoT-%V#WVW; "!tc ;
OUTPUT:
Ent!r t! &u"/!nt!, "r&//&r (ro,uct#ons fo%%o+!, - !n,; P@E1; E@ET); E@T; T@TJF0; T@F2; F@E;3; F@#9; !n, SLR P&rs#n" T&-%! Act#on Goto J ; # Z E T F
-
8/18/2019 Compiler Design Lab Moster Copy
41/63
s0 s2 1 ) 1 s3 &cc) r) s9 r) r) r0 r0 r0 r0
0 s0 s2 < ) 2 r3 r3 r3 r33 s0 s2 = 9 s0 s2 1< s3 s11= r1 s9 r1 r11 r r r r11 r2 r2 r2 r2
RESULT: Tus t! (ro"r&/ for Construct#n" LR P&rs#n" T&-%! for t! "#$!n "r&//&r #s!'!cut!, succ!ssfu%%
EX.NO:= IMPLEMENTATION OF INTERMEDIATE CODEGENERATION
AIM:
To +r#t! & C (ro"r&/ to #/(%!/!nt t! #nt!r/!,#&t! co,! for t! "#$!n s!t of #n(ut!'(r!ss#ons.
ALGORIT8M:1. St&rt t! (ro"r&/.). G!t t! #n(ut !'(r!ss#on fro/ t! us!r.. C!c* t! !'(r!ss#ons for #ts $&%#,&t#on.0. If #t #s #n$&%#, r!turn t! !rror /!ss&"!.2. Ot!r+#s!4 for !&c co/(ut&t#on stor! t! r!su%t #n t! tr!! a &,,r!ss st&t!/!nt
-
8/18/2019 Compiler Design Lab Moster Copy
42/63
stor! #t #n t!/(or&r $&r#&-%! s& t14 t)4 !tc.4; .3. Ass#"n t! f#n&% t!/(or&r $&%u! to t! $&r#&-%! #n +#c t! r!su%t &s to -! stor!,.9. Sto( t! (ro"r&/. PROGRAM:
>#nc%u,!?st,#o.@>#nc%u,!?ct(!.@>#nc%u,!?st,%#-.@>#nc%u,!?con#o.@>#nc%u,!?str#n".@$o#, s/&%%;$o#, ,o$!#nt ;#nt (V2W414)4404c14#4*4%4/4(#c&r s+V2W4444J4V)W4&V2W4-V2W4cV)W$o#, /n;
c%rscr; (r#ntfEnt!r t! !'(r!ss#on:; sc&nfKs4; (r#ntfnntT! Int!r/!,#&t! co,! #s:n; s/&%%;$o#, ,o$!#nt #; &VW-VW I f#s,#"#tV#)W; #s,#"#tV#)W;; &VWV#1W -VWV#1W #f#s,#"#tV#)W;; &VWV#1W -VWt -V1WV#)W #f#s,#"#tV#)W;; -VWV#1W &VWt &V1WV#)W -V1W #f#s,#"#tV#)W; #s,#"#tV#)W;; &VWt -VWt &V1WV#)W
-
8/18/2019 Compiler Design Lab Moster Copy
43/63
-V1WV#)W #to&c4c41; V#)WV#)WcVW #fV#WJ; (r#ntfttK,KsJKsn4c4&4-;
#fV#W; (r#ntfttK,KsKsn4c4&4-; #fV#W; (r#ntfttK,KsKsn4c4&4-; #fV#W; (r#ntfttK,KsKsn4c4&4-; #fV#W; (r#ntftKctK,4V#1W4c; #to&c4c41; V#WcVW c
s/&%%;
$o#, s/&%%; (#% for##?str%!n;#; for//?2/; #fV#Ws+V/W; #f(#?(V/W; (#(V/W %1 *# #f%1; ,o$!*; !%s! "!tc; !'#t ;
-
8/18/2019 Compiler Design Lab Moster Copy
44/63
OUTPUT:
Ent!r t! !'(r!ss#on:&-cJ,!f
T! Int!r/!,#&t! co,! #s: t1cJ, t)!f t-t1 t0t1t) &t0
-
8/18/2019 Compiler Design Lab Moster Copy
45/63
RESULT:
Tus t! front !n, of & co/(#%!r #s #/(%!/!nt!, - "!n!r&t#n" &,,r!ss co,!s &s #n(utfor t! r!"u%&r !'(r!ss#on
EX.NO:1 IMPLEMENTATION OF CODE OPTIMIQATION TEC8NIdUES
AIM:
To +r#t! & C (ro"r&/ to #/(%!/!nt t! co,! "!n!r&t#on &%"or#t/.
ALGORIT8M: T! co,! "!n!r&t#on &%"or#t/ t&*!s &s #n(ut & s!eu!nc! of tr!! a &,,r!ss st&t!/!ntsconst#tut#n" & -&s#c -%oc*. For !&c tr!! a &,,r!ss st&t!/!nt of t! for/ ' : o( +! (!rfor/ t! fo%%o+#n" &ct#ons: 1. In$o*! & funct#on "!tr!" to ,!t!r/#n! t! %oc&t#on L +!r! t! r!su%t of t!co/(ut&t#on o( sou%, -! stor!,. L +#%% usu&%% -! & r!"#st!r4 -ut #t cou%, &%so -! &/!/or %oc&t#on. ! s&%% ,!scr#-! "!tr!" sort%.
4 L to (%&c! & co( of #n L.′ #f t! $&%u! of #s curr!nt% -ot #n /!/or &n, & r!"#st!r.If t! $&%u! of #s not &%r!&, #n L4 "!n!r&t! t! #nstruct#on MO7 ′4 on! of; t! curr!nt
%oc&t#ons; of . (r!f!r t! r!"#st!r for ′). Consu%t t! &,,r!ss ,!scr#(tor for to
,!t!r/#n! #s & curr!nt %oc&t#on of . A"n4 (r!f!r & r!"#st!r to & /!/or %oc&t#on #f #s #n -ot.U(,&t! t! &,,r!ss ,!scr#(tor of ' to #n,#c&t! t&t ' #s #n %oc&t#on L. If L #s & r!"#st!r4u(,&t! #ts ,!scr#(tor to #n,#c&t! t&t #t contns t! $&%u! of '4 &n, r!/o$! ' fro/ &%%
ot!r r!"#st!r ,!scr#(tors.′4 L +!r! ′. G!n!r&t! t! #nstruct#on OP
0. If t! curr!nt $&%u!s of &n,or &$! no n!'t us!rs4 &r! not %#$! on !'#t fro/ t! -%oc*4 &n, &r! #n r!"#st!r ,!scr#(tor to #n,#c&t! t&t4 &ft!r !'!cut#on of ' : o( 4 tos!r!"#st!rs no %on"!r +#%% contn &n,or 4 r!s(!ct#$!%.
PROGRAM:>#nc%u,!?st,#o.@>#nc%u,!?con#o.@>#nc%u,!?str#n".@struct o(c&r %
-
8/18/2019 Compiler Design Lab Moster Copy
46/63
c&r rV)Wo(V1W4(rV1W
$o#, /n;
#nt &4#4*44n44/4e
c&r J(4J%c&r t!/(4tc&r Jt!/c%rscr; (r#ntf!nt!r no of $&%u!s;sc&nfK,4n;for##?n#; (r#ntf%!ftt;
o(V#W.%"!tc!; (r#ntfr#"t:t;sc&nfKs4o(V#W.r; (r#ntf#nt!r/!,#&t! Co,!n; for##?n#; (r#ntfKc4o(V#W.%; (r#ntfKsn4o(V#W.r;for##?n1#;t!/(o(V#W.%for?n; (strcro(VW.r4t!/(;#f(; (rVW.%o(V#W.%strc((rVW.r4o(V#W.r;
(rVW.%o(Vn1W.%strc((rVW.r4o(Vn1W.r; (r#ntfn&ft!r ,!&, co,! !%#/#n&t#onn;for**?*;
-
8/18/2019 Compiler Design Lab Moster Copy
47/63
(r#ntfKct4(rV*W.%; (r#ntfKsn4(rV*W.r;
su- !'(r!ss#on !%#/#n&t#on
for//?/;t!/(rV/W.rfor/1?; (strstrt!/4(rVW.r;#f(;t(rVW.% (rVW.%(rV/W.% for##?#;
%strcr(rV#W.r4t; #f%;&%(rV#W.r(r#ntf(os: K,4&; (rV#W.rV&W(rV/W.% (r#ntf!%#/#n&t! co//on !'(r!ss#onn;for##?#; (r#ntfKct4(rV#W.%; (r#ntfKsn4(rV#W.r; ,u(%#c&t! (ro,uct#on !%#/#n&t#on
for##?#;for#1?;estrc/((rV#W.r4(rVW.r;#f(rV#W.%(rVW.%;e;
(rV#W.% strc((rV#W.r4; (r#ntfo(t#/#!, co,!;for##?#;
-
8/18/2019 Compiler Design Lab Moster Copy
48/63
#f(rV#W.%; (r#ntfKc4(rV#W.%; (r#ntfKsn4(rV#W.r;
"!tc;
OUTPUT:
!nt!r no of $&%u!s 2%!ft &r#"t: =%!ft -r#"t: c,%!ft !r#"t: c,%!ft fr#"t: -!%!ft rr#"t: f #nt!r/!,#&t! Co,!&= -c,!c,
-
8/18/2019 Compiler Design Lab Moster Copy
49/63
f-!rf
&ft!r ,!&, co,! !%#/#n&t#on - c,
! c,f -!r f !%#/#n&t! co//on !'(r!ss#on - c, - c,f --r f o(t#/#!, co,!-c,f--rf
RESULT:
Tus t! &-o$! (ro"r&/ #s co/(#%!, &n, !'!cut!, succ!ssfu%% &n, out(ut #s $!r#f#!,.
EX.NO:11 CON7ERSION OF INFIX TO POSTFIX EXPRESSION
AIM:
To +r#t! & C (ro"r&/ to #/(%!/!nt t! (ostf#' !'(r!ss#on for t! "#$!n #nf#'!'(r!ss#on
ALGORIT8M:
1. Sc&n t! #n(ut fro/ %!ft to r#"t.). In#t#&%#s! &n !/(t st&c*.
-
8/18/2019 Compiler Design Lab Moster Copy
50/63
. If t! sc&nnn!, c&r&ct!r #s &n o(!r&n,4 &,, #t to t! Postf#' str#n". If t! sc&nn!,c&r&ct!r #s &n o(!r&tor &n, #f t! st&c* #s !/(t Pus t! c&r&ct!r tost&c*. If t! sc&nn!, c&r&ct!r #s &n O(!r&n, &n, t! st&c* #s not !/(t4 co/(&r! t!
(r!c!,!nc! of t! c&r&ct!r +#t t! !%!/!nt on to( of t! st&c*to(St&c*;.¬
If to(St&c* &s #"!r (r!c!,!nc! o$!r t! sc&nn!, c&r&ct!r Po(t! st&c* !%s! Pus
t! sc&nn!, c&r&ct!r ¬ to st&c*. R!(!&t t#s st!( &s %on" &s st&c* #s not !/(t &n,to(St&c* &s (r!c!,!nc! o$!r t! c&r&ct!r.
R!(!&t t#s st!( t#%% &%% t! c&r&ct!rs &r! sc&nn!,.¬
0. Aft!r &%% c&r&ct!rs &r! sc&nn!,4 +! &$! to &,, &n c&r&ct!r t&t t! st&c*/&&$! to t! Postf#' str#n".; If st&c* #s not !/(t &,, to(St&c* to Postf#' str#n"&n, Po( t!st&c*. R!(!&t t#s st!( &s %on" &s st&c* #s not !/(t.2. R!turn t! Postf#' str#n".PROGRAM:>#nc%u,!?st,#o.@>#nc%u,!?con#o.@>#nc%u,!?str#n".@
#nt ((4 s( 14 #(4 #c&r (ostV
-
8/18/2019 Compiler Design Lab Moster Copy
51/63
(r#ntf`nnnt Inf#': Ksnnt Postf#': Ksnn_4 #n4 (ost; "!tc ;
#nt o(!r&t#onc&r c;
for##?2#; #fc o(!rV#WVW; r!turn0
-
8/18/2019 Compiler Design Lab Moster Copy
52/63
RESULT:
Tus t! #nf#' to (ostf#' !'(r!ss#on #s con$!rt!, !'!cut!, succ!ssfu%% &n, out(ut #s$!r#f#!,.
EX.NO:1) IMPLEMENTATION OF dUADRAPLES
AIM:
To +r#t! & C (ro"r&/ to ,!r#$! eu&,r&(%!s for t! "#$!n !'(r!ss#on
ALGORIT8M:
1. R!&, t! "#$!n #n(ut fro/ r#"t to %!ft). B&s#c&%% eu&,ru(%!s &r! t! r!cor, structur! +#t four f#!%,s c&%%!, o(4&r"14&r")4 &n,r!su%t.. Un&r o(!r&tors ,o!s not us! &r").Con,#t#on&% uncon,#t#on&% u/( (ut t&r"!t %&-!%#n r!su%t.0. P&rt#t#on t! #n(ut #nto s/-o%s4o(!r&tors4*!+or,s &n, #,!nt#f#!rs &s "#$!n #n t!#n(ut.2. If so4t! t!/(or&r n&/!s /ust -! !nt!r!, #nto t! s/-o% t&-%! &s t! &r! cr!&t!,3. F#n&%%4t! eu&,ru(%!s for t! "#$!n #n(ut #s "!n!r&t!,.
PROGRAM:>#nc%u,!?st,#o.@>#nc%u,!?con#o.@>#nc%u,!?ct(!.@c&r st&c*V)2W#nt to(c&r (ostf#'!'(V)2W$o#, eu&,ru(%!s ; c&r est&c*V)2W #nt to(4#4t1 c&r c +#%!c(ostf#'!'(V#W;66; #f#s&%(&c;;
-
8/18/2019 Compiler Design Lab Moster Copy
53/63
to( est&c*Vto(Wc !%s!
#fc 6^5; #f#s&%(&est&c*Vto(W;; (r#ntf`nKc tKc ttK,_4c4est&c*Vto(W4t; est&c*Vto(Wt !%s! (r#ntf`nKc_4c;
#f#s&%(&est&c*Vto(W;; (r#ntf`tKc_4 est&c*Vto(W; !%s! (r#ntf`ttK,_4est&c*Vto(W; #f#s&%(&est&c*Vto(1W;; (r#ntf`tKc_4est&c*Vto(1W; !%s! (r#ntf`ttK,_4est&c*Vto(1; (r#ntf`ttK,_4t;to( est&c*Vto(Wt t # $o#, /n ; c%rscr ; (r#ntf`ENTER T8E EXPRESSION IN POSTFIX FORM:_; sc&nf`Ks_4(ostf#'!'(; eu&,ru(%!s ; "!tc ;
OUTPUT:
-
8/18/2019 Compiler Design Lab Moster Copy
54/63
ENTER T8E EXPERESSION IN POSTFIX FORM: &-c,J
- & t1
, c t) J t) t1 t
RESULT:
Tus t! #/(%!/!nt&t#on of eu&,r&(%!s #s co/(#%!, &n, !'!cut!, succ!ssfu%% &n, out(ut#s $!r#f#!,.
EX.NO:1 IMPLEMENTATION OF TRIPLES
AIM:
To +r#t! & C (ro"r&/ to ,!r#$! tr#(%!s for t! "#$!n !'(r!ss#on
-
8/18/2019 Compiler Design Lab Moster Copy
55/63
ALGORIT8M:1. St&rt t! Pro"r&/ ). R!&, t! #n(ut !'(r!ss#on fro/ r#"t to %!ft.. A$o#, !nt!r#n" t!/(or&r n&/!s #nto t! s/-o% t&-%!4r!f!r to & t!/(or&r $&%u! -
t! (os#t#on of t! st&t!/!nt t&t co/(ut!s #t.0. T!n t! Tr!! &,,r!ss st&t!/!nts c&n -! r!(r!s!nt!, - r!cor,s +#t on% tr!!f#!%,s :o(4&r"14&r").2. #!.;T! st&t!/!nt #s !nco,!, #n t! tr#(%! r!(r!s!nt&t#on - (%&c#n" t! #n(ut #n t!s(!c#f#!, co%u/s.3. S&/! &s eu&,ru(%!s !'c!(t t&t r!su%t t&t occu(#!s !'tr& on! co%u/n.9. F#n&%% t! "#$!n st&t!/!nts #s (r#nt!, &s & tr#(%! r!(r!s!nt&t#on for/.
PROGRAM:>#nc%u,!?st,#o.@>#nc%u,!?con#o.@
>#nc%u,!?ct(!.@c&r st&c*V)2W#nt to(c&r (ostf#'!'(V)2W$o#, tr#(%!s ; c&r est&c*V)2W #nt to(4#4t c&r c +#%!c(ostf#'!'(V#W;66; #f#s&%nu/c;; to( est&c*Vto(Wc !%s! #fc 65; #f#s&%(&est&c*Vto(W;; (r#ntf`nK, tKc tKc_4t4c4est&c*Vto(W; !%s! (r#ntf`nK, tKc tK,;_4t4c4est&c*Vto(W; !%s! (r#ntf`nK, tKc_4t4c; #f#s&%(&est&c*Vto(1W;;
-
8/18/2019 Compiler Design Lab Moster Copy
56/63
(r#ntf`tKc_4 est&c*Vto(1W; !%s! (r#ntf`tK,;_4est&c*Vto(1W; #f#s&%(&est&c*Vto(W;; (r#ntf`tKc_4est&c*Vto(W;
!%s! (r#ntf`tK,;_4est&c*Vto(; to( est&c*Vto(Wt t # $o#, /n ;
c%rscr ; (r#ntf`ENTER T8E EXPRESSION IN POSTFIX FORM:_; sc&nf`Ks_4(ost,#'!'(; tr#(%!s ; "!tc ;
OUTPUT:
ENTER T8E EXPRESSION IN POSTFIX FORM: &-c,J & - 1 c , ) J ; 1;
-
8/18/2019 Compiler Design Lab Moster Copy
57/63
RESULT:
Tus t! #/(%!/!nt&t#on of tr#(%!s #s co/(#%!, &n, !'!cut!, succ!ssfu%% &n, out(ut #s
$!r#f#!,.
.
EX.NO:10 GENERATION OF TO[ENS FOR GI7EN LEXEME
AIM:
To +r#t! & C (ro"r&/ to "!n!r&t! to*!ns for t! "#$!n !'(r!ss#on
ALGORIT8M:1. St&rt t! Pro"r&/ ). R!&, t! #n(ut fro/ r#"t to %!ft. F#rst #,!nt#f t! c&r&ct!rs #n,#$#,u&%%4t&t #t &s & co%%!ct#$! /!&n#n".0. To*!ns not#n" -ut & str#n" of ,!scr#(tors2. Aft!r "#$#n" t! !'(r!ss#on #t ,#s(%& t! to*!ns +!t!r #t #s*!+or,4o(!r&tors4#,!nt#f#!rs or &n ot!r s(!c#&% s/-o%s #n t! s/-o% t&-%!.
PROGRAM:>#nc%u,!?st,#o.@>#nc%u,!?str#n".@$o#, /n;c&r #n(utVW #nt %!n4#44*4-4c4/ (r#ntfn !nt!r t! !'(r!ss#on:; sc&nfKs4#n(ut; (r#ntfKsn4#n(ut;
-
8/18/2019 Compiler Design Lab Moster Copy
58/63
%!nstr%!n#n(ut; # +#%!#?%!n; #f#s&%(&#n(utV#W;;
/# # +#%!#s&%(&#n(utV#W;; # c# for*/*?c*; (r#ntfKc4#n(utV*W; (r#ntft #,!nt#f#!rn;
!%s! #f#s,#"#t#n(utV#W;; /# +#%!#s,#"#t#n(utV#W;; # -I for*/*?-*; (r#ntfKc4#n(utV*W; (r#ntft const&ntn; !%s! (r#ntfKct o(!r&torn4#n(utV#W; #
-
8/18/2019 Compiler Design Lab Moster Copy
59/63
OUTPUT:
Ent!r t! !'(r!ss#on: su/r&t! $&r su/r&t! $&r su/ #,!nt#f#!r o(!r&tor r&t! #,!nt#f#!r
o(!r&tor $&r #,!nt#f#!r
RESULT:
-
8/18/2019 Compiler Design Lab Moster Copy
60/63
Tus t! to*!ns "!n!r&t!, for t! "#$!n st&t!/!nts succ!ssfu%% &n, out(ut #s $!r#f#!,.
EX.NO:12 PARSING T8E STRING
AIM:
To +r#t! & C (ro"r&/ to f#n, +!t!r t! str#n" #s (&rs#n" or not.
ALGORIT8M:
1. St&rt t! Pro"r&/ ). R!&, t! #n(ut fro/ r#"t to %!ft. T!n co%%!ct t! #nfor/&t#on &-out t! to*!ns &s (!r t! #n(ut.0. T!n (!rfor/ t(! c!c*#n" &n, ot!r t(! of s!/&nt#c &n&%s#s.
2. Fro/ t&t "!n!r&t! t! #nt!r/!,#&t! r!(r!s!nt&t#on t!n #t +#%% -! (&rs!,.3. If #t s not "!n!r&t!, /!&ns t!n t! str#n" +#%% not -! not (&rs!,.
PROGRAM:
>#nc%u,!?st,#o.@c&r #(V)W#nt # $o#, /n; (r#ntfn !nt!r t! str#n"; sc&nfKs4#(; !; #f#(V#WZ; (r#ntfn t! str#n" #s (&rs!,; #f#(V#WZ; (r#ntfn t! str#n" #s not (ro(!r% t!r/#n&t!,; "!tc; !; t; !(r#/!; !(r#/!; #f#(V#W; #
-
8/18/2019 Compiler Design Lab Moster Copy
61/63
t; !(r#/!; t;
f; t(r#/!; t(r#/!; #f#(V#WJ; # f; t(r#/!;
f; #f#s&%(&#(V#W;;
# !%s! #f#(V#W; # !; #f#(V#W;; # !%s! !rror; !%s! !rror; !rror; (r#ntfn str#n" c&nnot -! (&rs!, succ!ssfu%%n; "!tc;
-
8/18/2019 Compiler Design Lab Moster Copy
62/63
OUTPUT:
Ent!r t! str#n"&-JcZt! str#n" #s (&rs!,
Ent!r t! str#n"&-Jct! str#n" #s not (ro(!r% t!r/#n&t!,
Ent!r t! str#n"&J-JcZstr#n" c&nnot -! (&rs!, succ!ssfu%%
-
8/18/2019 Compiler Design Lab Moster Copy
63/63
RESULT:
Tus t! (&rs#n" (ro"r&/ #s co/(#%!, &n, !'!cut!, succ!ssfu%% &n, out(ut #s $!r#f#!,.