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

4.4.2 -(4) Follow set

Terrence-Chen opened this issue · comments

I transformed the original grammar to the following by doing left-factoring and ambiguity elimination.

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

While from my understanding on the First & Follow set algorithm, we should get these sets, in which the Follow sets are totally different from the 'answer' that author provided.

First(S) = {(, a}
First(B) = {+, (, a, *, ε}
First(A) = {+, (, a, *}
First(T) = {(, a}

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

Below is the provided answer.

first(T) = [(, a]
first(A) = [+, *] + first(T) =[+, *, (, a]
first(B) = [ε] + first(A) = [ε, +, *, (, a]
first(S) = first(T) = [(, a]

follow(T) = [$, +, *, (, a]
follow(A) = [$, +, *, (, ), a]
follow(B) = [$]
follow(S) = [$, +, *, (, ), a]

The 'answer' can be easily proven as wrong. For example, we have production S -> TB while First(B) contains ε. So we know that Follow(S) must be a subset of Follow(B) by rule 3 of Follow set algorithm introduced in dragon book.