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.
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
Exercises
for lecture 4 will be done from 8.15 till 10.00 before Lecture 5 on Wednesday
the 12th of March.
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 +?
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;
}
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;
}