Category Archives: Compiler Construction

Compiler Construction, WS0910

Please follow the instructions given in the lecture to complete the assignment.

* Download/install Xtext, preferably using a complete Eclipse distribution from http://xtext.itemis.com/xtext/language=en/23947/downloads
* Create a new workspace and a new Xtext project
* Write a grammar corresponding to the simplified EngineeringC model shown in the lecture
* Run the generated workflow and test the parser with some EngineeringC diagrams

To hand in your homework, ZIP the Eclipse workspace which contains the Xtext project and upload the ZIP file to your CVS repository. Deadline is Friday, Feb. 5th.

Compiler Construction, WS0910

Implement code generation for functions and function calls. You should test and implement the following features:

  • Generate a function from a funcDecl, using a separate InstructionList and adrTab.
  • Generate function calls, pushing actual parameters onto the stack and invoke a previously generated function by name.

You do not need to implement parameter overloading or access to global variables from functions.

Deadline is before the next lecture, which will be held Jan. 22nd. Until then, happy holidays everyone!

Compiler Construction, WS0910

Implement code generation for if, else and while statements. You should start by reenabling your interpreter unit tests and try to fix one by one by extending your code generator. The compiler should handle boolean operators <, >, =, <=, etc. The expression compiler should generate a branch instruction if it detects a conditional expression. Afterwards, the surrounding code handling the if statement is responsible for jump target fixup to implement branching logic. Please make sure to write tests for all if/else combinations with true and false conditions. while should be simpler, but make sure to test it, too.

Compiler Construction, WS0910

We’re now ready to actually generate working code. Your task, should you accept it, is to change your interpreter to a compiler for arithmetic expressions. Generate code for the following “features”:

  • Numbers (PUSH)
  • Addition / subtraction (DADD, DSUB)
  • Multiplication / division (DMUL, DDIV)
  • Variable assignment (DSTORE)
  • Variable access (DLOAD)
  • Edit: variable assignments do NOT have to work like expressions, i.e. you don’t have to support “x = y = 5”!

A common problem with code generation is that you have to distinguish between compile time and run time. These are two separate phases, which is different from the way most interpreters work. Compile time is the first phase, in which you parse statements and generate code. You can use the JVM debugger to check this phase for errors, have a look at the symbol table and the results of the parser. When running the generated code, you cannot simply attach a debugger to it. This means that in the case of a wrong result, you have to manually check the bytecode to see what’s wrong. You do not see that stack, you have no variable names and there are no explicit control structures. You can use the Navigator view in Eclipse to open the .class files in your bin folder and see the bytecode. To make sure the bytecode is working, manually interpret it in your head. Keep track of the values on the stack by writing them on a sheet of paper. Check out the BCEL manual for help on the bytecode!

And if anybody happens to know a good bytecode debugger, please let everyone know on the mailing list…