Context-Free Grammars and Parse Ambiguity

Separate the language of accepted strings from their parse structure and encode precedence by splitting nonterminals.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. A derivation expands one occurrence at a time
  2. Separate nonterminals by syntactic role

A derivation expands one occurrence at a time

A context-free production replaces one nonterminal regardless of its neighbors. With \(S\to SS\mid a\), the string \(aaa\) can group its first two symbols or its last two first. This ambiguity concerns distinct parse trees. Expanding different independent nodes in different orders can also produce different derivations of the same tree, so derivation order alone does not establish ambiguity.

Separate nonterminals by syntactic role

For arithmetic expressions, use an expression layer that combines terms by addition, a term layer for multiplication and an atom layer for literals or parentheses. Then \(a+b*c\) groups \(b*c\) as a term before addition. Left and right recursion can encode different associativity and interact differently with parser implementations. Pushdown automata relate stacks to context-free recognition. LR parsing chooses reductions from left-to-right input with explicit parser state, where an ambiguous grammar can produce conflicts.[1][2]

References

  1. John Hopcroft, Rajeev Motwani and Jeffrey Ullman, Context-Free Grammars and Languages ↩
  2. Michael Sipser, Introduction to the Theory of Computation ↩