fool2fish / dragon-book-exercise-answers

Compilers Principles, Techniques, & Tools (purple dragon book) second edition exercise answers. 编译原理(紫龙书)第2版习题答案。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exercise 4.4.1 d)

huangxueqin opened this issue · comments

The production S -> S + S | S S | (S) | S * | a, after eliminate left recursion you got

S -> TB
B -> AB | ε
A -> +S | TB | *          <--- TB should replace with S
T -> (S) | a  

so it turns to be

S -> TB
B -> AB | ε
A -> +S | S | *
T -> (S) | a  

And the follow set should be

FOLLOW(S) = {),$}
FOLLOW(B) = {),$}
FOLLOW(A) = {+,*,(,a,),$}
FOLLOW(T) = {+,*,(,a,),$}

Because A -> +S and Grammer Rule No.3, all elements in FOLLOW(A) should in FOLLOW(S), which means they are all {+, *, (, a, ), $}.

commented

Well, I agree with @NoteBookie

Yes, I think the FOLLOW sets are all the same too.

So. The origin answer should be changed as soon as possible.

yeah,I get the same answer as @NoteBookie ,it should be changed.

i have a question,why $ is in FOLLOW(A) and FOLLOW(T)?

For the Follow of T, we need first of B, now first of B(remember second grammar has a epsilon, so we can substitute it) means first of A, which means +,* and first of S, which implies first of T, which has ( and a, now come back to the epsilon, using the 1st grammar, we reach at the end of it since we can substitute that B with € and thus we hit the end of it(1st production) and thus we include follow of (left side)S which has $ and )
Hope it helps @theo-one.