计算FIRST和FOLLOW集自顶向下解析,对于下面的文法

问题描述:

对于下面的文法,我已经计算出FIRST和FOLLOW集合T> TS | S 解决方案: 消除左递归后,语法是:计算FIRST和FOLLOW集自顶向下解析,对于下面的文法

S->a|+|(T) 
T->aT'|+T'|(T)T' 
T'->.ST'|epsilon 

现在第一集合是: FIRST(S)= {A,+,(} FIRST(T)= {A,+,( } FIRST(T')= {。,epsilon}

后续设置为: 关注(S)= {。 ,$} FOLLOW(T)= {)} FOLLOW(T')= {)}

非左递归语法和FOLLOW设置是否正确?那么有人可以告诉我我是否已经达到了正确的解决方案。我很困惑$是否会被加入到FOLLOW集为TT'也... 请帮我

First sets, Straight forward: 
S = { a, +, (} 
T = { a, +, (} 
T' = {epsilon, .} 

Follow Sets: 
S = {), ., $ } 
T = {) } 
T' = {) } 

As per the definition of follow from dragon book: 

1. Place $ in FOLLOW(S) , where S is the start symbol, and $ is the input 
right end marker . 

2. If there is a production A -> aBβ, then everything in FIRST(β) except ε 
is in FOLLOW(B) . 

3. If there is a production A -> aB, or a production A -> aBβ, where 
FIRST(β) contains ε, then everything in FOLLOW(A) is in FOLLOW(B) . 

Follow of S contains ')' if we apply the rule 3: 
T' -> .ST' 

As per rule 3: a = . , B = S , T' = β and FIRST(T') contains 'ε', everything in Follow of T' is in Follow of S. Since follow of T' contains ')' it also becomes member of S.