3. Translation Schemes Cont.
1/23/2019
3
❑ Syntax-directed definition for a BNF grammar
❑A syntax-directed translation scheme is a notation for specifying a
translation by attaching program fragments to productions in a
grammar.
❑A translation scheme is like a syntax-directed definition, except that
the order of evaluation of the semantic rules is explicitly specified.
❑ Program fragments embedded within production bodies are
called semantic actions.
❑ The position at which an action is to be executed is shown by
enclosing it between curly braces and writing it within the
production body, as in
rest -> + term {print('+') } restl
Embedded
semantic action
4. New BNF Productions and Parse Trees Using Semantic
Actions
1/23/2019
4
❑ Actions are added in the productions
❑ When drawing a parse tree
• Indicate an action by constructing an extra child for it, connected by a dashed
line to the node that corresponds to the head of the production
5. 1/23/2019
5
❑ Actions embedded in the production bodies translate such
expressions into postfix notation, provided we perform a left-to-
right depth-first traversal of the tree and execute each print
statement when we visit its leaf.
New BNF Productions and Parse Trees Using Semantic
Actions
6. 1/23/2019
6
❑ Actions embedded in the production bodies translate such
expressions into postfix notation, provided we perform a left-to-
right depth-first traversal of the tree and execute each print
statement when we visit its leaf.
Actions Translating 9-5+2 into 95-2+
New BNF Productions and Parse Trees Using Semantic
Actions: Actions Translating 9-5+2 into 95-2+