Project 4 (Scanning and Parsing with JavaCC)
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Project 4 (Scanning and Parsing with JavaCC)
Goal
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, for, break, and continue statements.
6. Support exception handlers.
7. Support interface type declaration.
Grammars
The lexical and syntactic grammars for j-- and Java can be found at https://www.cs.umb.edu/j--/grammar.pdf |.
Download the Project Tests
Download and unzip the tests | for this project under $j/j-- .
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).
Run the following command inside the $j/j-- directory to compile the j-- compiler with your changes.
· ~/workspace/j--
$ ant
Part I: Additions to JavaCC Scanner
To scan your j-- programs using the JavaCC scanner, you need to run the javaccj-- command as follows:
· ~/workspace/j--
$ bash . / bin / javaccj - - - t project4 / XYZ . java
which only scans XYZ .java and prints the tokens in the program along with the line number where each token appears. The
file project4/XYZ .tokens provides the reference (ie, expected) output.
Problem 1. (Multiline Comment) Add support for multiline comment, where all the text from the ASCII characters /* to the ASCII characters */ is ignored.
Directions:
❼ Using the rules for single line comment as a model, write down rules for scanning a multiline comment.
Problem 2. (Operators) Add support for the following operators.
? >> | |
: >>= |= |
~ >>> || |
!= >>>= & |
/ >= &= |
/= << |
-= *= <<= < |
% ^ |
%= ^= |
Directions:
❼ List the operators in j-- .jj .
Problem 3. (Reserved Words) Add support for the following reserved words.
break case catch continue default do
double finally for implements interface long
switch throw throws try
Directions:
❼ List the reserved words in j-- .jj .
Problem 4. (Literals) Add support for long and double literals (just decimal).
Directions:
❼ Using the regular expressions for the currently supported literals as a model, write down regular expressions for scanning
long and double literals.
Part II: Additions to JavaCC Parser
To parse your j-- programs using the JavaCC parser, you need to run the javaccj-- command as follows:
· ~/workspace/j--
$ bash . / bin / javaccj - - -p project4 / XYZ . java
which will only parse XYZ .java and print the AST for the program. The file project4/XYZ .ast provides the reference (ie, expected) output.
Problem 5. (Long and Double Basic Types) Add support for the long and double basic types.
AST representation(s):
❼ JLiteralLong .java
❼ JLiteralDouble .java
Directions:
❼ Modify j-- .jj to parse longs and doubles.
Problem 6. (Operators) Add support for the following operators.
!= <<= / & |
/= < % + |
-= ^= << |
*= |= >> |
%= || >>> |
>>= &= ~ |
>>>= ++ | |
>= -- ^ |
AST representation(s):
❼ -= : JMinusAssignOp in JAssignment .java
❼ *= : JStarAssignOp in JAssignment .java
❼ /= : JDivAssignOp in JAssignment .java
❼ %= : JRemAssignOp in JAssignment .java
❼ |= : JOrAssignOp in JAssignment .java
❼ &= : JAndAssignOp in JAssignment .java
❼ ^= : JXorAssignOp in JAssignment .java
❼ <<= : JALeftShiftAssignOp in JAssignment .java
❼ >>= : JARightShiftAssignOp in JAssignment .java
❼ >>>= : JLRightShiftAssignOp in JAssignment .java
❼ / : JDivideOp in JBinaryExpression .java
❼ % : JRemainderOp in JBinaryExpression .java
❼ | : JOrOp in JBinaryExpression .java
❼ ^ : JXorOp in JBinaryExpression .java
❼ & : JAndOp in JBinaryExpression .java
❼ << : JALeftShiftOp in JBinaryExpression .java
❼ >> : JARightShiftOp in JBinaryExpression .java
❼ >>> : JLRightShiftOp in JBinaryExpression .java
❼ || : JLogicalOrOp in JBooleanBinaryExpression .java
❼ != : JNotEqualOp in JBooleanBinaryExpression .java
❼ >= : JGreaterEqualOp in JComparison .java
❼ < : JLessThanOp in JComparison .java
❼ ~ : JComplementOp in JUnaryExpression .java
❼ ++ : JPostIncrementOp in JUnaryExpression .java
❼ -- : JPreDecrementOp in JUnaryExpression .java
❼ + : JUnaryPlusOp in JUnaryExpression .java
Directions:
❼ Modify j-- .jj to parse the operators, correctly capturing the precedence rules by parsing the operators in the right
places.
❼ Update statementExpression() in j-- .jj to include post-increment and pre-decrement expressions.
Problem 7. (Conditional Expression) Add support for conditional expression (e ? e1 : e2).
AST representation(s):
❼ JConditionalExpression .java
Directions:
❼ Modify j-- .jj to parse a conditional expression.
Problem 8. (Do Statement) Add support for a do statement.
AST representation(s):
❼ JDoStatement .java
Directions:
❼ Modify j-- .jj to parse a do statement.
Problem 9. (For Statement) Add support for a for statement.
AST representation(s):
❼ JForStatement .java
Directions:
❼ Modify j-- .jj to parse a for statement.
❼ If forInit() is looking at a statement expression, then it must return a list of statement expressions. Otherwise, it must
return a list containing a single JVariableDeclaration object encapsulating the variable declarators.
Problem 10. (Break Statement) Add support for a break statement.
AST representation(s):
❼ JBreakStatement .java
Directions:
❼ Modify j-- .jj to parse a break statement.
Problem 11. (Continue Statement) Add support for a continue statement.
AST representation(s):
❼ JContinueStatement .java
Directions:
❼ Modify j-- .jj to parse a continue statement.
Problem 12. (Switch Statement) Add support for a switch statement.
AST representation(s):
❼ JSwitchStatement .java
Directions:
❼ Modify j-- .jj to parse a switch statement. After parsing SWITCH parExpression LCURLY , parse zero or more occurrences of a
switchBlockStatementGroup , and then scan an RCURLY .
❼ In switchBlockStatementGroup() , after parsing one or more occurrences of switchLabel , parse zero or more occurrences of a
blockStatement .
Problem 13. (Exception Handlers) Add support for exception handling, which involves supporting the try , catch , finally ,
throw , and throws clauses. Note that there has to be a finally clause if there are not catch clauses.
AST representation(s):
❼ JTryStatement .java
❼ JThrowStatement .java
Directions:
❼ Modify j-- .jj to parse a try statement, a throw statement, and the throws clause in constructor and method declarations.
Problem 14. (Interface Type Declaration) Implement support for interface declaration.
AST representation(s):
❼ JInterfaceDeclaration .java
Directions:
❼ Modify j-- .jj to parse an interface declaration and the implements clause in class declaration.
Files to submit:
1. j-- .jj
2. TokenInfo .java
3. Scanner .java
4. Parser .java
5. JBinaryExpression .java
6. JUnaryExpression .java
7. notes .txt
Before you submit your files, make sure: ❼ Your code is adequately commented and follows good programming principles. ❼ You update the notes .txt file. |
2023-03-17