Questions tagged [ambiguity]

Questions about ambiguity in context-free grammars.

A context-free grammar is ambiguous if some word has two different parse trees. Often this is undesired: for example, we would like $a + b \times c$ to be read as $a + (b \times c)$ rather than $(a+b) \times c$.

In parsing, ambiguity is removed through operator precedence and associative rules (does an operator associate to the left, like addition, or to the right, like powering).

Some classes of context-free grammars, for example the LL(1) grammars used in parsing.

Some languages are inherently ambiguous, that is, any context-free grammar for the language will be ambiguous. The standard example is $\{a^nb^nc^m\} \cup \{a^nb^mc^m\}$.

115 questions
34
votes
4 answers

How to prove that a grammar is unambiguous?

My problem is how can I prove that a grammar is unambiguous? I have the following grammar: $$S → statement ∣ \mbox{if } expression \mbox{ then } S ∣ \mbox{if } expression \mbox{ then } S \mbox{ else } S$$ and make this to an unambiguous grammar, I…
user1594
  • 541
  • 2
  • 6
  • 9
31
votes
6 answers

Why are ambiguous grammars bad?

I understand that if there exist 2 or more left or right derivation trees, then the grammar is ambiguous, but I am unable to understand why it is so bad that everyone wants to get rid of it.
Turing101
  • 1,230
  • 2
  • 16
  • 32
17
votes
2 answers

When is the concatenation of two regular languages unambiguous?

Given languages $A$ and $B$, let's say that their concatenation $AB$ is unambiguous if for all words $w \in AB$, there is exactly one decomposition $w = ab$ with $a \in A$ and $b \in B$, and ambiguous otherwise. (I don't know if there's an…
15
votes
2 answers

How do C compilers distinguish casting from grouping?

In the C language, there's the 2 similar-in-form but distict-in-meaning constructs: (type)value which is a casting, and (value), which is a grouping. How do a $LR(1)$ parser deal with this? I can think of a compiler that breaks syntax and semantic…
DannyNiu
  • 402
  • 3
  • 14
10
votes
1 answer

How do I reconstruct the forest of syntax trees from the Earley vector?

Using the Earley vector as a recognizer is quite straightforward: when the end of the string is reached, you just have to check for a completed axiomatic production started at position 0. If you have at least one, then the string is accepted. Using…
9
votes
2 answers

Is there a different resolution of the "dangling else" problem other than "match closest"?

The following context-free grammar presents a "dangling else" type ambiguity (imagine that $a$ stands for if expr then and $b$ stands for else and $c$ stands for some other kind of instruction or block): $$ \begin{aligned} S &\rightarrow aSbS \;|\;…
Gro-Tsen
  • 393
  • 2
  • 13
8
votes
2 answers

Inherent ambiguity of the language $L_2 = \{a^nb^mc^m \;|\; m,n \geq 1\}\cup \{a^nb^nc^m \;|\; m,n \geq 1\}$

I went through a question asking me to choose the inherently ambiguous language among a set of options. $$L_1 = \{a^nb^mc^md^n \;|\; m,n \geq 1\}\cup \{a^nb^nc^md^m \;|\; m,n \geq 1\}$$ $$and$$ $$L_2 = \{a^nb^mc^m \;|\; m,n \geq 1\}\cup \{a^nb^nc^m…
Shashwat
  • 513
  • 2
  • 6
  • 14
7
votes
1 answer

Why is $\{a^n b^m c^p: n\neq m\} \cup \{a^n b^m c^p: m\neq p\}$ an inherently ambiguous language?

I came across a very hard interview question in last month’s Ph.D. entrance exam. It was asking which one of the languages is inherently ambiguous. Short answer says 2). Why is the language in 2) an inherently ambiguous language? 1) $L = \{a^n…
7
votes
2 answers

Proof that there is unambigous grammar for every regular language

How can I prove, or where can I find proof that for every regular language there is unambigous grammar?
Josko
  • 73
  • 1
  • 3
6
votes
1 answer

Hardness of ambiguity/non-ambiguity for context-free grammars

A grammar is ambiguous if at least one of the words in the language it defines can be parsed in more than one way. A simple example of an ambiguous grammar $$ E \rightarrow E+E \ |\ E*E \ |\ 0 \ |\ 1 \ |\ ... $$ because the string 1+2*3 can be…
6
votes
4 answers

Big Oh vs Big Theta

I mathematically understand $f(n) \in O(g(n))$ : $f(n)$ does not grow faster than $g(n)$. More formally, $\exists c, n_0$ s.t. $f(n) \leq cg(n) \forall n \geq n_0$. Similarly, $f(n) \in \Theta(g(n))$ means that $f(n)$ grows approximately as fast as…
iart
  • 243
  • 3
  • 6
6
votes
1 answer

Language of ambiguous words

Consider an ambiguous context-free grammar $G$. Define $A(G)$ the set of ambiguous words, meaning: $$A(G) = \{u\in L(G) \mid u \text{ has at least two derivation trees for }G\}$$ Can we say something about $A(G)$ in the general case? In particular,…
Nathaniel
  • 18,309
  • 2
  • 30
  • 58
6
votes
1 answer

Are these special (one production) Context-Free Grammars always unambiguous?

Consider the following (Context-Free) Grammars with only one production rule (not including the epsilon production): $S \rightarrow aSb\;|\;\epsilon$ $S \rightarrow aSbS\;|\;\epsilon$ $S \rightarrow aSaSb\;|\;\epsilon$ $S \rightarrow…
5
votes
2 answers

unambiguous grammar that produce equal number of a and b

is there any unambiguous grammar on alphabet={a,b} that can produce strings which have equal number of a and b (e.g. "aabb" , "baba" , "abba") ?
mmsamiei
  • 71
  • 2
  • 5
5
votes
1 answer

What precisely is infinite ambiguity in a grammar?

From what I've read, an example of infinite ambiguity is usually given in a form of a loop: $S \rightarrow aA \\ A \rightarrow B \\ B \rightarrow A \\ B \rightarrow b$ But a grammar is called ambiguous if there's more than 1 way to derive the input…
Jakub Lédl
  • 233
  • 1
  • 8
1
2 3 4 5 6 7 8