Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ambiguous grammar
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 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 (formal languages)|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 <code>'a'</code> (the regular expression <code>a*</code>), 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: {| border="0" |----- | || A || β A + A | | | A || β A + A |----- | || || β a + A | | | || β A + A + A (First A is replaced by A+A. Replacement of the second A would yield a similar derivation.) |----- | || || β a + A + A | | | || β a + A + A |----- | || || β a + a + A | | | || β a + a + A |----- | || || β a + a + a | | | || β a + a + a |} As another example, the grammar is ambiguous since there are two [[parse tree]]s for the string a + a − a: : [[Image:Leftmostderivations jaredwf.svg|Leftmostderivations jaredwf.svg|400px|class=skin-invert]] 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 | a === Dangling else === {{main|Dangling else}} A common example of ambiguity in computer programming languages is the [[dangling else]] problem. In many languages, the <code>else</code> in an [[Conditional (computer programming)#Ifβthen(βelse)|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. In a grammar containing the rules{{efn|The following example uses [[Pascal (programming language)|Pascal]] syntax.}} 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 <code>else</code> is associated with the first <code>if</code> or second <code>if</code>. This is resolved in various ways in different languages. Sometimes the grammar is modified so that it is unambiguous, such as by requiring an <code>endif</code> statement or making <code>else</code> 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 <code>else</code> with the nearest <code>if</code>. In this latter case the grammar is unambiguous, but the context-free grammar is ambiguous.{{clarify|reason=There is no such thing as an 'overall phrase grammar'. The 'nearest-if' rule can possibly be implemented by using a slightly modified, but still context-free grammar.|date=January 2017}} === 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 { 0+0, 0+1, 1+0, 1+1 }. While each of these four strings has only one leftmost derivation, it has two different derivations, for example S [[Context-free grammar#Rule application|β]] A + A β 0 + A β 0 + 0 and S β A + A β A + 0 β 0 + 0 Only the former derivation is a leftmost one.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)