计算FIRST和FOLLOW设置用于自上而下的解析,用于以下语法

时间:2015-03-19 05:03:00

标签: compiler-construction

对于下面的语法,我计算了FIRST和FOLLOW集     S->一种| + |(T)     T-> T.S | S 解: 消除左递归后,语法为:

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

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

FOLLOW集合是:     FOLLOW(S)= {。 ,$}     FOLLOW(T)= {)}     FOLLOW(T')= {)}

非左递归语法和FOLLOW设置是否正确?那么有人能告诉我我是否已经找到了正确的解决方案。我很困惑,$是否会添加到TT'的FOLLOW集合中... 请帮帮我

1 个答案:

答案 0 :(得分:0)

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.