P1774.14 为练习4.3的文法构造一个预测语法分析器 bexpr→bexpr or bterm|bterm bterm→bterm and bfactor | bfactor bfactor→not bfactor|(bexpr)|true |false 解1 非递归方法 1) 消除左递归 ①bexpr→bterm A ②A→or bterm A ③A→ε ④bterm→bfactor B ⑤B→and bfactor B ⑥B→ε ⑦bfactor→not bfactor ⑧bfactor→(bexpr) ⑨bfactor→true ⑩bfactor→false 2) 求first集 与 follow集 针对以同一非总结符开头的产生式右部求first集 如果该非终结符能产生ε 则需要求其follow集 ①bexpr→bterm A first(bterm A)= {not,(,true,false} ②A→or bterm A first(or bterm A)={or} ③A→ε follow(A)=follow(bexpr)= {$, )} ④bterm→bfactor B first(bfactor B)={not,(,true,false} ⑤B→and bfactor B first(and bfactor B)={and} ⑥B→ε follow(B)=follow(bterm)=first(A) 因为first(A)= {or , ε} 包含ε 所以follow(B)=follow(bterm) =first(A) ∪follow(A)-{ε}={or, $, )} ⑦bfactor→not bfactor first(not bfactor)={not} ⑧bfactor→(bexpr) first((bexpr))={(} ⑨bfactor→true first(true)={true} ⑩bfactor→false first(false)={false}
|