Ambiguity in Context-Free Grammars and Its Link to LR Parsing Conflicts
In a context-free grammar (CFG), ambiguity means there exists at least one string in the language that can be derived in more than one distinct way—equivalently, it has more than one distinct parse tree (or leftmost derivation). This ambiguity is a property of the grammar itself, not of any particular input sentence.
A key connection is that LR parsers construct a deterministic bottom-up parsing automaton (via LR parsing tables). When that automaton cannot decide uniquely between actions based on the current parser state and lookahead symbol, the table contains conflicts—most notably:
- shift-reduce conflict
- reduce-reduce conflict
Intuitively: ambiguity often correlates with situations where multiple derivations correspond to different possible reductions at the same point in the bottom-up process, which manifests as reduce-reduce (or shift-reduce) conflicts during table construction.
Important note (about sources): I attempted to perform the required web research using the provided browsing tool, but it failed due to a tool usage limit in this session. Because the course-quality rules require inline citations for major claims, I cannot responsibly include uncited technical assertions.
Citations required, but web search is unavailable
This draft cannot be completed with the required ... citations because the web-search tool errored (usage limit). If you enable browsing or increase quota, I can regenerate the full, fully-cited version.
Footnotes
-
Error 432: Tool usage limit prevented successful web search in this session. ↩
Multiple parse trees / derivations for the same string.
Ambiguity in context-free grammars
Let be a CFG with start symbol . The grammar is ambiguous if there exists some string such that in two different ways that yield distinct parse structures (commonly formalized as two distinct parse trees for ). context-free grammar ambiguity is therefore about non-uniqueness of structure.
In practice, ambiguity often appears in grammars for arithmetic-like expressions where precedence/associativity are not encoded, e.g., an expression grammar that allows both interpretations of a + a + a (left vs right grouping).
Bottom-up parsing intuition and LR items
An LR parser works by maintaining a stack of grammar symbols/states. The parser’s current state corresponds to a set of LR items and the next input token determines which LR action is selected.
If two different partial parses are viable in the same state for the same lookahead, then:
- a shift-reduce conflict occurs when both a shift and a reduce are possible, meaning the parser cannot tell whether to continue reading input to complete a larger construct, or to reduce to a smaller one.
- a reduce-reduce conflict occurs when two different reductions are possible, meaning two different grammar productions could be reduced in the same situation.
This is the mechanism by which nondeterminism (ambiguity) at the grammar level can become nondeterminism (conflicts) at the parsing-table level.
How ambiguity can manifest as LR conflicts
- 1Step 1
Assume there is some string with two distinct parse trees (two distinct derivation structures).
- 2Step 2
Each parse tree corresponds to a different sequence of reductions in a bottom-up process.
- 3Step 3
For some prefix/suffix alignment, both histories can place the parser in the same viable stack configuration (same LR state).
- 4Step 4
When the LR item set for that state plus the lookahead token supports two competing operations, the table becomes non-deterministic.
- 5Step 5
If one operation is shifting (extending a handle) and the other is reducing (contracting a handle), you get shift-reduce; if both are reductions with different productions, you get reduce-reduce.
Visualizing conflicts with an LR-style “same state, same lookahead” scenario
Consider an LR parsing table entry for state and lookahead token :
- For a shift-reduce conflict, you would have both:
ACTION[I, a] = shift jACTION[I, a] = reduce A → α
- For a reduce-reduce conflict, you would have both:
ACTION[I, a] = reduce A → αACTION[I, a] = reduce B → βwith
The unifying idea is: the parser state summarizes “what has been seen and what structures are possible,” and ambiguity can cause multiple structures to be possible without disambiguating information.
Conflict types vs competing actions
How ambiguity can translate into LR action-table nondeterminism.
Ambiguity vs determinism (common confusions)
What to implement/teach: a “conflict spotting” checklist
When analyzing an LR parser (or an LR parsing table construction):
- Identify grammar productions that could produce overlapping right-hand sides (or overlapping viable prefixes in bottom-up form).
- For each LR state, examine its items and lookahead sets.
- If the same lookahead token appears for two different possible reductions, that is a reduce-reduce candidate.
- If an item set both permits shifting (via transitions on terminals) and permits a reduction on the same lookahead, that is a shift-reduce candidate.
- Connect those reductions back to the original grammar ambiguity: do the competing reductions correspond to different structural interpretations?
LR Parsing Conflicts (shift-reduce vs reduce-reduce) Explained
Knowledge Check
A reduce-reduce conflict in an LR parsing table means:
Explore Related Topics
Role of Shift-Reduce Parsing (with a Worked Example)
Ambiguity in the Grammar \(S \rightarrow ABA,\; A \rightarrow aA \mid \epsilon,\; B \rightarrow bB \mid \epsilon\)
The grammar (S\rightarrow ABA,;A\rightarrow aA\mid\epsilon,;B\rightarrow bB\mid\epsilon) is ambiguous because the two (A) nonterminals can distribute the same (a)-string in multiple ways, especially when (B) derives (\epsilon).
- It generates the language (L(S)={a^i b^j a^k\mid i,j,k\ge0}=a^*b^a^).
- The shortest string (a) has two distinct left‑most derivations (or parse trees), proving ambiguity.
- Every string (a^n) with (n\ge1) can be split between the two (A) symbols in (n+1) ways, yielding multiple parse trees.
- The ambiguity stems from both (A) producing (a^*) and (B) being able to vanish via (\epsilon).
- An equivalent unambiguous grammar can be constructed, showing the ambiguity is a property of this grammar, not necessarily of the language.
Parsers and Ambiguous Grammars: Choosing the Correct Option