Search code examples
parsingcompiler-constructiongrammarcontext-free-grammar

How to find FIRST sets for a recursive grammar


I have a grammar:

S → SL | ε
L → A; | E; | C;
E → (EBE) | N | V
A → let V =E
C → while E do S | while E do S else S
B → + | - | * | >
V → x | y | z
N → ND | N0 | D
D → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 

I am trying to build the parse table for me to be able to prove that it is not an LL(1) grammar. I am currently stuck of finding the FIRST set for S. I keep getting S in the set and cannot reach a terminal of G. What would FIRST(SL) be?

When I do FIRST(SL), I keep going back to FIRST(S), which goes to FIRST(SL) ⋃ FIRST(ε) = FIRST(S) ⋃ {ε}, and the FIRST(S) will repeat itself over and over.


Solution

  • Capital letters represent non-terminals. Small letters represent terminals.

    First and Follow are the TERMINAL symbols that precede or succeed a terminal or a non terminal (in a pre order traversal of the parse tree).

    For all A -> xYZ
    First (A) = {x}
    For all A-> Xyz
    First (A) = First(X)
    For all A-> εYZ
    First (A) = First(Y)
    

    Take the union of all these terminal symbols.