Semantics lab for the compiler course

by Todd Cooper & Karen Lemone

In this lab you will add semantic actions to create an abstract syntax tree.

The yacc (bison) program is in the left box and input to that program is in the right box. The lex code is the bottom box.

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

  1. The web page will be submitted to bison (yacc) and flex (lex), to create a C program.
  2. This C program is compiled by the cc compiler.
  3. The resultant executable program is run on the input.
  4. Like the other labs, the results will be displayed on the bottom of the page. (You might have to scroll down in your browser to see them.)

Step 1 Edit the input, removing all but the first statement, 1 + 1;. Click on the button and examine the output. Now (on paper), show a trace of how the bottom-up parse creates the abstract syntax tree:

Tree for Lab
The printtree function prints this as: (+ 1 1)

Step 2 Change the yacc/bison grammar so that it recognizes division and add that node to the AST tree.
Run an expression as input to show that the division works.

Step 3 Add the pow() function (^) to compute the power of two numbers; include both the syntax (the yacc productions) and semantics (the $$ = stuff)
Again, run an example to show power works.

Step 4 Now read (yes, read it first!) and begin Project Part 3. Have fun!

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