GATE / CS ARCHIVE

GATE CSE 1993 / QUESTION 25(a)

Grammar for small Pascal statements

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 1993 paper

GATE 1993, Q25(a) Descriptive
Report issue

Parsing & Syntax Analysis

Question 25(a), GATE 1993: Construct an ordinary CFG for assignment id:=expression, for id:=expression to expression do statement, and begin statement;…;statement end. expression is already defined. Do not use EBNF optional brackets or star.
Question 25(a), GATE 1993: Construct an ordinary CFG for assignment id:=expression, for id:=expression to expression do statement, and begin statement;…;statement end. expression is already defined. Do not use EBNF optional brackets or star.
Tap to enlarge
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.

Original paper, page 14