Category Archives: Compiler Construction

Compiler Construction, WS0910

We’ve made the jump from interpretation to actual compiling of expressions. To start your code-generating compiler, download the BCEL library from http://jakarta.apache.org/bcel/.

Add the library to your project build path and write an example Java program as shown in the lecture. Run the BCELifier tool that comes with the library and take a good look at the output. Refer to the BCEL manual to get a better understanding of the code. Now, modify your interpreter to initialize the bytecode generation facilities. Remove the call to your recursive interpreter. This will cause your tests to fail, so delete or ignore them for now. Write a simple test which evaluates a string which only contains a number. Include the code produced by BCELifier in your parser method. You can leave the hardcoded numerical value in the code. Check that a .class file is generated and load it using the Java ClassLoader, invoke the generated method using java.lang.reflect, and return the result produced by the generated code.

We will start to generate code from the input text in the next homework.

Compiler Construction, WS0910

Homework #4 (due Nov. 27th, see last entry!) requires you to implement the following:

  • extend the parser to handle if, else, and while statements. The new grammar rules are:
    ifStat ::= 'if' condExpr '{' block '}' ('else' '{' block '}')?
    whileStat ::= 'while' condExpr '{' block '}'
    condExpr ::= expr (bop expr)?
    bop ::= '<' | '>' | '==' | ...

    A conditional expression (condExpr) is true if its value is > 0, and false otherwise.

  • parse function declarations:
    funcDecl ::= name '(' paramDeclList ')' '{' block '}'
    paramDeclList ::= name*

    Extend your symbol table to be able to store a function declaration.

  • interpret function calls:
    factor ::= number | name | name '(' expr* ')'

    Make sure to handle parameter symbols correctly when calling functions. It should be possible to access globals from a function, but you may choose to make function execution free of side effects. Please make a comment or two about how your implementation works in the code!

Please remember to write simple test cases first (if without else, functions without parameters…). Develop iteratively, adding features as you go. Do not attempt to get everything right on first attempt…

Compiler Construction, WS0910

On the next 2 Fridays (Nov. 13th & Nov. 20th) there will be no lecture. The deadline for homework #4 will be Friday, Nov. 27th. There was no exercise today, instead the exercise covering homework #4 will be held in place of the lecture on Friday, starting 10:15!

To summarize, the next dates are:

  • Friday, Nov. 13th, 10:15 – Exercise for homework #4
  • Friday, Nov. 27th, 10:15 – Deadline for homework #4 and next lecture
Compiler Construction, WS0910

Please implement the following extensions to your interpreter:

  • identifiers longer than one character
  • floating point numbers

To do this, you have to implement a simple scanner, as demonstrated in the lecture. Create a Token class which contains information about a word. Tokens are created by the scanner using a finite automaton and then interpreted by the parser. As usual, create tests first!

I was asked what the criteria for evaluation of the homeworks are. The tests are very important, as is a consistent and readable code style. If the functionality is complete and working, and the tests make sense and cover most of the implementation, you will get full score. Comments are welcome, but not required, and I prefer meaningful method and variable names. In extreme cases, I may deduct points for code I’m not able to comprehend, so keep it simple 🙂