Compiler Lab #4 - Code generation

Created by Todd Cooper & Karen Lemone

This lab will prepare you to do Project, Part 4 which uses the abstract syntax tree created in Project, part 3 to generate code.

As in the other labs, the yacc (bison) program is in the left box and input to that program is in the right box. The lex code is below.

When you press the button Edit the text and click me, the following will happen.

  1. Like the other labs, the yacc code will be submitted to bison (yacc), flex (lex), the cc compiler, and the resultant program will be run on the input.
  2. The results will be displayed on the bottom of the page.
    You might have to scroll down in your browser to see them.

Step 0 Run the input and look at the output. Note that pseudocode for a stack machine is produced.

Step 1 Change the yacc/bison grammar so that it recognizes division and will divide an expression and add that node to the AST tree.
Add an expression to the input and show that the division works.
(You should have this from the last lab.) Make the divide operation print the correct pseudo-assembler in the generate function.

Step 2 Now add code to generate pseudo assembly for the pow() funtion (^) to get the power of two numbers, adding the pow node to the ast tree (as done in the last lab).

Step 3 On to Project, part 4! Now you are a compiler designer!

Yacc/Bison Parsing Program Yacc/Bison Parsing Input
Lex Scanner Program
Edit the text above, and click on the button to see the result.

Valid XHTML 1.0 Strict