0

LL(1) Grammar:

(1) Var -> ID DimList   
(2) DimList -> ε DimList'  
(3) DimList' -> Dim DimList'
(4) DimList' -> ε  
(5) Dim -> [ CONST ]

And, in the script that I am reading, it says that the function FIRST(ε DimList') gives {#, [}. But, how?

My guess is that since the right side of (2) begins with ε, it skips epsilon and takes FIRST(DimList') which is, considering (3) and (5), equal to {[}, BUT also, because of (4), takes FOLLOW(DimList') which is {#}.

Other way it could be is that, since (2) begins with ε it skips epsilon and takes FIRST(DimList') BUT ALSO takes FOLLOW(DimList) from (2)...

First one makes more sense to me, though I'm still in the process of learning basics of LL(1) grammars so I would appreciate if someone takes the time to make this clear, thank you.

EDIT: And, of course, it could be that neither of these is true.

TedK.
  • 125
  • 1
  • 9

1 Answers1

1

The usual definition of the FIRST function would result in FIRST(Dimlist) (or, if you like, FIRST(ε Dimlist') being {ε, [}. ε is in FIRST(ε Dimlist') because both ε and Dimlist' are nullable. [ is an element because it could be the first symbol in a derivation of ε Dimlist, which is the same as saying that it could be the first symbol in a derivation of Dimlist'.

Another way of saying this is that:

FIRST(ε Dimlist' #) = {#, [}

We usually then define the function PREDICT:

PREDICT(ω) = FIRST(ω FOLLOW(ω))

and we can see that

PREDICT(Dimlist) = FIRST(Dimlist FOLLOW(Dimlist)) = {#, [}

Here, FIRST(ω) is the set of strings of terminals (of length ≤ 1) which could appear at the beginning of a derivation of ω, while PREDICT(ω) is the set of strings of terminals (of length ≤ 1) which could be present in the input when a derivation of ω is possible.

It's not uncommon to confuse FIRST and PREDICT, but it's better to keep the difference straight.

Note that all of these functions can be generalized to strings of maximum length k, which are usually written FIRSTk, FOLLOWk and PREDICTk, and the definition of PREDICTk is similar to the above:

PREDICTk(ω) = FIRSTk(ω FOLLOWk(ω))

rici
  • 234,347
  • 28
  • 237
  • 341