An Expression in Wren
Consider the following compound expression to be translated to an assembly-language program of the virtual machine:
x / ( y – 5 ) * ( z + 2 * y )
Syntax-Directed Definitions for Code Generation
The SDDs will translate a program in Wren into an assembly-language program of the virtual machine, which has a load/store architecture and two input/output commands: GET and PUT.
There are four arithmetic operations—ADD, SUB, MULT, and DIV—and three logical operations—AND, OR, and NOT.
The Expression Parse Tree
While the expression is being parsed, a parse tree is created implicitly.
The annotated parse tree of the above expression is given on the right.
The corresponding semantic rules, which generate assembly code of the virtual machine, execute when a production is used at a node in a parse tree.
The Object Program in an Assembly Language
The SDDs will be used to generate an assembly-language program as shown on the right for the above expression.
The final code includes 18 instructions and three temporary locations.
It could be further optimized by using various code optimization techniques.
Executing the Object Program to Capture the Semantics
The object program executes on the virtual machine and the execution results may reveal the semantics of the program.