Grammar has ambiguity. The same string can have multiple meanings or produce multiple parse trees.

Compilers

  • Alternation is the source of Ambiguity (e.g., the bar |)
    • Not every | causes grammatical ambiguity
    • Every grammatical ambiguity is caused by a |
  • Linguists want to model ambiguity in human languages
    • The Context-Free Grammar (CFG) idea is from linguists
  • We want to design computer languages without ambiguity

In Practice

  • If we do not specify the operator associativity properly, then we might have an ambiguous grammar.
  • Ex. Multiplication binds tighter than addition. If we do not specify the operator precedence properly, then we might have an ambiguous grammar

Fixing Precedence

  1. Each level of precedence should be its own non-terminal
  2. The lowest level of precedence should be the top level production
  3. Each production should use the next highest level of precedence