CS451/651 Project 4 (Scanning and Parsing with JavaCC)

$30.00

Category: Tags: , , , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (6 votes)

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
See tests/MultiLineComment.tokens for output.
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
$ $j /j – -/ bin / javaccj — -t tests / ReservedWords . java
See tests/ReservedWords.tokens for output.
Problem 3. (Operators) Add support for the following operators.
? ~ != / /=
-= — *= % %=
>> >>= >>> >> >= >=
<< <<= < ^ ^=
| |= || & &=
$ $j /j – -/ bin / javaccj — -t tests / Operators . java
See tests/Operators.tokens for output.
1 of 7
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Problem 4. (Separators) Add support for the separator : (colon).
$ $j /j – -/ bin / javaccj — -t tests / Separators . java
See tests/Separators.tokens for output.
Problem 5. (Literals) Add support for (just decimal for now) long and double literals.
< int_literal > = 0 | (1 -9) {0 -9} // decimal
< long_literal > = < int_literal > (l | L )
< digits > = (0 -9) {0 -9}
< exponent > = (e | E ) [(+ | -)] < digits >
< suffix > = d | D
< double_literal > = < digits > . [< digits >] [< exponent >] [< suffix >]
| . < digits > [< exponent >] [< suffix >]
| < digits > < exponent > [< suffix >]
| < digits > [< exponent >] < suffix >
$ $j /j – -/ bin / javaccj — -t tests / Literals . java
See tests/Literals.tokens for output.
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 the appendix at the end for the grammar (ie, formal specification) for each new construct you will be supporting
in j–.
2. The AST output provided for each problem is meant to give you an idea as to what the AST ought to look like once
the syntactic constructs for that problem are implemented in j–. You are expected to implement the writeToStdOut()
method in the J* files for the constructs such that your AST output is 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.
Problem 6. (Long and Double Basic Types) Add support for the long and double basic types.
$ $j /j – -/ bin / javaccj — -p tests / BasicTypes . java
See tests/BasicTypes.ast for output.
Problem 7. (Operators) Add support for the following operators, obeying precedence rules (see appendix at the end).
~ != / /= -=
++ — *= % %=
>> >>= >>> >> >= >=
<< <<= < ^ ^=
| |= || & &=
$ $j /j – -/ bin / javaccj — -p tests / Operators . java
See tests/Operators.ast for output.
2 of 7
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
See tests/ConditionalExpression.ast for output.
Problem 9. (Switch Statement) Add support for a switch statement.
$ $j /j – -/ bin / javaccj — -p tests / SwitchStatement . java
See tests/SwitchStatement.ast for output.
Problem 10. (Do-while Statement) Add support for a do-while statement.
$ $j /j – -/ bin / javaccj — -p tests / DoWhileStatement . java
See tests/DoWhileStatement.ast for output.
Problem 11. (For Statement) Add support for a for statement.
$ $j /j – -/ bin / javaccj — -p tests / ForStatement . java
See tests/ForStatemnet.ast for output.
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
See tests/ExceptionHandlers.ast for output.
Problem 13. (Interface Type Declaration) Implement support for interface declaration.
$ $j /j – -/ bin / javaccj — -p tests / Interface . java
See tests/Interface.ast for output.
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 – czvf j – -. tar . gz j – -/*
• Make sure your report uses the given template, isn’t too verbose, doesn’t contain lines that exceed 80 characters,
and doesn’t contain spelling mistakes
3 of 7
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
Appendix: Java Syntax
compilationUnit ::= [ package qualifiedIdentifier ; ]
{ import qualifiedIdentifier ; }
{ typeDeclaration }
EOF
qualifiedIdentifier ::= { . }
typeDeclaration ::= typeDeclarationModifiers ( classDeclaration | interfaceDeclaration )
| ;
typeDeclarationModifiers ::= { public | protected | private | static | abstract | final }
classDeclaration ::= class [ extends qualifiedIdentifier ]
[ implements qualifiedIdentifier { , qualifiedIdentifier } ]
classBody
interfaceDeclaration ::= interface // can’t be final
[ extends qualifiedIdentifier { , qualifiedIdentifier } ]
interfaceBody
modifiers ::= { public | protected | private | static | abstract | final }
classBody ::= { { ;
| static block
| block
| modifiers memberDecl
}
}
interfaceBody ::= { { ;
| modifiers interfaceMemberDecl
}
}
memberDecl ::= // constructor
formalParameters
[ throws qualifiedIdentifier { , qualifiedIdentifier } ] block
| ( void | type ) // method
formalParameters
[ throws qualifiedIdentifier { , qualifiedIdentifier } ] ( block | ; )
| type variableDeclarators ; // fields
interfaceMemberDecl ::= ( void | type ) // method
formalParameters
[ throws qualifiedIdentifier { , qualifiedIdentifier } ] ;
| type variableDeclarators ; // fields; must have inits
block ::= { { blockStatemnt } }
blockStatement ::= localVariableDeclarationStatement
| statement
4 of 7
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
statement ::= block
| if parExpression statement [ else statement ]
| for ( [ forInit ] ; [ expression ] ; [ forUpdate ] ) statement
| while parExpression statement
| do statement while parExpression ;
| try block
{ catch ( formalParameter ) block }
[ finally block ] // must be present if no catches
| switch parExpression { { switchBlockStatementGroup } }
| return [ expression ] ;
| throw expression ;
| break [ ] ;
| continue [ ] ;
| ;
| : statement
| statementExpression ;
formalParameters ::= ( [ formalParameter { , formalParameter } ] )
formalParameter ::= [ final ] type
parExpression ::= ( expression )
forInit ::= statementExpression { , statementExpression }
| [ final ] type variableDeclarators
forUpdate ::= statementExpression { , statementExpression }
switchBlockStatementGroup ::= switchLabel { switchLabel } { blockStatement }
switchLabel ::= case expression : // must be constant
| default :
localVariableDeclarationStatement ::= [ final ] type variableDeclarators ;
variableDeclarators ::= variableDeclarator { , variableDeclarator }
variableDeclarator ::= [ = variableInitializer ]
variableInitializer ::= arrayInitializer | expression
arrayInitializer ::= { [ variableInitializer { , variableInitializer } ] }
arguments ::= ( [ expression { , expression } ] )
type ::= basicType | referenceType
basicType ::= boolean | byte | char | short | int | float | long | double
5 of 7
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
referenceType ::= basicType [ ] { [ ] }
| qualifiedIdentifier { [ ] }
statementExpression ::= expression // but must have side-effect, eg, i++
expression ::= assignmentExpression
assignmentExpression ::= conditionalExpression // must be a valid lhs
[
( =
| +=
| -=
| *=
| /=
| %=
| >>=
| >>>=
| <<=
| &=
| |=
| ^=
) assignmentExpression ]
conditionalExpression ::= conditionalOrExpression [ ? assignmentExpression : conditionalExpression ]
conditionalOrExpression ::= conditionalAndExpression { || conditionalAndExpression }
conditionalAndExpression ::= inclusiveOrExpression { && inclusiveOrExpression }
inclusiveOrExpression ::= exclusiveOrExpression { | exclusiveOrExpression }
exclusiveOrExpression ::= andExpression { ^ andExpression }
andExpression ::= equalityExpression { & equalityExpression }
equalityExpression ::= relationalExpression { ( == | != ) relationalExpression }
relationalExpression ::= shiftExpression ( { ( < | > | <= | >= ) shiftExpression } | instanceof referenceType )
shiftExpression ::= additiveExpression { ( << | >> | >>> ) additiveExpression }
additiveExpression ::= multiplicativeExpression { ( + | – ) multiplicativeExpression }
multiplicativeExpression ::= unaryExpression { ( * | / | % ) unaryExpression }
unaryExpression ::= ++ unaryExpression
| — unaryExpression
| ( + | – ) unaryExpression
| simpleUnaryExpression
6 of 7
CS451/651 Project 4 (Scanning and Parsing with JavaCC) Swami Iyer
simpleUnaryExpression ::= ~ unaryExpression
| ! unaryExpression
| ( basicType ) unaryExpression // basic cast
| ( referenceType ) simpleUnaryExpression // reference cast
| postfixExpression
postfixExpression ::= primary { selector } { ++ | — }
selector ::= . qualifiedIdentifier [ arguments ]
| [ expression ]
primary ::= parExpression
| this [ arguments ]
| supper ( arguments | . [ arguments ] )
| literal
| new creator
| qualifiedIdentifer [ arguments]
creator ::= ( basicType | qualifiedIdentifier )
( arguments
| [ ] { [ ] } [ arrayInitializer ]
| newArrayDeclarator
)
newArrayDeclarator ::= [ [ expression ] ] { [ [ expression ] ] }
literal ::= | | |
| | | true | false | null
7 of 7