java -jar PA5_groupname.jar --two-pass-mips input_file
Your program should generate an AST that is printed to an input_file.ast.dot file. Next your program should generate a symbol table and an associated input_file.ST.dot file that visualizes the symbol table. Your program should also print out a dot file that helps visualize the lines and positions associated with each of the AST nodes. A visitor that checks the type usage in the program will use the symbol table to lookup type information. Finally, the code generation visitor should be extended to handle the new program constructs and generate a MIPS program in the input_file.s file. The generated MIPS program must be capable of being interpreted by the MARS interpreter.
The input_file will contain a special main construct that starts with the keywords "special" and "main" and is followed by curly braces containing zero or more variable declarations followed by zero or more statements with the assignment of boolean and integer expressions to variables, println statements, if statements, and while statements. For example, the input_file might include the following:
special main {
int a;
boolean b;
a = 1;
b = true && ! false && (a<3);
if (b && true) {
a = 42;
System.out.println(1);
} else {
a = 7;
System.out.println(0);
}
System.out.println(a*1 + 2);
while (0 < a) {
System.out.println(a);
a = a - 10;
}
}
The output from running the compiler should be
as follows:
Printing dot file to input_file.ast.dot Printing symbol table to input_file.ST.dot Printing AST with line and position info to input_file.astlines.dot Printing MARS MIPS file to input_file.sThe output from running MARS on the file input_file.s should be as follows:
MARS 3.8 Copyright 2003-2010 Pete Sanderson and Kenneth Vollmar
1
44
42
32
22
12
2
For this assignment, you can no longer assume correct input programs. The programs will be correct syntactically, but they can contain semantic errors, which your program must report. The two types of semantic errors are (1) redeclarations of variables and (2) inappropriate type usage including undeclared variables. ALL variable redeclarations should be reported. For example, the following input:
special main
{
int a;
int a;
int b;
int a;
a = 5;
b = 5;
System.out.println( a + b );
}
should result in the following output:
Printing dot file to two-redeclares.txt.ast.dot
[4,11] Redeclared symbol a
[6,11] Redeclared symbol a
Errors found while building symbol table
For inappropriate type usage, it is only necessary to report the first error.
For example, the following input file:
special main {
int b;
boolean a;
b = 3;
System.out.println(a + b * 2);
System.out.println(2 * (6 - 1) + 2 - x);
}
results in only the first type error being reported, even though the variable x is also not declared.
Printing dot file to error-op1.txt.ast.dot
Printing symbol table to error-op1.txt.ST.dot
Printing AST with line and position info to error-op1.txt.astlines.dot
[5,24] Invalid left operand type for operator +
This is a significant assignment that you should start right away.
We strongly recommend the following progression:
SpecialMain ::= "special" "main" "{" (VarDeclaration)* (Statement)* "}"
The statement and variable declaration grammar productions can be the same as those found
here.
Keep in mind that you have to create list productions as you did in the last assignment for the statement list.
The only variable types you need to handle for this assignment are the "int" and "boolean" types.
As in PA4, all of the AST nodes needed have been provided in MJControlFlowStart/src/ast/nodes. Also, the ast/analysis package has been updated.
The symbol table data structure should be able to insert and lookup the symbol table entries for symbols and print itself to a dot file. Here is a possible interface:
STE lookup(String)
void insert(STE)
void outputDot(...)
MJControlFlowStart includes the reference compiler MJ.jar for this assignment. Use the reference compiler to generate example symbol table .dot files (i.e., they are the .ST.dot files). Use any format you prefer, but some form of visualization of the symbol table with dot is required.
[LINENUM,POSNUM] Redeclared symbol SYMNAMEwhere LINENUM is the line number for the symbol, POSNUM is the position number for the symbol, and SYMNAME is the symbol name. ALL such errors should be printed to standard error, and at the end of symbol table construction if any such errors are printed then the following error should be printed to standard error and your compiler should exit:
Errors found while building symbol tableNote that since the reference compiler is provided in jar form, you can compare your output with the output from the reference MJ.jar. Also note that id tokens (i.e., the Token class) already has line and position information.
a + b - c * dassume that the variable c in the above is at line 10 and position 11. The line and position associated with the multiplication (i.e. MulExp node) should be line 10 and position 11. You may want to use the ReversedDepthFirstAdapter (in MJControlFlow/src/ast/analysis/) to implement the visitor that calculates lines and positions.
To generate the correct line and position information, you will have to modify the .lex files so as to reset the yychar variable to 0 everytime an end of line token is found.
EOL=(\r|\n|\r\n)
...
{EOL} { /* reset position counter */ yychar = 0; }
Note also that the reference compiler adds 1 to yyline when creating tokens, because the generated lexer starts the line number at 0.
"+" {return new Symbol(sym.PLUS,new TokenValue(yytext(), yyline+1, yychar));}
[LINENUM,POSNUM] Undeclared variable VARNAME
// anywhere an id token could be a variable name
[LINENUM,POSNUM] Invalid expression type assigned to variable VARNAME
// assignment statements
[LINENUM,POSNUM] Invalid left operand type for operator OP
[LINENUM,POSNUM] Invalid right operand type for operator OP
[LINENUM,POSNUM] Invalid operand type for operator !
[LINENUM,POSNUM] Invalid argument type for method println
[LINENUM,POSNUM] Invalid condition type for if statement
[LINENUM,POSNUM] Invalid condition type for while statement
where LINENUM is the line number for the symbol, POSNUM is the position number for the symbol, OP is a specific binary operator, and VARNAME is a specific variable name.
To enable easier testing and grading do not change the phrasing of the error messages. Implement the first four errors where relevant and then implement other variants of error messages three and four and the rest of the errors in phase 6.
For expressions, use the following precedence, which is listed from lowest to highest with operators and tokens listed on the same line having equal precedence:
The TestCases directory includes some test cases.
Start working through the phases as outlined above.
cs453Projects/
PA5/
Makefile
src/
...
then do the following commands:
% cd PA5
% make clean
% cd .. // do tar command in cs453Projects/ directory
% tar cvzf PA5_groupname.tgz PA5 --exclude=.svn
svn log
svn info
svnlook tree REPOSITORY_PATH svnlook must be issued on a department machine
~cs453/bin/checkin PA5 PA5_groupname.tar