11.6. Using Ambiguous Grammars

When thinking about semantics, we generally ignore syntactic issues, even including ambiguity. We don't want to get caught up in one thing while thinking about another.

So semantic rules are commonly written for ambiguous grammars, leaving how to deal with that to the parser.


Example

Here are semantic rules for a simple (ambiguous) expression grammar.

Production Semantic rule
En E.val = n.val
EE1 + E2 E.val = E1.val + E2.val
EE1 * E2 E.val = E1.val * E2.val
E → ( E1 ) E.val = E1.val