Assignment 4: Some Additional Information
The provided ejay.cup parser uses the following potentially non-obvious token IDs, for the indicated constructs:
ID Construct SEMI Data Declaration LEFT_BRKT Array Type (in type context) LEFT_BRKT Array Selection Operation (in expression context) LEFT_BRACE Struct Type (in type context) LEFT_BRACE Block (in block context) LEFT_PAREN Function Declaration LEFT_PAREN Function Call as a statement RT_PAREN Function Call in an expression COMMA Formal Parameter Declaration
Note also the important difference between type keyword IDs versus literal type IDs. Here they are:
Type Keyword Literal Value INT INTEGER FLOAT FLOATING_PT STRING STRING_LIT BOOLEAN TRUE and FALSE
In conjunction with the Error Handling requirements specified in the original writeup, you must perform the following semantic checks:
The following are specific forms of error checking that you do NOT need to do:
The following sample programs are recommended test cases for the different development phases in Assignment 4. They are simple enough to help you focus on particular aspects of the problem, one step at a time. As in the preceding assignments, the actual test data may include some test cases that are not specifically covered in these examples, so you are encouraged to expand on these examples to provide further test data of your own.
/* * This program has just three global variable decls. Run it with * EJayInterpreterNoFuncsTest to confirm that you're assigning memory locations * properly. The result should be offsets of 0, 1, and 2 for i, j, and k * respectively. */ int i,j,k;
/* * This is the simplest possible executable program. The result should be a * value of 10 in location 0 of static memory. */ int i; void main() { i = 10; }
/* * This is a slightly less simple program. The result should be a value of 10 * in location 0, 20 in location 1, and 210 in location 2. * * Before you have functions working, you should test the program with the "no * functions" version of the test program. * * After you get this program working, you should be able to test the full * range of expression evaluation. You should then proceed to test if, while, * and print statements. */ int i,j,k; void main() { i = 10; j = 20; k = i + j * 10; }
/* * This is a very simple test program for a function call. The result should * be a value of 3 in static memory location 0. Test the program with * EJayInterpreterTest.java. */ int i; void main() { f(3); } int f(int x) { i = x; }
/* * This is a program for testing function returns and local scoping. The * result should be a value of 3 in memory locations 0 and 1. */ int i,j; void main() { i := f(3); } int f(int x) { j = x; return x; }
/* * This is a program to test three levels of function call. The result should * be a value of 1 in memory location 0. */ int i; void main() { i = f(3); } int f(int x) { return g(x)-1; } int g(int x) { return x-1; }
/* * This is a simple factorial program, to test the execution of a recursive * function. The result should be a value of 24 in location 0. */ int x; void main() { x = fact(4); } int fact(int x) { if (x > 0) { return x * fact(x - 1); } else { return 1; } }
/* * This is an iterative version of factorial. The result should be a value of * 24 in location 0. */ int x; void main() { int i; i = 4; x = 1; while (i > 0) { x = i * x; i = i - 1; } }
/* * This is a version of factorial with a print. If you get print working * early, it can be helpful in debugging other EJay constructs. */ int x; void main() { x = fact(4); } int fact(int x) { print "value of x in f: ", x, "0; if (x > 0) { return x * fact(x - 1); } else { return 1; } }