CFG
CFG和RE的主要区别在于CFG可以递归,而RE不可以。
CFG use RE as components.
abstract syntax tree
Normal principle
Token sequences cannot be recovered from them (unlike parse trees).
Ambiguity in grammar
Ambiguous grammar: a grammar that generates a string with two distinct parse trees is called an ambiguous grammar.
Eliminate ambiguity in grammar:
- make a dis ambiguity rule
– advantage:
It corrects the ambiguity without changing the grammar
– disadvantage
The syntactic structure of the language is no longer given by the grammar alone - change the grammar into a form that forces the construction of the correct parse tree
a left recursive rule makes its operators associate on the left
a right recursive rule makes them associate on the right.
Precedence and associativity
将以下RE文法转换成正确优先级文法
为一个运算符构建属于它的每一条文法,优先级低的在前,高的在后
Solution: