Examples
Trivial language
The simplest example is the following ambiguous grammar (with start symbol A) for the trivial language that consists of only the empty string: :A → A , ε …meaning that the nonterminal A can be derived to either itself again, or to the empty string. Thus the empty string has leftmost derivations of length 1, 2, 3, and indeed of any length, depending on how many times the rule A → A is used. This language also has an unambiguous grammar, consisting of a single production rule: :A → ε …meaning that the unique production can produce only the empty string, which is the unique string in the language. In the same way, any grammar for a non-empty language can be made ambiguous by adding duplicates.Unary string
The regular language of unary strings of a given character, say'a'
(the regular expression a*
), has the unambiguous grammar:
:A → aA , ε
…but also has the ambiguous grammar:
:A → aA , Aa , ε
These correspond to producing a right-associative tree (for the unambiguous grammar) or allowing both left- and right- association. This is elaborated below.
Addition and subtraction
The context free grammar :A → A + A , A − A , a is ambiguous since there are two leftmost derivations for the string a + a + a: As another example, the grammar is ambiguous since there are two parse trees for the string a + a − a: : The language that it generates, however, is not inherently ambiguous; the following is a non-ambiguous grammar generating the same language: :A → A + a , A − a , aDangling else
A common example of ambiguity in computer programming languages is theelse
in an If–then(–else) statement is optional, which results in nested conditionals having multiple ways of being recognized in terms of the context-free grammar.
Concretely, in many languages one may write conditionals in two valid forms: the if-then form, and the if-then-else form – in effect, making the else clause optional:The following example uses Pascal syntax
In a grammar containing the rules
Statement → if Condition then Statement ,
if Condition then Statement else Statement ,
...
Condition → ...
some ambiguous phrase structures can appear. The expression
if a then if b then s else s2
can be parsed as either
if a then begin if b then s end else s2
or as
if a then begin if b then s else s2 end
depending on whether the else
is associated with the first if
or second if
.
This is resolved in various ways in different languages. Sometimes the grammar is modified so that it is unambiguous, such as by requiring an endif
statement or making else
mandatory. In other cases the grammar is left ambiguous, but the ambiguity is resolved by making the overall phrase grammar context-sensitive, such as by associating an else
with the nearest if
. In this latter case the grammar is unambiguous, but the context-free grammar is ambiguous.
An unambiguous grammar with multiple derivations
The existence of multiple derivations of the same string does not suffice to indicate that the grammar is ambiguous; only multiple ''leftmost'' derivations (or, equivalently, multiple parse trees) indicate ambiguity. For example, the simple grammar S → A + A A → 0 , 1 is an unambiguous grammar for the language . While each of these four strings has only one leftmost derivation, it has two different derivations, for example S ⇒ A + A ⇒ 0 + A ⇒ 0 + 0 and S ⇒ A + A ⇒ A + 0 ⇒ 0 + 0 Only the former derivation is a leftmost one.Recognizing ambiguous grammars
The decision problem of whether an arbitrary grammar is ambiguous is undecidable because it can be shown that it is equivalent to the Post correspondence problem. At least, there are tools implementing some semi-decision procedure for detecting ambiguity of context-free grammars. The efficiency of parsing a context-free grammar is determined by the automaton that accepts it. Deterministic context-free grammars are accepted by deterministic pushdown automata and can be parsed in linear time, for example by anInherently ambiguous languages
The existence of inherently ambiguous languages was proven with Parikh's theorem in 1961 by Rohit Parikh in an MIT research report. While some context-free languages (the set of strings that can be generated by a grammar) have both ambiguous and unambiguous grammars, there exist context-free languages for which no unambiguous context-free grammar can exist. An example of an inherently ambiguous language is the union of with . This set is context-free, since the union of two context-free languages is always context-free. But give a proof that there is no way to unambiguously parse strings in the (non-context-free) common subset .p.99-103, Sect.4.7See also
* GLR parser, a type of parser for ambiguous and nondeterministic grammars * Chart parser, another type of parser for ambiguous grammars * Syntactic ambiguityReferences
Notes
{{reflist, group=noteExternal links