Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

page 96 page 97 page 100 4. (20pt) Consider the following grammar for a declaration list: ded list - ded list deel ; ded; dedid

image text in transcribed
image text in transcribed page 96
image text in transcribedpage 97
image text in transcribedpage 100
4. (20pt) Consider the following grammar for a declaration list: ded list - ded list deel ; ded; dedid : type type intreal char array const .. const of type record ded_list end Construct the characteristic finite state machine for this grammar (like the one in Fig. 2.26. p.96-97 of textbook). Then use it to parse (like in Fig.2.30, p.100 of textbook) the program below: foo: record a : char; b: array 1.. 2 of real; end; 96 Chapter 2 Programming Language Syntax State Transitions program - h list $$ on stilist shift and goto 2 stat.list- st.lists sti.list . sont- id :- expr start- read id St . write expr on strut shift and reduce (pop 1 state, push sterlist on input) on id shift and goto 3 on read shift and goto 1 on write shift and goto 4 1. St read.id on id shift and reduce (pop 2 states, push stmi on input) 2. program strat list similist . $$ that list start str Sint- sto i d :- expr .read id write expr on $$ shift and reduce (pop 2 states, push program on input) on stm shift and reduce (pop 2 states, push statlist on input) on id shift and goto 3 on read shift and goto 1 on vrite shift and goto 4 3. Samt id.:- expr on :-shift and goto 5 4. Stif write . expr on expr shift and goto 6 on term shift and goto 7 on factor shift and reduce (pop state, push term on input) expr expr ferint- term factor factor - factor . territ . expr wel op te .fctor . terme mult op factor . expr .id number on shift and goto 8 on id shift and reduce (pop I state, push factor on input) on number shift and reduce (pop I state, push factor on input) 5. str i d .expr on cxpr shift and goto 9 on term shift and goto 7 on factor shift and reduce (pop 1 state, push term on input) expr . term expr . expr add op termi ferm . factor term terme munop factor factor . expr) factor - .id factor nusber on shift and goto 8 on id shift and reduce (pop I state, push factor on input) on number shift and reduce (pop I statc, push factor on input) 6. sta expr v ite expr. e xpr. add op term on FOLLOW(str) = {id, read, write. $$) reduce (pop 2 states, push stron input) on add op shift and goto 10 on shift and reduce (pop 1 state, push add op on input) on-shift and reduce (pop 1 state, push add op on input) addop Add op . Figure 2.26 CFSM for the calculator grammar (Figure 2.25). Basis and closure itens in each state are separated by a horizontal rule. Trivial reduce-only states have been eliminated by use of shift and reduce transitions (continued State Transitions 7. . Exp term -term op factor on FOLLOW (pr) = id, read, write. $5.). -.- reduce pop I state, pushpron input) on top shit and goto 11 on shift and reduce (pop I state, push maitop on input) on / shift and reduce (popstate.push o p on input) mult.p . factor (.apr ) on per shift and goto 12 on te shift and goto 7 on factor shift and reduce (pop I state.push term on input) expr er m factor factor factor . expr adapter factor . r ap factor . expr ) .id . number on shift and goto 8 on id shift and reduce (pop l state, push factor on input) on number shift and reduce (pop I state, push factor on input) sufid: expr. expr expr. add. op term on FOLLOW strut) = {1d, read, write, SS) reduce pop 3 states. push stron input) on add ap shift and goto 10 on + shift and reduce (pop 1 state, push add op on input) on-shift and reduce (popstate, push add op on input) addop add_op- . .- 10. cxprexpr adidapter on term shift and goto 13 on factor shift and reduce (pop I state, push term on input) Term Term factor factor factor . facter . term mult op factor . expr > .id . number on shift and goto 8 on id shift and reduce (pop l state, push factor on input) on number shift and reduce (pop I state, push factor on input) 11. fem m e mult op.factor on factor shift and reduce (pop 3 states, push term on input) factor factor factor (expr) .id . number on shift and goto 8 on id shift and reduce (pop l state, push factor on input) on number shift and reduce (pop I state, push factor on input) 12. factor (expr.) expr expr. addop term lop . dilep- .- on) shift and reduce (pop 3 states push factor on input) on add ap shift and goto 10 on shift and reduce (pop 1 state, pushad op on input) on-shift and reduce (pop 1 state.pushaddop on input) 13. expr le e xpr add op term tap factor - on FOLLOW (pr) = {id, read, write. $5.). - -} reduce pop 3 states, pushpron input) on muut op shift and goto 11 on shift and reduce (pop l state, push p on input) on / shift and reduce (pop 1 state. poshmatop on input) malop -.. m .op . Figure 2.26 continued 100 Chapter 2 Programming Language Syntax Parne stack Input stream Comment O read ] read A read ... A read w read .. Stread. read id Otrolir 2 O s ist read Om Mist 2 shit sdCA) & reduce by shifts & reduce by s shift ist hread but sa(B) & reduce by shiftsm & reduce by sms talot acous) read id lists - O lit 2 Os.Jis 2 10 3 Ostr 2 10 3 - 5 Ostmist 2 10 3 5 Ostir 21 JS Ostr 2 1 3 5 7 O st list 2 1 3 - 5 O m 2 1 5 expr 9 Oslist 2 143 - Sexpr 9 O r 2 185 expr 9 ad ep 10 Ostw.list 2 10 3 - expr 9 adeep 10 O lur 2 145 expr 9 ad ep 10 Ostatlist 2 13 5 expr 9 ep 10 for Omir 2 1 3 - 5 O der ? 13 Sep 9 O list 2 Ostatlist 2 O lis 2 write 4 Ostr.list 2 write 4 O r write 4 OmJor write term 7 O list 2 write 4 Ostu rite expr 6 O 2 factor 3 Shift IdCA) & reduce by factor i d B. but actor & reduce by actor 3 vite. htter cp 3 write reduce by pre 3 write adap write chift & reduce by addop . B vites lap factories shift dB) & reduce by factorid writes tacter & reduce by actor 13 write ... shift terme redace by expresprad op term write se stop Stwrites reduce by s ide expe ft & reduce by Jists write me... shift statlist su vites hift write face vite shift sdsus) & reduce by factor i d writes shift factor & reduce by factor write prwrite u deceby expre writer http reduce by write expr smrt write su... shifts & reduce by miss lists write shift strat list sum / 2... hift write factor/2 shift (sua) & reduce by factor i d Hom /2.. hittar & reduce by em factor 1 2 $$ shift op 25$ shift / & reduce by m e.op / 2 $$ shift . facters shift sumber(2) & reduce by number $$ shift factor & reduce by m e murop factor shift cxpr reduce by expr- shift cope $$ reduce by l ite expr strar less shifts & redace by wrist w list stet shift trist program shift $$ & reduce by program stutist $$ O strat list 2 O ur write 4 Ost list 2 write 4 O list 2 write 4 Oslis 2 write 4 7 Oslo Vite 4 m 7 Osmlist 2 write 4 MW 7 walep 11 Ost list 2 write 4 mm 7 male op 11 Osim list 2 write 4 O s ist 2 write 4 m 7 O der weite Ostatlist 2 vite 4 expr 6 O list 2 O llir 2 donc Figure 2.30 Trace of a table-driven SLR(U) parse of the sum-and-average program. States in the parse stack are shown in boldface type. Symbols in the parse stack are for clarity only they are not needed by the parsing algorithm. Parsing begins with the initial state of the CFSM (State) in the stack. It ends when we reduce by pregrine list $. uncovering State O again and pushing program onto the input stream 4. (20pt) Consider the following grammar for a declaration list: ded list - ded list deel ; ded; dedid : type type intreal char array const .. const of type record ded_list end Construct the characteristic finite state machine for this grammar (like the one in Fig. 2.26. p.96-97 of textbook). Then use it to parse (like in Fig.2.30, p.100 of textbook) the program below: foo: record a : char; b: array 1.. 2 of real; end; 96 Chapter 2 Programming Language Syntax State Transitions program - h list $$ on stilist shift and goto 2 stat.list- st.lists sti.list . sont- id :- expr start- read id St . write expr on strut shift and reduce (pop 1 state, push sterlist on input) on id shift and goto 3 on read shift and goto 1 on write shift and goto 4 1. St read.id on id shift and reduce (pop 2 states, push stmi on input) 2. program strat list similist . $$ that list start str Sint- sto i d :- expr .read id write expr on $$ shift and reduce (pop 2 states, push program on input) on stm shift and reduce (pop 2 states, push statlist on input) on id shift and goto 3 on read shift and goto 1 on vrite shift and goto 4 3. Samt id.:- expr on :-shift and goto 5 4. Stif write . expr on expr shift and goto 6 on term shift and goto 7 on factor shift and reduce (pop state, push term on input) expr expr ferint- term factor factor - factor . territ . expr wel op te .fctor . terme mult op factor . expr .id number on shift and goto 8 on id shift and reduce (pop I state, push factor on input) on number shift and reduce (pop I state, push factor on input) 5. str i d .expr on cxpr shift and goto 9 on term shift and goto 7 on factor shift and reduce (pop 1 state, push term on input) expr . term expr . expr add op termi ferm . factor term terme munop factor factor . expr) factor - .id factor nusber on shift and goto 8 on id shift and reduce (pop I state, push factor on input) on number shift and reduce (pop I statc, push factor on input) 6. sta expr v ite expr. e xpr. add op term on FOLLOW(str) = {id, read, write. $$) reduce (pop 2 states, push stron input) on add op shift and goto 10 on shift and reduce (pop 1 state, push add op on input) on-shift and reduce (pop 1 state, push add op on input) addop Add op . Figure 2.26 CFSM for the calculator grammar (Figure 2.25). Basis and closure itens in each state are separated by a horizontal rule. Trivial reduce-only states have been eliminated by use of shift and reduce transitions (continued State Transitions 7. . Exp term -term op factor on FOLLOW (pr) = id, read, write. $5.). -.- reduce pop I state, pushpron input) on top shit and goto 11 on shift and reduce (pop I state, push maitop on input) on / shift and reduce (popstate.push o p on input) mult.p . factor (.apr ) on per shift and goto 12 on te shift and goto 7 on factor shift and reduce (pop I state.push term on input) expr er m factor factor factor . expr adapter factor . r ap factor . expr ) .id . number on shift and goto 8 on id shift and reduce (pop l state, push factor on input) on number shift and reduce (pop I state, push factor on input) sufid: expr. expr expr. add. op term on FOLLOW strut) = {1d, read, write, SS) reduce pop 3 states. push stron input) on add ap shift and goto 10 on + shift and reduce (pop 1 state, push add op on input) on-shift and reduce (popstate, push add op on input) addop add_op- . .- 10. cxprexpr adidapter on term shift and goto 13 on factor shift and reduce (pop I state, push term on input) Term Term factor factor factor . facter . term mult op factor . expr > .id . number on shift and goto 8 on id shift and reduce (pop l state, push factor on input) on number shift and reduce (pop I state, push factor on input) 11. fem m e mult op.factor on factor shift and reduce (pop 3 states, push term on input) factor factor factor (expr) .id . number on shift and goto 8 on id shift and reduce (pop l state, push factor on input) on number shift and reduce (pop I state, push factor on input) 12. factor (expr.) expr expr. addop term lop . dilep- .- on) shift and reduce (pop 3 states push factor on input) on add ap shift and goto 10 on shift and reduce (pop 1 state, pushad op on input) on-shift and reduce (pop 1 state.pushaddop on input) 13. expr le e xpr add op term tap factor - on FOLLOW (pr) = {id, read, write. $5.). - -} reduce pop 3 states, pushpron input) on muut op shift and goto 11 on shift and reduce (pop l state, push p on input) on / shift and reduce (pop 1 state. poshmatop on input) malop -.. m .op . Figure 2.26 continued 100 Chapter 2 Programming Language Syntax Parne stack Input stream Comment O read ] read A read ... A read w read .. Stread. read id Otrolir 2 O s ist read Om Mist 2 shit sdCA) & reduce by shifts & reduce by s shift ist hread but sa(B) & reduce by shiftsm & reduce by sms talot acous) read id lists - O lit 2 Os.Jis 2 10 3 Ostr 2 10 3 - 5 Ostmist 2 10 3 5 Ostir 21 JS Ostr 2 1 3 5 7 O st list 2 1 3 - 5 O m 2 1 5 expr 9 Oslist 2 143 - Sexpr 9 O r 2 185 expr 9 ad ep 10 Ostw.list 2 10 3 - expr 9 adeep 10 O lur 2 145 expr 9 ad ep 10 Ostatlist 2 13 5 expr 9 ep 10 for Omir 2 1 3 - 5 O der ? 13 Sep 9 O list 2 Ostatlist 2 O lis 2 write 4 Ostr.list 2 write 4 O r write 4 OmJor write term 7 O list 2 write 4 Ostu rite expr 6 O 2 factor 3 Shift IdCA) & reduce by factor i d B. but actor & reduce by actor 3 vite. htter cp 3 write reduce by pre 3 write adap write chift & reduce by addop . B vites lap factories shift dB) & reduce by factorid writes tacter & reduce by actor 13 write ... shift terme redace by expresprad op term write se stop Stwrites reduce by s ide expe ft & reduce by Jists write me... shift statlist su vites hift write face vite shift sdsus) & reduce by factor i d writes shift factor & reduce by factor write prwrite u deceby expre writer http reduce by write expr smrt write su... shifts & reduce by miss lists write shift strat list sum / 2... hift write factor/2 shift (sua) & reduce by factor i d Hom /2.. hittar & reduce by em factor 1 2 $$ shift op 25$ shift / & reduce by m e.op / 2 $$ shift . facters shift sumber(2) & reduce by number $$ shift factor & reduce by m e murop factor shift cxpr reduce by expr- shift cope $$ reduce by l ite expr strar less shifts & redace by wrist w list stet shift trist program shift $$ & reduce by program stutist $$ O strat list 2 O ur write 4 Ost list 2 write 4 O list 2 write 4 Oslis 2 write 4 7 Oslo Vite 4 m 7 Osmlist 2 write 4 MW 7 walep 11 Ost list 2 write 4 mm 7 male op 11 Osim list 2 write 4 O s ist 2 write 4 m 7 O der weite Ostatlist 2 vite 4 expr 6 O list 2 O llir 2 donc Figure 2.30 Trace of a table-driven SLR(U) parse of the sum-and-average program. States in the parse stack are shown in boldface type. Symbols in the parse stack are for clarity only they are not needed by the parsing algorithm. Parsing begins with the initial state of the CFSM (State) in the stack. It ends when we reduce by pregrine list $. uncovering State O again and pushing program onto the input stream

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions

Question

1 of 3 ZOOM Page

Answered: 1 week ago