Programming Languages and Compilers

 

Lecture 4

 

In this lecture we will look a bit more at programming language syntax issues and programming language design in general. We will also look at the JavaCC compiler compiler. This tool can help you generate (at least the front-end of) recursive decent compilers. JavaCC is akin to the famous Lex and Yacc tools, but simpler to use.

 

The slides for this lecture can be found here and here.

 

Literature

 

Pratt and Zelkowitz, chapter 2 and chapter 3

 

and

 

the article in Java World: “Build your own language with JavaCC”, by Oliver Enseling, which can be downloaded from http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-cooltools_p.html

 

As background reading I will recommend you read:

 

The JavaCC FAQ:

http://www.engr.mun.ca/~theo/JavaCC-FAQ/javacc-faq.htm

 

The JavaCC documentation:

http://www.webgain.com/products/java_cc/

 

You can download a free copy of JavaCC from the above website.

 

There is a repository of grammars for languages, including Java and SQL on the below URL:

 

http://www.cobase.cs.ucla.edu/pub/javacc/

 

The Java Tree Builder tool can be found on

 

http://www.cs.purdue.edu/jtb/

 

Exercises

 

Exercises for lecture 4 will be done from 8.15 till 10.00 before Lecture 5 on Wednesday the 12th of March.

 

  1. Do exercise 3 in Chapter 2 of Pratt and Zelkowitz on page 68 for Unix and the JVM. To solve this exercise you may want to consult the web site http://cne.gmu.edu/itcore/virtualmachine/index.htm

 

  1. Do exercise 5 in Chapter 2 of Pratt and Zelkowitz on page 68.

You may consider the following Java code

 

public static int increment(int x) {

            return x + 1;

}

 

What is the binding time of

        Value of argument x?

        Set of values of argument x?

        Type of argument x?

        Set of types of argument x?

        Properties of operator +?

 

  1. What is the scope of the various x’s in the following C program?

You may want to compile and execute the program.

 

//StaticScopingExample;

#include<stdio.h>

 

int x;

 

void Proc1(void)

{

  int x;

   

  x = 3;

  printf("In Proc1: x = %d\n", x);

}

 

void Proc2(void)

{

      x = 9;

    printf("In Proc2, x = %d\n", x);

}

 

int main(void)

{

  x = 1;

  printf("Before Proc1, x = %d\n", x);

  Proc1();

  printf("After Proc1, x = %d\n", x);

  Proc2();

  printf("After Proc2, x = %d\n", x);

  return 0;

}

 

  1. What is the scope of the various x’s in the following C Program?

You may want to compile and execute the program.

 

//DynamicScopingExample;

 

#include <stdio.h>

 

int x;

 

void Proc1()

{

    x = 1;

}

 

void Proc2()

{

  int x;

  x = 2;

  printf("In Proc2 before Proc1, x = %d\n", x);

  Proc1();

  printf("In Proc2 after Proc1, x = %d\n", x);

}

 

int main(void)

{

      x = 3;

      printf("Before Proc2, x = %d\n", x);

      Proc2();

      printf("After Proc2, x = %d\n", x);

      Proc1();

      printf("After Proc1, x = %d\n", x);

      return 0;

}

 

  1. Download and install JavaCC. Look at the file Calc2i.jj  in the examples/SimpleCalculator directory. Copy the file to an empty directory. Run Javacc on the file. Look at the .java files. Run javac *.java and java Calc2i

 

  1. Look at the files eg1.jjt and eg4.jjt in the examples/JJTreeExamples directory. Copy the files to two new directories. Run jjtree on each of the files and look at the generated .jj files and .java files. The run javacc on the .jj file and javac *.jj. Then run java eg1, resp. java eg4