OUT: Friday, April 3
DUE: Friday, April 10
After grinding away at your BigRigcompiler for the last few weeks, you are probably ready for a quick diversion.
Since Week 2 of the course, we have been using the simple acdc compiler as a work space for exploring issues and techniques for implementing a compiler. Our latest version of the compiler uses a table-driven parser and semantic actions to generate the abstract syntax tree of an ac, which is then verified for symbol usage and types before the code generator writes an equivalent dc program.
Not all compilers generate code for such a low-level machine as dc. Sometimes, we use compilers to translate programs written in one high-level langauge to another. For instance, I might want to "cross-compile" the following ac program:
i a
f b
a = 5
b = a + 3.2
p b
into an equivalent Java program:
public class sample1_ac
{
public static void main( String[] args )
{
int a;
double b;
a = 5;
b = (double) ( a ) + 3.2;
System.out.println( b );
}
}
Or -- gasp! -- into an equivalent Scheme program:
(let ((a #f))
(let ((b #f))
(begin (set! a 5)
(set! b (+ a 3.2))
(display b)
(newline))))
This isn't a particularly good Scheme program, but it is both syntactically and semantically valid. It also reflects as closely as possible an ac-style of programming. If I were really going to generate Scheme, I would prefer to generate an equivalent nearly functional program:
(let ((a 5))
(let ((b (+ a 3.2)))
(begin
(display b)
(newline))))
I can't avoid side effects for printing and sequences that include it, of course.
Why would anyone want to do this? Cross-compilation is a common technique for developing programs on a platform with a limited number of development tools. For example, many machines have a C compiler even when they don't have ac, Ada, or Scheme environments. In the case of Scheme as a target language, consider what you get. ac is statically typed, whereas Scheme is not. By writing programs in ac and then translating to Scheme, we can be more certain that our Scheme programs do not contain run-time errors that result from bad typing.
These programs are not hard to generate from the abstract syntax of an ac program.
The acdc compiler is written in Java. If you do not know Java or are otherwise uncomfortable about attempting this assignment in Java, please see me immediately for an alternative.
Download this code as the starting point for your homework:
It can be any language you like, so long as I can compile and execute it on the CNS system without having to install any new stuff.
Be sure to compile and run the generated code to verify that it compiles and runs! Use the ac source files in the code package to test your solution.
For the scanner and parser components of the compiler, I have factored out a common interface for those components and factored the compiler class to use a factory method, so that I can easily plug in a new scanner or parser. You may do so for the code generator, if you want, or just hard-code your new objects into copies of the current classes.
If you attempt any extra credit (see below), be sure that your README describes what you've done, too.
By 12:00 noon on Friday, April 10, submit the following: