GATE / CS ARCHIVE

GATE CSE 2001 / QUESTION 16(a)

Compute FIRST and FOLLOW

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

GATE 2001, Q16(a) Descriptive
Report issue

Parsing & Syntax Analysis

Question 16(a), GATE 2001: Start symbol E; productions E→aA | (E), A→+E | *E | ε. Terminals a,(,),+,*.
Question 16(a), GATE 2001: Start symbol E; productions E→aA | (E), A→+E | *E | ε. Terminals a,(,),+,*.
Tap to enlarge
Answer & solution

Answer: FIRST(E)={a,(}; FIRST(A)={+,*,ε}; FOLLOW(E)=FOLLOW(A)={),$}. (no official key was published)

E→aA begins with a; E→(E) begins with (, so FIRST(E)={a,(}. A’s alternatives begin with + and *, or are empty, giving FIRST(A)={+,*,ε}. Put end marker $ into FOLLOW(E) because E is the start symbol. E→(E) adds ) to FOLLOW(E). E→aA puts A at the end, so FOLLOW(E) is included in FOLLOW(A). A→+E and A→*E put E at the end, so FOLLOW(A) is included in FOLLOW(E). Iterating these inclusions reaches the fixed point FOLLOW(E)=FOLLOW(A)={),$}. Neither + nor * follows E in this grammar; those operators start A.

Original paper, page 9