CS 3721 Programming Languages
Syntax Directed Translation


Overview: The term semantic action refers to extra code added to a parser so that it will carry out some task, often translating from the language the grammar describes to some other language. During the course of the parse, the function calls implicitly carry out a complete traversal of the parse tree, though the actual parse tree is not constructed. The tricky part is to insert extra code, so that (at run time) during the traversal, the translation will be carried out. This process is known as syntax-directed translation.


Initial Examples of Semantic Actions: Consider a recursive descent parser for the grammar giving arithmetic expressions.

Arithmetic Expressions
P ---> E '$'
E ---> T {('+'|'-') T}
T ---> S {('*'|'/') S}
S ---> F '^' S | F
F ---> '(' E ')' | char

It is easy to add extra code to this parser so that it will translate arithmetic expressions in to reverse Polish notation (RPN). So little additional code is needed that this example illustrates the power of this approach.

Here is another example of semantic actions: an evaluator of arithmetic expressions:


Further examples of semantic actions:
Revision date: 2004-11-02. (Please use ISO 8601, the International Standard.)