Compiler-Solution for First set, Follow set and LL(1) Parsing table
Solution for First Set
To compute FIRST(X) for all grammar symbols X, apply the following rules until no more terminals or ε can be added to any FIRST set.
If X is a terminal, then
. If X is a nonterminal and
is a production for some , then place in if for some , is in , and is in all of . If is in for all , then add to . For example, everything in is surely in . If does not derive , then we add nothing more to , but if , then we add , and so on. If
is a production, then add to .
Now, we can compute
Solution for Follow Set
To compute
Place $ in
, where is the start symbol, and $ is the input right end marker. If there is a production
, then everything in except is in . If there is a production
, or a production , where contains , then everything in is in .
- 对于文法的开始符号
,置 $ 于 中; - 若
是一个产生式,则把 加入到 中 - 若
是一个产生式,或 是一个产生式且 ,则把 加入到 中
(
Solution for LL(1) Parsing Table
Suppose the parsing table
The entry
For each production
- Foe each token
in , add the grammar production to . - If
is in then, for each token in the , add to . - All other entries in the table are left blank and correspond to a syntax error.
流程:
遍历两次 Production Rule
第一次遍历,对每个 Production Rule 检查 Rule-1
- 如果
是终结符,则把 加入到 中 - 如果
是非终结符,则把 加入到
第二次遍历,对每个 Production Rule 检查 Rule-2
查看Production Rule 中所有可以产生