CSCI 101: Intro to Computer Science
Lab 6.3: Generating Code
Rosetta |
---|
The code generating part of our translator program starts with the parse tree for a statement, and uses it to produce a list of PIPPIN instructions that corresponds to the original statement. Let's use Rosetta, now, to demonstrate code generation.
- Start as we did in the previous lab, by entering and setting the equation: W = X * (2 + Y) into Rosetta's equation window.
- Now, click "Code Generation" in the control panel at the bottom of the program window. Notice that the parse tree is generated immediately, since that is the starting point for code generation.
- Then, click the "Play" button to watch Rosetta produce PIPPIN instructions as it works its way through the parse tree. Examine the PIPPIN code along the right side of the program window to convince yourself that it corresponds to the original statement.
- Click the "Reset" button, and then "Step" through the code generation to see which PIPPIN statements correspond to which branches of the parse tree.
- Repeat steps 1-4 to generate code for the following equation:
X = (3 * Y) + (2 /W)
- For each of the following statements, determine - without using Rosetta!
- the PIPPIN code that would be generated for it. Then, use Rosetta to verify
your answers.
- Y = Z * 3 - X
- X = (Y + 5) / 2
- Z = (W * W) + (X * X)
- For each of the PIPPIN code segments below, find a statement that
corresponds to it. Use Rosetta to confirm your suspicions.
LOD Z DIV #2 STO X
LOD Z DIV #2 MUL Y STO X
LOD #2 MUL Y STO T1 LOD Z DIV T1 STO X