Programming Assignment 2
CS 31600
Programming Languages
Spring, 2023
This assignment makes use of the files contained in this zip file.
This assignment is due Thursday, March 9.
In this assignment you will write three recursive descent parsers for a grammar that uses most of the integer operators from the C++ language.
Here is the grammar.
E ::= Or ( '||' Or )* // left associative
Or ::= Eq ( '&&' Eq )* // left associative
Eq ::= R ( ( '==' | '!=' ) R )* // left associative
R ::= A ( ( '<' | '<=' | '>' | '>=' ) A )* // left associative
A ::= T ( ( '+' | '-' ) T )* // left associative
T ::= F ( ( '*' | '/' | '%' ) F )* // left associative
F ::= ( '+' | '-' | '!' ) F // unary plus, minus, not operators, right associative
| B [ '**' F ] // binary exponentiation operator, right associative
B ::= '(' E ')'
| N
N ::= N D
| D
D ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
This grammar is similar to the expression grammars that we worked with in class. It has more operators and more levels of precedence than the grammars from class, but this grammar can be parsed using the same techniques that we used in class.
In the zip file there are three Java files that you need to complete.
Hw2_Parse2Value.java
Hw2_Parse2AST.java
Hw2_Parse2Syntax.java
The first file parses an expression to its integer value. The second file parses an expression to its expression tree data structure. The third file parses an expression to its parse tree data structure. Each of these three files is similar to the code from the corresponding examples that we worked with in class.
When you download the homework zip file, the versions of the three Java programs contain stub implementations of all the parser "get" methods. This lets the files be compilable and runnable. It helps you write your code incrementally. You can implement the "get" methods in a grammar from either the bottom up or the top down, and you can compile and test each method, one at a time, as you write them, without having to write all of the methods at once.
When you write the "get" methods for the Hw2_Parse2Value
parser, make sure that the boolean operators always return 0 or 1. So, for example, the value of 4 || 0
should be 1, the value of 4 && 0
should be 0, the value of !2
should be 0, and the value of 2 < 3
should be 1.
Each of the three Java programs listed above contain a lot of test expressions. In the homework zip file there are three text files, Hw2_Parse2Value_output.txt
, Hw2_Parse2AST_output.txt
, and Hw2_Parse2Syntax_output.txt
, that contain the output that your programs should produce for the test cases. In addition, the zip file contains two sub folders, hw2_ast
and hw2_syntax
, that contain the expression tree and parse tree pictures that your programs should produce for the test cases.
In the zip file there is a folder called demo
that contains a version of this assignment that you can run interactively. In the demo
folder there is an executable jar file that runs an interactive command-line version of the three parsers (a REPL for the grammar). Run the program and at its prompt type any valid expression from the grammar. The REPL will reply with the value of the expression, and the REPL will also produce, in its folder, picture files for the expression tree and the parse tree. You can check the value of the expression you entered by using the online C++ Tutor visualizer.
Here is why the interactive demo and the C++ Tutor are helpful in this assignment. The grammar uses the subset of C++ operators that operate on the integer data type. This includes the boolean operators since C++ treats the boolean value false
as the integer value 0
and it treats any non-zero integer value as equivalent to the boolean value true
. This means that you can mix up boolean and arithmetic operators in bizarre (and meaningless) ways. So, for example, this is a legal C++ expression.
4 + 5 && 6 < 0 > 9 != 5 + 2 || !-42
Here is that expression entered into the C++ Tutor. You can also enter this expression into the demo program (or into your own programs when they are completed). The C++ Tutor tells you this expression's value, but the demo program shows you how C++ actually parsed and executed the expression. The C++ Tutor and the demo program are tools that you can use to verify the correctness of your code.
After you complete the above three parser programs, you can build a copy of the demo program. Included in the hw zip file is the program Hw2_demo.java
. When you compile and run this program, it launches the interpreter command-line. In fact, you can compile and run this program even before you make any changes in the above three parsers. The stub functions in those parsers will interpret expressions that are just numbers (and only expressions that are just numbers). As you incrementally add code to the three parsers, you can even use the interpreter to test your code on the expressions that your parsers can handle.
To create the pictures of the trees you need the Graphviz program. If you haven't yet, you should install Graphviz on your computer. You can use one of Graphviz's installer programs, or you can download and unzip the "portable" version of Graphviz. The programs in this assignment assume you have Graphviz installed in the folder C:\Graphviz
.
Your code should compile without any warnings. These are programs that use Java generics. Getting generic code to compile without warnings can be tricky. But the warnings always mean that you are doing something wrong (they are really errors, not warnings). So try to figure out what is causing each warning (it is usually because you are either forgetting a type parameter or using the wrong type parameter). Ask me questions if you get stuck.
Do not change the package structure of this code. The tree implementation is in the tree
package. The tokenizer is in the tokenizer
package. The three programs listed above are in the "default" (unnamed) package. Don't change that. Do not put those three files into some named package.
Turn in a zip file called CS316Hw2Surname.zip
(where Surname
is your last name) containing only your versions of the three Java files listed above. Please be sure to put your name in each of your files.
This assignment is due Thursday, March 9.