2

I can't tell if I'm misunderstanding what's going on, or if Wikipedia's explanation is incorrect.

Wikipedia says:

FOLLOW(k,B) of an item set k and an nonterminal B is the union of the follow sets of all items in K where '•' is followed by B.

Their example grammar looks like this:

S → E
E → T
E → ( E )
T → n
T → + T
T → T + n

where they found the LR(0) item set 0 to be:

[S → • E]
[E → • T]
[E → • ( E )]
[T → • n]
[T → • + T]
[T → • T + n] 

That means, then, that FOLLOW(0,T) is the union of the follow sets of all items in item set 0 where '•' is followed by T.

Applying their logic, we get that "items in item set 0 where '•' is followed by T" are in fact these two items:

  • [E → • T]
  • [T → • T + n]

However, this is where I get stuck:
The follow set of the second one includes the symbol ), because the item [E → • ( E )] can produce [E → • ( T )], which means a ) must be in the follow set.

However, Wikipedia says that FOLLOW(0,T) = { $, '+' }.

What am I doing wrong?

user541686
  • 205,094
  • 128
  • 528
  • 886

1 Answers1

1

I found the description of "Warshall's Closure Algorithm" in this book

Bornat : Understanding and Writing Compilers

to be helpful here. (Overall I found that book more readable and practically useful than the more famous books on compiler design!)

There may be other good descriptions of the algorithm around.

EDIT : I am rusty but I believe Wikipedia article is right in this respect :

Remember this is Follow(0,T).
Now you are clearly right that ')' can follow T in some circumstances.

However, not from a starting point of 0 ... that would mean an expression of the form n) or n+n) or n+n+n) which, written out explicitly like this, are obvious syntax errors.

(Making these things explicit instead of burying them in mathematical notation is what I really liked about that book!)