GATE / CS ARCHIVE

GATE CSE 2003 / QUESTION 56

Predictive-table conflict for a dangling-else grammar

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

GATE 2003, Q56 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 56, GATE 2003: For the grammar S→iEtSS′ | a; S′→eS | ε; E→b, consider the usual predictive parsing table M with end marker $. What are M[S′,e] and M[S′,$], respectively?
A. {S′→eS}, {S′→ε}
B. {S′→eS}, ∅
C. {S′→ε}, {S′→ε}
D. {S′→eS,S′→ε}, {S′→ε}
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (2 parts)

1. FIRST(eS)={e}, so insert S′→eS in the e column.
2. S is the start symbol, so $∈FOLLOW(S). In S→iEtSS′, the first S is followed by nullable S′ whose nonempty FIRST is {e}; hence e∈FOLLOW(S) as well.
3. The final S′ inherits FOLLOW(S), giving FOLLOW(S′)={e,$}. For the ε production, put S′→ε in every FOLLOW column, therefore both e and $.
4. The resulting cells are {eS,ε} and {ε}, option D. The two entries in the e cell are the dangling-else conflict, so the grammar itself is not LL(1).

Solution: FIRST(eS)={e}, so the rule S′→eS enters M[S′,e]. Because S′ can be empty and follows S in S→iEtSS′, e belongs to FOLLOW(S), while $ belongs to FOLLOW(S) because S is the start symbol. The trailing S′ inherits FOLLOW(S), giving FOLLOW(S′)={e,$}. Thus S′→ε also enters both cells. The e cell contains both alternatives and the $ cell contains only ε, option D; the conflict means this grammar is not LL(1).

Original paper, page 8