Knowledge Builders

why are ambiguous grammars undesirable

by Gudrun Vandervort Published 3 years ago Updated 2 years ago
image

Ambiguous grammar is unlikely to be useful for a programming language because two parse tree structures (or more) for the same string (program) imply two different meanings (executable programs) for the program.

A grammar that can produce more than one parse tree for a given sentence is called ambiguous. Ambiguity is a very bad property for a grammar because it can lead to the sentence being interpreted in different and unexpected ways.

Full Answer

What is ambiguous grammar in programming languages?

Ambiguity is a property of grammar not languages. Ambiguous grammar is unlikely to be useful for a programming language, because two parse trees structures (or more) for the same string (program) implies two different meanings (executable programs) for the program.

Is language inherently ambiguous?

If every Context Free Grammar G with Language L = L (G) is ambiguous, then L is said to be inherently ambiguous Language. Ambiguity is a property of grammar not languages.

How to remove the ambiguity of a grammar?

Note : Ambiguity of a grammar is undecidable, i.e. there is no particular algorithm for removing the ambiguity of a grammar, but we can remove ambiguity by: Disambiguate the grammar i.e., rewriting the grammar such that there is only one derivation or parse tree possible for a string of the language which the grammar represents.

Is every context-free grammar inherently ambiguous?

If every Context-Free Grammar G with Language L = L (G) is ambiguous, then L is said to be inherently ambiguous Language. Ambiguity is a property of grammar not languages.

When compiling a program, we want the interpretation of the syntax to be unambiguous.?

Why is code non-portable?

Is ambiguous grammar useful?

Can two different compilers default to different derivations for an ambiguous expression?

Is it common to have ambiguous grammars?

Can you use a simple parser in LL?

Can a parser rule out ambiguity?

See 4 more

About this website

image

What is the problem of ambiguous grammar?

A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for the given input string. If the grammar is not ambiguous, then it is called unambiguous. If the grammar has ambiguity, then it is not good for compiler construction.

Why ambiguity is unsuitable as a programming language?

But in general such ambuguity is problematic because it is confusing to humans. If the same program can cause different behaviors, due to ambiguities in parsing or in the semantics, it can not easily used by programmers.

Which is better ambiguous or unambiguous grammar?

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.

How ambiguity is avoided in grammar?

Expansion: Adding a word or two to the sentence can remove ambiguity.

What do you mean by ambiguous grammar?

In computer science, an ambiguous grammar is a context-free grammar for which there exists a string that can have more than one leftmost derivation or parse tree, while an unambiguous grammar is a context-free grammar for which every valid string has a unique leftmost derivation or parse tree.

What makes a grammar ambiguous?

A grammar is said to be ambiguous if there exists more than one left most derivation or more than one right most derivation or more than one parse tree for a given input string. If the grammar is not ambiguous then we call it unambiguous grammar. If the grammar has ambiguity then it is good for compiler construction.

Is the grammar ambiguous or unambiguous justify your answer?

There exists no string belonging to the language of grammar which has more than one parse tree. Since a unique parse tree exists for all the strings, therefore the given grammar is unambiguous.

What is the difference between ambiguous and ambiguous?

Ambiguity is a noun and a concept. You can talk about ambiguity, you can describe ambiguity, you can even try to prevent it. Ambiguous is an adjective, derived from the noun, describing something. Like, "that text is ambiguous".

Which language can be called as ambiguous language?

Ambiguous Context Free Languages : A context free language is called ambiguous if there is no unambiguous grammar to define that language and it is also called inherently ambiguous Context Free Languages. Note : If a context free grammar G is ambiguous, language generated by grammar L(G) may or may not be ambiguous.

Why should we avoid ambiguity?

Ambiguity can be avoided. A small number of careless word choices cause most of the potential misunderstandings. Ambiguity results when a pronoun can refer to more than one noun. The reader may be able to determine the intended meaning, but careless writing is never highly respected.

What is the reason why we should avoid ambiguous word in communication?

Choosing ambiguous words or terms to convey your message will lead the receiver to confusion and misunderstanding. You can avoid any ambiguity by repeating the term or using alternatives. Ambiguous communication means open to many different interpretations.

How the ambiguity can be eliminated?

One can get rid of the ambiguity by encoding operator precedence and as- sociativity. Let us assume that ∗ binds more strongly than ⊕ and ⊗ and that these bind more strongly than &. We also assume that ⊕ and ⊗ bind equally tight and that ambiguity between these is resolved by choosing left associativity.

Are high level programming languages ambiguous?

Computer programming languages cannot be ambiguous by definition. They have to be directly converted to machine code to be executed by the CPU. There is no “maybe” reserved word in any of the 30 or so computer languages I have used. Some computer languages have a more simple syntax than others.

What is meant by ambiguity in Java?

The ambiguities are those issues that are not defined clearly in the Java language specification. The different results produced by different compilers on several example programs support our observations.

What is an ambiguous grammar How do you prove that a given grammar is ambiguous explain with an example?

A Grammar that makes more than one Leftmost Derivation (or Rightmost Derivation) for the similar sentence is called Ambiguous Grammar. Example − Verify whether the following Grammar is Ambiguous or Not. E → E+E|E ∗ E|id. Solution. For string id + id * id, there exist two parse trees.

How to Prove a Grammar is ambiguous? S -> (S)|SS|()

The easiest way to prove a grammar ambiguous is to find a sentence with two different parse trees. (Or two different rightmost derivations, which is exactly the same thing.

Converting ambiguous grammar to unambiguous - Stack Overflow

I did not understand how a unambiguous grammar is derived from a ambiguous grammar? Consider the example on site: Example. How was the grammar derived is confusing to me. Can anyone please guide...

When compiling a program, we want the interpretation of the syntax to be unambiguous.?

When compiling a program, we want the interpretation of the syntax to be unambiguous. The easiest way to enforce this is using an unambiguous grammar. If the grammar is ambiguous, we can provide tie-breaking rules, like operator precedence and associativity. These rules can equivalently be expressed by making the grammar unambiguous in a particular way.

Why is code non-portable?

Which leads people to assume it's a bug in one of the compilers while it's actually a fault in the language specification.

Is ambiguous grammar useful?

In contrast to the other existing answers [ 1, 2 ], there is indeed a field of application, where ambiguous grammars are useful . In the field of natural language processing (NLP), when you want to parse natural language (NL) with formal grammars, you've got the problem that NL is inherently ambiguous on different levels [adapted from Koh18, ch. 6.4]:

Can two different compilers default to different derivations for an ambiguous expression?

The first can fall out naturally, the second requires that the compiler programmer knows about the ambiguity. If this ambiguity stays undetected then it is possible that 2 different compilers default to different derivations for that ambiguous expression. Leading to code being non-portable for non-obvious reasons.

Is it common to have ambiguous grammars?

In real life it's pretty common to simply live with ambiguous grammars, as long as they aren't (so to speak) too ambiguous.

Can you use a simple parser in LL?

You’re also not dealing with something simple like a LL (1) language, so you can’t use a simple, small, fast parser. Your grammar has symbols that can be read multiple ways, so you have to be prepared to backtrack a lot.

Can a parser rule out ambiguity?

Even if there’s a well-defined way to handle ambiguity (ambiguous expressions are syntax errors, for example), these grammars still cause trouble. As soon as you introduce ambiguity into a grammar, a parser can no longer be sure that the first match it gets is definitive. It needs to keep trying all the other ways to parse a statement, to rule out any ambiguity. You’re also not dealing with something simple like a LL (1) language, so you can’t use a simple, small, fast parser. Your grammar has symbols that can be read multiple ways, so you have to be prepared to backtrack a lot.

Why is an inherently ambiguous language unsuitable for programming?

An inherently ambiguous language would be absolutely unsuitable as a programming language, because we would not have any way of fixing a unique structure for all its programs.

Can you practice for the GATE exam before the actual exam?

Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Is ambiguity a property of grammar?

Ambiguity is a property of gramm ar not languages. Ambiguous grammar is unlikely to be useful for a programming language, because two parse trees structures (or more) for the same string (program) implies two different meanings (executable programs) for the program.

Why is given grammar ambiguous?

Given grammar is Ambiguous since there exists two Parse Trees for the same string because else condition can belong to any if statement.

Which grammar makes more than one leftmost derivative?

A Grammar that makes more than one Leftmost Derivation (or Rightmost Derivation) for the similar sentence is called Ambiguous Gramm ar.

What does ACLA mean by ambiguity?

Given a grammar, ACLA will report it to be ambiguous, unambiguous, or pos-sibly ambiguous (that is, it is unsure if the grammar is ambiguous). ACLA'sapproach to ambiguity detection is based on two linguistic properties: verticaland horizontal ambiguity. Vertical ambiguity means that during the parsing of

What is context free grammar?

Context Free Grammars (CFGs) are widely used for describing formal languages,including Programming Languages (PLs). The full class of CFGs (grammarsfrom now on) includes ambiguous grammars|those which can parse inputs inmore than one way. Needless to say, ambiguous grammars are highly undesirable.If an input can be parsed in more than one way, which one of those parses shouldbe taken? We would not enjoy using a compiler if it were to continually ask usto choose which parse we want. Unfortunately, we know that, in general, it isundecidable as to whether a given grammar is ambiguous or not [7]. While thereare various parsing approaches which allow a user to manually disambiguateamongst multiple parses, one can not in general know if all possible points ofambiguity have been covered. Perhaps because of this, most tools use parsingalgorithms such as LL and LR, which limit themselves to parsing only a subsetof unambiguous grammars. This leads to other trade-o s: grammars have to becontorted to t them within these subsets; and these subsets rule out the abilityto compose grammars [14].

Can random grammars be close enough?

Random grammar generators have one major problem from our perspective:even if they produce grammars in the general style of those used by PLs, it canbe reasonably argued that they are never close enough. Of course, exactly whatisclose enough is impossible to pinpoint: it seems unlikely that any metric, orset of metrics, can reliably classify PL vs. non-PL grammars. Instead, we havelittle choice but to fall back on the intuitive notion that we know one when wesee one." This means that past work has struggled to understand how ambiguitya ects PL-like grammars: we simply can't get hold of enough of them to performadequate studies. The best attempt of which we are aware is the work of Basten,who took 20 unambiguous PL grammars and manually altered them to introduceambiguity [3]. Manually altering grammars is tedious, hard to scale, and alwaysopen to the possibilities of unintentional human bias.

Is Ambidexter eective for PL?

AmbiDexter is e ective for PL (altered and mutated) grammars, but is less ef-fective for Boltzmann grammars. AmbiDexter does well on PL grammars fortwo reasons. First, PL grammars contain short ambiguous subsets (see Table 4)and AmbiDexter's exhaustive search, whereby it checks for short strings exhaus-tively, is quick to nd it. Second, its ltering of unambiguous fragments wasvery e ective on PL grammars. For mutated grammars, where SLR1 was thebest performing lter, the percentage of rules ltered out were 60% (Pascal),90% (SQL), and 24% (Java) and 20% (C), whereas for Boltzmann grammars,it was 19%. There was a noticeable di erence in (SLR1) ltering time betweenmutated (1.3s) and Boltzmann grammars (0.7s). Since AmbiDexter uses an ex-haustive approach, it struggles when the ambiguous subsets are long and deeplynested.

When compiling a program, we want the interpretation of the syntax to be unambiguous.?

When compiling a program, we want the interpretation of the syntax to be unambiguous. The easiest way to enforce this is using an unambiguous grammar. If the grammar is ambiguous, we can provide tie-breaking rules, like operator precedence and associativity. These rules can equivalently be expressed by making the grammar unambiguous in a particular way.

Why is code non-portable?

Which leads people to assume it's a bug in one of the compilers while it's actually a fault in the language specification.

Is ambiguous grammar useful?

In contrast to the other existing answers [ 1, 2 ], there is indeed a field of application, where ambiguous grammars are useful . In the field of natural language processing (NLP), when you want to parse natural language (NL) with formal grammars, you've got the problem that NL is inherently ambiguous on different levels [adapted from Koh18, ch. 6.4]:

Can two different compilers default to different derivations for an ambiguous expression?

The first can fall out naturally, the second requires that the compiler programmer knows about the ambiguity. If this ambiguity stays undetected then it is possible that 2 different compilers default to different derivations for that ambiguous expression. Leading to code being non-portable for non-obvious reasons.

Is it common to have ambiguous grammars?

In real life it's pretty common to simply live with ambiguous grammars, as long as they aren't (so to speak) too ambiguous.

Can you use a simple parser in LL?

You’re also not dealing with something simple like a LL (1) language, so you can’t use a simple, small, fast parser. Your grammar has symbols that can be read multiple ways, so you have to be prepared to backtrack a lot.

Can a parser rule out ambiguity?

Even if there’s a well-defined way to handle ambiguity (ambiguous expressions are syntax errors, for example), these grammars still cause trouble. As soon as you introduce ambiguity into a grammar, a parser can no longer be sure that the first match it gets is definitive. It needs to keep trying all the other ways to parse a statement, to rule out any ambiguity. You’re also not dealing with something simple like a LL (1) language, so you can’t use a simple, small, fast parser. Your grammar has symbols that can be read multiple ways, so you have to be prepared to backtrack a lot.

image

1.Why are ambiguous grammars undesirable? - Quora

Url:https://www.quora.com/Why-are-ambiguous-grammars-undesirable

5 hours ago  · ComFreek. 438 2 13. Show 1 more comment. 11. Even if there’s a well-defined way to handle ambiguity (ambiguous expressions are syntax errors, for example), these grammars …

2.Why are ambiguous grammars bad? - Computer Science …

Url:https://cs.stackexchange.com/questions/110402/why-are-ambiguous-grammars-bad

23 hours ago  · Ambiguity is a property of grammar not languages. Ambiguous grammar is unlikely to be useful for a programming language because two parse tree structures (or more) …

3.What is Ambiguous Grammar? - tutorialspoint.com

Url:https://www.tutorialspoint.com/what-is-ambiguous-grammar

21 hours ago  · Given grammar is Ambiguous since there exists two Parse Trees for the same string because else condition can belong to any if statement. In the above Parse tree, else …

4.Why are ambiguous grammars usually a bad idea? Why …

Url:https://groups.google.com/g/comp.compilers/c/8SPFyhrSLr8

18 hours ago Hello Compiler Experts! I am reading the (excellent) book "flex & bison" by John Levine. Chapter 7 talks about conflicts in grammars: shift/reduce and reduce/reduce conflicts. At

5.why is this a ambiguous grammar? - Stack Overflow

Url:https://stackoverflow.com/questions/57464287/why-is-this-a-ambiguous-grammar

6 hours ago  · Your understanding of point #2 is flawed. A grammar G is ambiguous iff some w in L(G) has more than one leftmost (or rightmost) derivation. You've shown one leftmost …

6.ambiguity - Ambiguous grammar? - Stack Overflow

Url:https://stackoverflow.com/questions/4484879/ambiguous-grammar

22 hours ago  · That is what ambigious grammar means. the problem with mbigious grammars is that if you are writing a compiler, and you want to identify each token in certain line of code (or …

7.Detecting Ambiguity in Programming Language …

Url:https://soft-dev.org/pubs/pdf/vasudevan_tratt__detecting_ambiguity_in_programming_language_grammars.pdf

14 hours ago of possibly ambiguous PL-like grammars, we can explore a much wider set of possibilities than is practical by hand. To summarise, our work has two hypotheses: H1 Covering a grammar in …

8.Solved Explain why even if a grammar is NOT ambiguous …

Url:https://www.chegg.com/homework-help/questions-and-answers/explain-even-grammar-ambiguous-could-still-defined-work-way-designer-may-wish-terms-operat-q23924560

22 hours ago Some grammars allow more than one parse tree for the same token sequence. Such grammars are ambiguous. Because compilers use syntactic structure to drive translation, ambiguity is …

9.COMS 381, Summer 2005 Supplementary Handout 4 …

Url:https://www.cs.cornell.edu/courses/cs381/2005su/Handouts/Ambiguity.pdf

29 hours ago Ambiguous grammars are undesirable in real-life compilers for a variety of good reasons. While we cannot write a program to determine in general whether a given grammar G is ambiguous …

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9