CS 50200 - Programming Assignment 5

This assignment makes use of the files contained in this zip file. This assignment is due Thursday, April 4.

This assignment is based on the languages cs50200-language-hw4 and cs50200-Language-10.

A common kind of programming error is to reference a variable that has not been declared previously in your program (for example, you might misspell a variable name when you are trying to reference it). 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 never 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 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 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.

In the zip file there is a text file cs50200-language-hw5.txt that defines the syntax of the language you are to analyze. Also in the zip file there is a file StaticAnalysis.java that is supposed to perform a static analysis for cs50200-language-hw5. You need to complete this program so that it finds all references to undeclared variables.

Also in the zip file there is a file AST2infix.java which is supposed to format strings from the language cs50200-language-hw5 in a "C like" notation that includes infix notation for arithmetic and boolean expressions. You need to complete this "translator". Your AST2infix.java program should use operator precedence to create an infix notation that uses the minimum number of needed parentheses.

The files Tokenizer.java, Token.java, Tree.java, BuildTree.java, and PrettyPrinter.java, which are in the zip file, do not need to be modified. You should use them exactly as they are.

When you have finished implementing Analyze.java and AST2infix.java, then the file Tests.java should compile and run. The output from running this program should look exactly like the file output.txt.

Notice how the error messages in output.txt contain the token number of an undeclared variable. This is so that the error messages are unambiguous. I have modified the Tokenizer.java, Tree.java, and BuildTree.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 containing your versions of StaticAnalysis.java and AST2infix.java along with the other files from the zip file.

This assignment is due Thursday, April 4.


Return to the main homework page.
Return to the CS 50200 home page.


compliments and criticisms