Parsing & Syntax Analysis


Answer & solution
Answer: S→id := E | for id := E to E do S | begin L end; L→S | S ; L. (no official key was published)
Let S denote a statement, L a nonempty statement list, E the given expression nonterminal, and id an identifier token. Use:
S → id := E
S → for id := E to E do S
S → begin L end
L → S
L → S ; L
The first production recognizes assignments. The second has a statement as its body, permitting either a single assignment, another loop, or a block. The third encloses a list in begin/end. The two L productions generate exactly one or more statements separated by semicolons, without a trailing semicolon. They replace the informal ellipsis with ordinary CFG recursion; no repetition operator or optional metasyntax is used.
For any finite list length k≥1, apply L→S;L exactly k−1 times and finish with L→S. Conversely each derivation has precisely that separator structure. An empty block is not explicitly requested; if the language separately permits begin end, add an epsilon-list production as an explicit extension rather than silently changing the language. Expression syntax is delegated to the provided E grammar.