|
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.
Here are semantic rules for a simple (ambiguous) expression grammar.
Production | Semantic rule |
---|---|
E → n | E.val = n.val |
E → E1 + E2 | E.val = E1.val + E2.val |
E → E1 * E2 | E.val = E1.val * E2.val |
E → ( E1 ) | E.val = E1.val |
|