This assignment makes use of the files contained in this zip file. This assignment is due Thursday, May 8 (and no late assignments will be accepted).
This assignment is based on Language_8. You will add "static analysis" to Language_8.
A common kind of programming error is to reference a variable that has not been declared previously in a program (for example, you might misspell a variable name when you are trying to reference it) or to re-declare a variable in the same scope. An interpreter is not a very good tool for finding these kinds of errors. First of all, there are parts of the program text that the interpreter might rarely see. For example, the interpreter may not traverse the else-clause of an if-expression because the else-clause is meant to handle a rarely occurring error condition. A second reason an interpreter is bad for finding undeclared (and re-declared) variables is that the interpreter may not find one until after the program has been running for a long time. The resulting error, and the halting of the interpreter, will find the undeclared or re-declared variable, but at the cost of much wasted time.
In a statically scoped language, it is possible to find all undeclared variables in a program without having to run the program (this is not true for dynamically scoped languages; for those languages, you must execute the program to find undeclared variables). In this assignment you will write a "static analysis" program that finds all undeclared variable references in a program and all re-declarations of previously declared variables..
In the zip file there is a file StaticAnalysis.java
that is supposed to perform a static analysis for Language_8
. You need to complete this program so that it can find all references to undeclared variables and all re-declarations of existing variables (in the same scope).
The StaticAnalysis.java
program should be structured in the same way as the Parser.java
, Evaluate.java
, and AST2Infix.java
programs are structured. They all use a "syntax driven" structure with one method for every production in the grammar (so the grammar of the language becomes a table of contents to the code). The methods in StaticAnalysis.java
should have names like analyzeApply()
(in place of getApply()
, evaluateApply()
, and convertApply()
from the parser, interpreter, and infix formatter, respectively). The methods in StaticAnalysis.java
should all be void
methods (they do not produce, or manipulate, any data structures). They should just print, to stderr, error messages when they find errors. The methods should not throw any exceptions. Whenever a method detects an error, it should print the appropriate error message, and then continue to analyze the remaining code (similar to how a compiler does not stop when it finds a compile time error).
When you have finished implementing StaticAnalysis.java
, the file Tests.java
should compile and run. The output from running this program should look exactly like the file Tests_output.txt
.
Notice how the error messages in Tests_output.txt
contain the token numbers of variables. This is so that the error messages are unambiguous. I have modified the Tokenizer.java
, Tree.java
, and Parser.java
files so that each token in the parse tree contains its token number. Use the getTokenNumber()
method from the Tree
class to get a token's number.
Turn in a zip file called CS502Hw6Surname.zip
(where Surname
is your last name) containing your version of StaticAnalysis.java
.
This assignment is due Thursday, May 8 (and no late assignments will be accepted).