Computer Science Department
Programming Methods Laboratory

Compilation     winter 99/00
Ecole Polytechnique Federale de Lausanne
Exercises
29.10.1999

1) The following grammars define regular languages. Give a single EBNF rule without recursion for every grammar. [5 credit points]
  • A = A a | A b | c
  • A = A a b | c
  • A = a A | A b | c
  • A = A a | B b B
    B = B b | c
2) Find an EBNF grammar without recursion that specifies the syntax of floating point numbers. The following numbers should be in the language defined by your grammar: [5 credit points]

    -3.1415927, 1.4e10, 127.0e-2, -0.1e-1

Integer numbers like 1, 4589 or -000335 (i.e. numbers without dots in the representation) need not be covered by your grammar.

3) List all the errors in this small J0 program. For each error state clearly why it is erroneous and whether it is a syntactic or a semantic error. [5 credit points]
module Star {
    Star[][] trek;
    
    int[][] enterprise() {
        while (false);
        { return newarray int[]("trek"); }
    }
    
    int voyager(int x, y) {
        if x > y
            int z = 2;
        else
            return enterprise()[-!y];
        return !-5;
    }
}
4) Write a small J0 module with a function
    int scalarProduct(int[] xs, int[] ys, int n)
for multiplying two vectors xs and ys with size n (implement the standard scalar product). [4 credit points]

5) In the following module, function main is called with parameter 2. What value does it return? [3 credit points]
module Magic {
    int i;
    int[] js;
    
    int[] init() {
        js = newarray int(2);
        js[0] = 1;
        js[1] = 2;
        i = 2;
        return js;
    }
    
    int foo(int[] ts, int j) {
        return ts[i - 1] * j;
    }
    
    int bar() {
        js[0] = 0;
	return 1;
    }
    
    int main(int i) {
        int[] ts;
        ts = init();
        i = 1;
        ts[i - 1] = 7;
        ts[1] = 6;
        if (init()[0] | bar())
            return js[i - 1] * foo(ts, ts[i - 1]);
        else
            return js[i] * (6 + i);
    }
}


Please return your solutions to the box in INR, third floor, next to room 321 until Friday, 05.11.99, 12:00. It is also possible to return your solutions in the next tutorial.

Tutorial for Programming Language Implementation, 29.10.99