|
|
Ambiguous grammar
In computer science, a grammar is said to be an ambiguous grammar if there is some string that it can generate in more than one way (i.e., the string has more than one parse tree or more than one leftmost derivation). A language is inherently ambiguous if it can only be generated by ambiguous grammars. Some programming languages have ambiguous grammars; in this case, semantic information is needed to select the intended parse of an ambiguous construct. For example, in C the following: x * y ; can be interpreted either as the declaration of an identifier
Example
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:
Recognizing an ambiguous grammarThe general question of whether a grammar is not ambiguous is undecidable. No algorithm can exist to determine the ambiguity of a grammar because the undecidable Post correspondence problem can be encoded as an ambiguity problem. There is an obvious difficulty in parsing an ambiguous grammar by a deterministic parser (see deterministic context-free grammar) but nondeterministic parsing imposes a great efficiency penalty. Most constructs of interest to parsing can be recognized by unambiguous grammars. Some ambiguous grammars can be converted into unambiguous grammars, but no general procedure for doing this is possible just as no algorithm exists for detecting ambiguous grammars. Compiler generators such as YACC include features for disambiguating some kinds of ambiguity, such as by using the precedence and associativity constraints. Inherently ambiguous languagesWhile some languages (the set of strings that can be generated by a grammar) have both ambiguous and unambiguous grammars, there exist languages for which no unambiguous grammar can exist. An example of an inherently ambiguous language is the union of {anbmcmdn | n,m > 0} with {anbncmdm | n,m > 0}. This is context-free, since it is a union of context-free languages, but Introduction to Automata Theory... contains a proof that there is no way to unambiguously parse strings in the (non-context-free) subset {anbncndn | n > 0} which is the intersection of the two languages. External linksReferencesCompilers: Principles, Techniques, and Tools
Published - December 2008 Information from Wikipedia
is available under the terms of the GNU Free Documentation
License
E-mail this article to your colleague! Need more translation jobs? Click here! Translation agencies are welcome to register here - Free! Freelance translators are welcome to register here - Free! |
|
|
Legal Disclaimer Site Map |