CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Objectives.
1. Support multiline comment.
2. Support long and double basic types.
3. Support operators.
4. Support conditional expression and switch statement.
5. Support do-while and for statements.
6. Support exception handlers.
7. Support interface type declaration.
In this project you will only modify the JavaCC specification file $j/j–/src/jminusminus/j–.jj for j– to add more Java tokens
and programming constructs to the j– language. In the first part, you will modify the scanner section of the j–.jj file to
support the Java tokens that you handled as part of Project 2 (Scanning). In the second part, you will modify the parser
section of the file to support the Java programming constructs that you handled as part of Project 3 (Parsing). To compile the
j– compiler with the JavaCC front-end, ie, with the scanner and parser generated by JavaCC, run the following command:
$ ant clean javacc compileJavaCC jar
Part I: Additions to JavaCC Scanner
To scan your j– programs using the JavaCC scanner, you need to run the javaccj– command as follows:
$ $j/j–/bin/javaccj — -t P.java
which only scans P.java and prints the tokens in the program along with the line number where each token appears.
Problem 1. (Multiline Comment) Add support for multiline comment, where all the text from the ASCII characters /* to
the ASCII characters */ is ignored.
$ $j/j–/bin/javaccj — -t tests/MultiLineComment.java
5 : public = public
5 : class = class
5 :
5 : { = {
9 : public = public
9 : static = static
9 : void = void
9 :
9 : ( = (
9 :
9 : [ = [
9 : ] = ]
9 :
9 : ) = )
9 : { = {
13 : } = }
14 : } = }
15 :
Problem 2. (Reserved Words) Add support for the following reserved words.
break case catch
continue default do
double final finally
for implements interface
long switch throw
throws try
1 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
$ $j/j–/bin/javaccj — -t tests/ReservedWords.java
1 : break = break
1 : case = case
1 : catch = catch
2 : continue = continue
2 : default = default
2 : do = do
3 : double = double
3 : final = final
3 : finally = finally
4 : for = for
4 : implements = implements
4 : interface = interface
5 : long = long
5 : switch = switch
5 : throw = throw
6 : throws = throws
6 : try = try
7 :
Problem 3. (Operators) Add support for the following operators.
? ~ != / /=
-= — *= % %=
>> >>= >>> >>>= >=
<< <<= < ^ ^= | |= || & &= $ $j/j--/bin/javaccj -- -t tests/Operators1.java 1 : ? = ? 1 : ~ = ~ 1 : != = != 1 : / = / 1 : /= = /= 2 : -= = -= 2 : -- = -- 2 : *= = *= 2 : % = % 2 : %= = %= 3 : >> = >>
3 : >>= = >>=
3 : >>> = >>>
3 : >>>= = >>>=
3 : >= = >=
4 : << = <<
4 : <<= = <<=
4 : < = <
4 : ^ = ^
4 : ^= = ^=
5 : | = |
5 : |= = |=
5 : || = ||
5 : & = &
5 : &= = &=
6 :
Problem 4. (Separators) Add support for the separator : (colon).
$ $j/j–/bin/javaccj — -t tests/Separators.java
1 : ; = ;
2 : : = :
3 : , = ,
4 : . = .
5 : [ = [
5 : { = {
2 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
5 : ( = (
5 : ) = )
5 : } = }
5 : ] = ]
6 :
Problem 5. (Literals) Add support for (just decimal for now) long and double literals.
| .
|
|
$ $j/j–/bin/javaccj — -t tests/Literals.java
1 :
1 :
1 :
2 :
2 :
2 :
3 :
3 :
3 :
4 :
Part II: Additions to JavaCC Parser
To parse your j– programs using the JavaCC parser, you need to run the javaccj– command as follows:
$ $j/j–/bin/javaccj — -p P.java
which will only parse P.java and print the AST for the program in XML format.
Note.
1. Consult Appendix C of our text for the grammar (ie, formal specification) for each new construct you will be supporting
in j–.
2. The AST shown (as XML) for each problem is only a suggestion as to what the AST ought to look like once the
syntactic constructs for that problem are implemented in j–. You are not expected to produce exactly the same AST,
but just something similar. The autograder will not match your AST against ours for correctness, but instead will test
if your parser parses our pass tests without errors and our fail tests with suitable error messages.
Problem 6. (Long and Double Basic Types) Add support for the long and double basic types.
$ $j/j–/bin/javaccj — -p tests/BasicTypes.java
3 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
4 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Problem 7. (Operators) Add support for the following operators, obeying precedence rules (see Appendix C).
~ != / /= -=
++ — *= % %=
>> >>= >>> >>>= >=
<< <<= < ^ ^= | |= || & &= $ $j/j--/bin/javaccj -- -p tests/Operators2.java
5 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
6 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
7 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
8 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Problem 8. (Conditional Expression) Add support for conditional expression (e1 ? e2 : e3).
$ $j/j–/bin/javaccj — -p tests/ConditionalExpression.java
9 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Problem 9. (Switch Statement) Add support for a switch statement.
$ $j/j–/bin/javaccj — -p tests/SwitchStatement.java
10 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
11 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
12 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Problem 10. (Do-while Statement) Add support for a do-while statement.
$ $j/j–/bin/javaccj — -p tests/DoWhileStatement.java
13 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Problem 11. (For Statement) Add support for a for statement.
$ $j/j–/bin/javaccj — -p tests/ForStatement.java
14 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Problem 12. (Exception Handlers) Add support for exception handling, which involves supporting the try, catch, finally,
throw, and throws clauses.
$ $j/j–/bin/javaccj — -p tests/ExceptionHandlers.java
15 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
16 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Problem 13. (Interface Type Declaration) Implement support for interface declaration.
$ $j/j–/bin/javaccj — -p tests/Interface.java
17 of 18
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Files to Submit
1. j–.tar.gz (j– source tree as a single gzip file)
2. report.txt (project report)
Before you submit:
• Make sure you create the gzip file j–.tar.gz such that it only includes the source files and not the binaries, which
can be done on the terminal as follows:
$ cd $j/j–
$ ant clean
$ cd ..
$ tar -cvf j–.tar j–/*
$ gzip j–.tar
• Make sure your report isn’t too verbose, doesn’t contain lines that exceed 80 characters, and doesn’t contain
spelling/grammatical mistakes
18 of 18