This assignment makes use of files contained in this zip file.
This assignment is due Tuesday, March 6.
In this assignment you will write a parser for the language of infix arithmetic expressions. You will write the parser in four stages, with each stage parsing a language that is more complicated than the one before.
The four languages are called Expr1
, Expr2
, Expr3
, and Expr4
. There are two parsers for each language,
ParseExpr_ParseTree.java
ParseExpr_AST.java
and one test file for each language,
TestExpr_.java
where the blank in each file name is filled in with a number from 1 to 4. The four test files are written for you. You need to complete each of the two parsers for each language. One parser creates a parse tree as it parses a string in its language, and the other parser creates an abstract syntax tree (an expression tree). When you are finished, the output from each TestExpr_.java
program should look exactly like its output file output-Expr_.txt
. In addition, the test programs create png image files of the parse trees and the expression trees. In the folder hw3/png-files/
you will find png files that show you what the output from your finished parsers should look like.
The language for each parser is described by a grammar at the beginning of each ParseExpr_AST.java
file. Language Expr1
is an expression language that has four operators that are right associative and with two levels of operator precedence. The next language, Expr2
, makes the four operators left associative (left associative is a bit harder to implement than right associative). Language Expr3
adds parentheses to the syntax. The last language, Expr4
, adds the unary negation and square-root operators and the (right associative) exponentiation operator. Language Expr4
is the same infix arithmetic expression language that was used in homework assignment 2 (Expr4
is also described in the file Infix Expression Grammar.txt
from the zip file).
In the zip file there are five source files,
BTree.java
BTree2dot.java
Tree.java
Tree2dot.java
Tokenizer.java
that are completed for you.
To get the test files to compile and run, you will need to add to the hw3
folder copies of your Evaluate.java
and InfixPrinter.java
files from homework assignment 2.
To get the test programs to run, you will also need to install Graphviz on your computer. You can download the Graphviz package using this URL.
Download and unzip the zip file, and then copy the unzipped folder to your C:\ drive. You can test your installation of Graphviz by running the parsers that we covered in class.
Here are a few suggestions for doing this assignment. Do the four expression tree parsers first, since they are a bit easier to implement than the parse tree parsers. Do the four expression tree parsers in order, from ParseExpr1.java
to ParseExpr4.java
. When you get an expression tree parser working, use it as the starting point for the next parser. You do not need to change much from one language to the next. When you have all four expression tree parsers working correctly, you should be ready to do the parse tree parsers. When you work on a parse tree parser, it helps to keep a simple expression from the language, and its parse tree, in your mind as you implement the methods in the parser. When you implement a method, look at your example parse tree (from the hw3\png-files\Expr1
directory) and ask yourself what that method needs to do to build its part of the parse tree.
Turn in a zip file called CS316Hw3Surname.zip
(where Surname
is your last name) containing your versions of the files InfixPrinter.java
and Evaluate.java
from Assignment 2, and each of the two programs ParseExpr_ParseTree.java
and ParseExpr_AST.java
for each integer 1, 2, 3, and 4. Make sure you include all 10 of these files. Be sure to put your name and email address in every file your turn in. Do not submit any of the dot or png files. I will create those when I compile and run your programs.
This assignment is due Tuesday, March 6.