Recursive Decent Parsing:Action Symbols in the Grammar
Now that we have a parser that can recognize assignment statements, we need to add semantic actions to the parser so that useful work is done. These actions are specified by embedding semantic action symbols in the grammar productions, giving a set of rules called a translation scheme (Aho p.37-40: Translation Schemes, Emitting a Translation).
Here are some examples of action symbols in a translation scheme.First, look at the simple grammar without any action symbols:
Now, as a translation scheme, with the semantic action symbols embedded in the grammar productions:
<> => ID { put ID in symbol table if not there }
; { pop value on stack into ID }
<> => ID { look up ID; push current value on stack }
CONST { evaluate constant; push value on stack }
The action symbols are placed in the exact places we want the actions to occur. We map these locations to the corresponding locations in the code for the recursive-descent parsing functions. The value stack is used by the parser to hold values of expressions.