|
|
Milestone 2 : Abstract Syntax Trees using JDTYour
task in this milestone is to extend the StaticJava ANTLR parser,
the file You must submit your assignment through the course Project Upload site by 11:59pm CDT on Friday October 19, 2007. Solutions submitted after the deadline will have their score reduced by 10% for each hour that they are late; late times will be rounded up to the next highest hour for this calculation. SetupDownload the file myname-milestone2.zip This contains a skeletal implementation of the first milestone with a JUnit testing framework for your use. In order to import the projects into your workspace, do the following: Goto the "File" menu, select "Import". Expand "General" and select "Existing Projects into Workspace". Click "Next". Select "Select archive file:" and click the "Browse" button associated with it. In the file browser, points to the milestone2.zip that you downloaded. You should see "myname-milestone-scanning-parsing" and "compiler-sjc" ("compiler-sjc" is only available if you don't already have the project in your workspace). Check both (or just the milestone and click "Finish". If you run Eclipse appropriately (i.e., using Java 5.0 and the right versions of plugins and by following the instructions in the course quick notes website), your workspace should be set. Make sure you rename "myname" in the project's name into your CIS username by right clicking the project, select "Refactor" -> "Rename...".
Instructions and HintsYour assignment is to implement the JDT construction actions
required for ESJ. You will do this by adding to the file: sjc.parser.extended.ExtendedStaticJavaAST.g which contains the JDT constructing actions for the basic SJ lexer and parser. We are also providing the file: sjc.parser.extended.ExtendedStaticJava.g which contains a solution to milestone 1 that passes all of the test cases for that milestone. You can use that to see how to handle any features in ESJ that you did not completely handle in your solution for milestone 1. You can tell if you are making progress by running the JUnit test
suite. This is done by highlighting the class: sjc.test.extended.ExtendedParserTest(which is located in the src-esjc-test project), right
clicking and selecting "Run As" -> "JUnit Test". The results will
be shown in total and on a per test case basis.
You can see the source of the test cases in You are done when all of the test cases pass or your run out of time. As with milestone 1, a systematic approach to considering the features of ESJ will be helpful. To get you started, in the following we describe the extensions to the StaticJava syntax defined in ESJ and comment on some details that are relevant for understanding how to construct the AST for those extensions.
// allows simple class declarations before and after main class declaration
<program> ::= <simple-class-declaration>*
<class-declaration>
<simple-class-declaration>*
// simple declaration can have zero or more public field declaration
// (i.e., no methods)
<simple-class-declaration> ::= "class" ID "{" <public-field-declaration>* "}"
// public field declaration has one field modifier: public followed by
// the field's type and name
<public-field-declaration> ::= "public" <type> ID ";"
// SJ type is not renamed as basic-type, and ESJ type can be basic-type,
// class type, or an array type (of basic-type or class type)
<type> ::= ( <basic-type> | ID ) ( "[" "]" )?
// add do-while, for, and increment/decrement statements
<statement> ::= <assign-statement> | <if-statement> | <while-statement>
| <invoke-exp-statement> | <return-statement>
| <for-statement> | <do-while-statement> | <inc-dec-statement>
// allow general lhs instead of SJ's identifier as an assignment's
// left hand side
<assign> ::= <lhs> "=" <exp>
// lhs can be variable reference (identifier), a field access, or
// an array access, respectively
<lhs> ::= ID | <exp> "." ID | <exp> "[" <exp> "]"
// for statement: note that as in Java for-inits, loop condition, and
// for-updates are optional
<for-statement> ::= "for" "(" <for-inits>? ";" <exp>? ";" <for-updates>? ")"
"{" <statement>* "}"
// for inits is a comma separated assignments; note that variable declaration is
// not allowed here
<for-inits> ::= <assign> ( "," <assign> )*
// for updates is a comma separated increments/decrements
<for-updates> ::= <inc-dec> ( "," <inc-dec> )*
// postfix increment/decrement
<inc-dec> ::= <lhs> "++" | <lhs> "--"
// do while statement
<do-while-statement> ::= "do" "{" <statement>* "}" "while" "(" <exp> ")" ";"
// inc/dec statement
<inc-dec-statement> ::= <inc-dec> ";"
// add new expression, array access expression, field access expression, and
// conditional expression
<exp> ::= <literal-exp> | <unary-exp> | <binary-exp> | <paren-exp>
| <invoke-exp> | <var-ref> | <new-exp> | <array-access-exp>
| <field-access-exp> | <cond-exp>
// add null literal
<literal-exp> ::= <boolean-literal> | NUM | "null"
// add bit-complement operator ("~")
<unary-exp> ::= <unary-op> <exp>
// add bit-complement operator ("~")
<unary-op> ::= "+" | "-" | "!" | "~"
// add shift operators ("<<" | ">>" | ">>>"), note that you need to enforce
// correct operator precedence (similar to Java's)
<binary-exp> ::= <exp> <binary-op> <exp>
// add shift operators ("<<" | ">>" | ">>>"), note that you need to enforce
// correct operator precedence (similar to Java's)
<binary-op> ::= "+" | "-" | "*" | "/" | "%" | ">" | ">=" | "==" | "<"
| "<=" | "!=" | "&&" | "||" | "<<" | ">>" | ">>>"
// add conditional operator
<cond-exp> ::= <exp> "?" <exp> ":" <exp>
// add simple class instance creation, new array creation with a specified
// length, and new array creation with given array elements, respectively
<new-exp> ::= "new" ID "(" ")"
| "new" <type> "[" <exp> "]"
| "new" <type> "[" "]" <array-init>
// array init is a comma separated expressions in curly braces
<array-init> ::= "{" <exp> ( "," <exp> )* "}"
// field access
<field-access-exp> ::= <exp> "." ID
// array access
<array-access-exp> ::= <exp> "[" <exp> "]"
Note that there are many different ways that you could structure your ESJ parser and your approach may differ from the illustrated above. As in Milestone 1, the EBNF and comments above are meant to be illustrative. Remember to use the AST view tool to see what "example" JDT looks like. What To SubmitYour solution should consist of the following :
yourname-milestone2.zip. You should test your archive
file to ensure that you can load it into your workspace and execute
the JUnit test driver; when doing this take care not to overwrite your
solution.
As with all programming, I expect that you provide reasonable internal documentation describing tricky/subtle parts of your implementation. If you have any limitations in your implementation you should include documentation describing those as well, since if we find them we will assume that you did not. Your grade will be based on your solution (90%), your in code documentation (5%), and the description file you provide (5%). If you admit to failing a test then you will lose only half of the points associated with that test in comparison to our finding out you fail the test when we run your solution. |