GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 22(b)

LL(1) parsing table

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

GATE 1998, Q22(b) Descriptive
Report issue

Parsing & Syntax Analysis

Question 22(b), GATE 1998: Construct the LL(1) table for Program→begin d semi X end; X→d semi X | sY; Y→semi sY | ε. Treat begin,d,semi,s,end as terminals.
Question 22(b), GATE 1998: Construct the LL(1) table for Program→begin d semi X end; X→d semi X | sY; Y→semi sY | ε. Treat begin,d,semi,s,end as terminals.
Tap to enlarge
Answer & solution

Answer: Nonempty table entries: Program/begin; X/d,X/s; Y/semi,Y/end. (no official key was published)

Step through it (2 parts)

FIRST(Program)={begin}; FIRST(X)={d,s}; FIRST(Y)={semi,ε}. Program is the start symbol, so FOLLOW(Program)={$}. In Program's production, X is followed by end, hence FOLLOW(X)={end}. The occurrence Y at the end of X→sY gives FOLLOW(Y)={end}; its recursive production adds no new follower.

Fill a production in columns belonging to the FIRST set of its right side. For the ε production, use FOLLOW(Y). The complete non-error entries are:

Nonterminal | lookahead | production
Program     | begin     | Program→begin d semi X end
X           | d         | X→d semi X
X           | s         | X→sY
Y           | semi      | Y→semi sY
Y           | end       | Y→ε

Every other cell, including all cells under $, is an error for these nonterminals. The parsing stack accepts only when both its end marker and the input end marker match after Program is fully processed. There is no conflict: X's alternatives start with different terminals, and FIRST(semi sY)={semi} is disjoint from FOLLOW(Y)={end}. That establishes LL(1), rather than merely presenting a table without explaining why it works.

Original paper, page 8