先决条件 – 上下文无关语法
1. 歧义语法:
如果存在多个派生树或解析树,则上下文无关文法称为二义性文法。
例子 –
S -> S + S / S * S / S / a
2. 明确语法:
如果存在一棵且只有一棵派生树或解析树,则上下文无关文法称为无歧义文法。
例子 –
X -> AB
A -> Aa / a
B -> b
歧义和无歧义语法之间的区别:
S.NO | Ambiguous Grammar | Unambiguous Grammar |
---|---|---|
1. | In ambiguous grammar, the leftmost and rightmost derivations are not same. | In unambiguous grammar, the leftmost and rightmost derivations are same. |
2. | Amount of non-terminals in ambiguous grammar is less than in unambiguous grammar. | Amount of non-terminals in unambiguous grammar is more than in ambiguous grammar. |
3. | Length of the parse tree in ambiguous grammar is comparatively short. | Length of the parse tree in unambiguous grammar is comparatively large. |
4. | Speed of derivation of a tree in ambiguous grammar is faster than that of unambiguous grammar. | Speed of derivation of a tree in unambiguous grammar is slower than that of ambiguous grammar. |
5. | Ambiguous grammar generates more than one parse tree. | Unambiguous grammar generates only one parse tree. |
6. | Ambiguous grammar contains ambiguity. | Unambiguous grammar does not contain any ambiguity. |