10. In class we studied a top-down parser for the following grammar: < S > < assignment > |...
Question:
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 .
Fundamentals of Physics
ISBN: 978-0471758013
8th Extended edition
Authors: Jearl Walker, Halliday Resnick