Exercises on the Introduction and on Grammars

Programming Language Technology 2012
Exercise 1

1. The following C code has around six errors (some of them depend on what you count as an error). Locate the errors and explain at which compiler phase they are (or should be) revealed.

    /* this is a buggy hello world program
  
    int main ()
    {
      int i ;
      int j = k + 1 ;
      int a[] = {1,2,3}
      j = a + 6 ;
      a[4] = 7 ;
      printf(hello world\n) ; 
    }

2. Decode the following representation of a JVM program to show (a) the corresponding assembly code and (b) a Java expression from which it can be obtained.

    0001 0000 1111 1111 0001 0000 0010 0000
    0110 0000 0001 0000 0000 1001 0110 1000

All these codes can be found in Lecture 1. The full set of codes can be found in the on-line JVM specification in http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc.html

3. Show the parse tree and abstract syntax tree of the statement

    while (x > y) y = x = x + 3 ;

in the grammar constructed in Lecture 2.

4. BNFC translates the separator macro into three rules, as follows:

                                [Exp] ::= ;
    separator Exp "," ;  --->   [Exp] ::= Exp ;
                                [Exp] ::= Exp "," [Exp] ;

The resulting grammar, however, is not quite accurate. Why? Design a different translation that would solve the problem.

5. Implement a parser for Lisp in BNFC. It should be able to parse the code example in http://lib.store.yahoo.net/lib/paulgraham/jmc.lisp - which is actually a complete Lisp interpreter!