Question
10. In class we studied a top-down parser for the following grammar: < S > < assignment > | < block > < assignment
10. In class we studied a top-down parser for the following grammar:
< S > → < assignment > | < block >
< assignment > → < id > = < E > ;
< block > → "{" < S List > "}"
< S List > → { < S > } +
Let us extend < S > by while-loops and conditionals:
< S > → < assignment > | < block > | < while > | < cond >
< while > → while "(" < B > ")" < S >
< cond > → if "(" < B > ")" < S > [ else < S > ]
Give pseudo code for a top-down parser for the extended < S >, < while >, < cond >, and < S List >. Presume that you are given the function B( ) to parse Boolean expressions < B >, E( ) to parse arithmetic expressions < E >, and getToken( ) to extract the next token and assign it to the string variable t . The tokens are: <id>, while , if , else , "{", "}", "(", ")", "=", ";". The top-down parser, if correctly constructed according to this grammar, should automatically build implicit parse trees in which each else matches the closest preceding unmatched if .
Step by Step Solution
3.51 Rating (158 Votes )
There are 3 Steps involved in it
Step: 1
Enter device blockh Enter Enter include Enter device ideh Enter theme malloch Block the device Structural Block list listelem listelem Items in all bl...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started