编译器代写: CS451/651 Project 3

CS451/651 Project 3 (Parsing) Swami Iyer

Ob jectives.

1. Support long and double basic types.
2. Support operators.
3. Support conditional expression and switch statement. 4. Support do-while and for statements.
5. Support exception handlers.
6. Support interface type declaration.

In this project, you will only be supporting the parsing of the above programming constructs and their representations in the abstract syntax tree (AST). To compile (just parse for now) your j– programs, you need to run the j– command as follows:

$ $j/j–/bin/j– -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 1. (Long and Double Basic Types) Add support for the long and double basic types.

$ $j/j–/bin/j– -p tests/BasicTypes.java <?xml version=”1.0″ encoding=”utf-8″?>

<JCompilationUnit line=”1″>
<Source fileName=”tests/BasicTypes.java”/>

<Imports >
<Import name=”java.lang.Double”/>

<Import name=”java.lang.Long”/> <Import name=”java.lang.System”/>

</Imports> <TypeDeclarations >

<JClassDeclaration line=”5″ name=”BasicTypes” super=”java.lang.Object”> <Modifiers >

<Modifier name=”public”/> </Modifiers >

<ClassBlock >
<JMethodDeclaration line=”6″ name=”main” returnType=”void”>

<Modifiers >
<Modifier name=”public”/>

<Modifier name=”static”/> </Modifiers >

<FormalParameters >
<JFormalParameter line=”6″ name=”args” type=”String[]”/>

</FormalParameters > <Body>

<JBlock line=”6″> <JVariableDeclaration >

<Modifiers > </Modifiers >

<VariableDeclarators >
<JVariableDeclarator line=”7″ name=”a” type=”double”>

<Initializer >
<JMessageExpression line=”7″ name=”parseDouble”>

1 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<Arguments >

<Argument > <JArrayExpression >

<TheArray >
<JVariable name=”args”/>

</TheArray> <IndexExpression >

<JLiteralInt line=”7″ type=”” value=”0″/> </IndexExpression >

</JArrayExpression > </Argument>

</Arguments > </JMessageExpression >

</Initializer > </JVariableDeclarator >

</VariableDeclarators > </JVariableDeclaration >

<JVariableDeclaration > <Modifiers >

</Modifiers > <VariableDeclarators >

<JVariableDeclarator line=”8″ name=”b” type=”long”> <Initializer >

<JMessageExpression line=”8″ name=”parseLong”> <Arguments >

<Argument > <JArrayExpression >

<TheArray >
<JVariable name=”args”/>

</TheArray> <IndexExpression >

<JLiteralInt line=”8″ type=”” value=”1″/> </IndexExpression >

</JArrayExpression > </Argument>

</Arguments > </JMessageExpression >

</Initializer > </JVariableDeclarator >

</VariableDeclarators > </JVariableDeclaration >

<JStatementExpression line=”9″> <JMessageExpression line=”9″ name=”println”>

<Arguments > <Argument >

<JBinaryExpression line=”9″ type=”” operator=”*”> <Lhs>

<JBinaryExpression line=”9″ type=”” operator=”*”> <Lhs>

<JLiteralDouble line=”9″ type=”” value=”3.14159D”/> </Lhs>

<Rhs>
<JVariable name=”a”/>

</Rhs> </JBinaryExpression >

</Lhs> <Rhs>

<JVariable name=”a”/> </Rhs>

</JBinaryExpression >

</Argument> </Arguments >

</JMessageExpression > </JStatementExpression >

<JStatementExpression line=”10″> <JMessageExpression line=”10″ name=”println”>

<Arguments > <Argument >

2 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<JBinaryExpression line=”10″ type=”” operator=”*”>

<Lhs>
<JLiteralLong line=”10″ type=”” value=”1729L”/>

</Lhs> <Rhs>

<JBinaryExpression line=”10″ type=”” operator=”+”> <Lhs>

<JVariable name=”b”/> </Lhs>

<Rhs>
<JVariable name=”b”/>

</Rhs> </JBinaryExpression >

</Rhs> </JBinaryExpression >

</Argument> </Arguments >

</JMessageExpression > </JStatementExpression >

</JBlock> </Body>

</JMethodDeclaration > </ClassBlock >

</JClassDeclaration > </TypeDeclarations >

</JCompilationUnit >

Problem 2. (Operators) Add support for the following operators, obeying precedence rules (see Appendix C).

~ != / /= -=

++ — *= % %= >> >>= >>> >>>= >=

<< <<= < ^ ^= | |= || & &=

$ $j/j–/bin/j– -p tests/Operators.java <?xml version=”1.0″ encoding=”utf-8″?>

<JCompilationUnit line=”1″>
<Source fileName=”tests/Operators.java”/>

<Imports >
<Import name=”java.lang.System”/>

</Imports> <TypeDeclarations >

<JClassDeclaration line=”3″ name=”Operators” super=”java.lang.Object”> <Modifiers >

<Modifier name=”public”/> </Modifiers >

<ClassBlock >
<JMethodDeclaration line=”4″ name=”main” returnType=”void”>

<Modifiers >
<Modifier name=”public”/>

<Modifier name=”static”/> </Modifiers >

<FormalParameters >
<JFormalParameter line=”4″ name=”args” type=”String[]”/>

</FormalParameters > <Body>

<JBlock line=”4″> <JStatementExpression line=”5″>

<JMessageExpression line=”5″ name=”println”> <Arguments >

<Argument >
<JBinaryExpression line=”5″ type=”” operator=”||”>

<Lhs>
<JBinaryExpression line=”5″ type=”” operator=”&amp;&amp;”>

<Lhs>
<JUnaryExpression line=”5″ type=”” operator=”!”>

3 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<Operand >

<JLiteralTrue line=”5″ type=””/> </Operand>

</JUnaryExpression > </Lhs>

<Rhs>
<JLiteralFalse line=”5″ type=””/>

</Rhs> </JBinaryExpression >

</Lhs> <Rhs>

<JLiteralFalse line=”5″ type=””/> </Rhs>

</JBinaryExpression > </Argument>

</Arguments > </JMessageExpression >

</JStatementExpression > <JStatementExpression line=”6″>

<JMessageExpression line=”6″ name=”println”> <Arguments >

<Argument >
<JBinaryExpression line=”6″ type=”” operator=”!=”>

<Lhs>
<JLiteralTrue line=”6″ type=””/>

</Lhs> <Rhs>

<JLiteralFalse line=”6″ type=””/> </Rhs>

</JBinaryExpression > </Argument>

</Arguments > </JMessageExpression >

</JStatementExpression > <JVariableDeclaration >

<Modifiers > </Modifiers >

<VariableDeclarators >
<JVariableDeclarator line=”7″ name=”x” type=”int”>

<Initializer >
<JLiteralInt line=”7″ type=”” value=”42″/>

</Initializer > </JVariableDeclarator >

</VariableDeclarators > </JVariableDeclaration >

<JStatementExpression line=”8″>
<JBinaryExpression line=”8″ type=”” operator=”-=”>

<Lhs>
<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”8″ type=”” value=”2″/> </Rhs>

</JBinaryExpression > </JStatementExpression >

<JStatementExpression line=”9″>
<JBinaryExpression line=”9″ type=”” operator=”*=”>

<Lhs>
<JVariable name=”x”/>

</Lhs>

<Rhs>
<JLiteralInt line=”9″ type=”” value=”2″/>

</Rhs> </JBinaryExpression >

</JStatementExpression > <JStatementExpression line=”10″>

<JBinaryExpression line=”10″ type=”” operator=”/=”> <Lhs>

4 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”10″ type=”” value=”10″/> </Rhs>

</JBinaryExpression > </JStatementExpression >

<JStatementExpression line=”11″>
<JBinaryExpression line=”11″ type=”” operator=”%=”>

<Lhs>
<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”11″ type=”” value=”3″/> </Rhs>

</JBinaryExpression > </JStatementExpression >

<JStatementExpression line=”12″> <JMessageExpression line=”12″ name=”println”>

<Arguments > <Argument >

<JUnaryExpression line=”12″ type=”” operator=”post++”> <Operand >

<JVariable name=”x”/> </Operand>

</JUnaryExpression > </Argument>

</Arguments > </JMessageExpression >

</JStatementExpression > <JStatementExpression line=”13″>

<JMessageExpression line=”13″ name=”println”> <Arguments >

<Argument >
<JUnaryExpression line=”13″ type=”” operator=”–pre”>

<Operand >
<JVariable name=”x”/>

</Operand> </JUnaryExpression >

</Argument> </Arguments >

</JMessageExpression > </JStatementExpression >

<JStatementExpression line=”14″>
<JBinaryExpression line=”14″ type=”” operator=”&gt;&gt;=”>

<Lhs>
<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”14″ type=”” value=”1″/> </Rhs>

</JBinaryExpression > </JStatementExpression >

<JStatementExpression line=”15″>
<JBinaryExpression line=”15″ type=”” operator=”&gt;&gt;&gt;=”>

<Lhs>
<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”15″ type=”” value=”2″/>

</Rhs> </JBinaryExpression >

</JStatementExpression > <JStatementExpression line=”16″>

<JBinaryExpression line=”16″ type=”” operator=”&lt;&lt;=”> <Lhs>

<JVariable name=”x”/> </Lhs>

5 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<Rhs>

<JLiteralInt line=”16″ type=”” value=”3″/> </Rhs>

</JBinaryExpression > </JStatementExpression >

<JStatementExpression line=”17″>
<JBinaryExpression line=”17″ type=”” operator=”^=”>

<Lhs>
<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”17″ type=”” value=”2″/> </Rhs>

</JBinaryExpression > </JStatementExpression >

<JStatementExpression line=”18″>
<JBinaryExpression line=”18″ type=”” operator=”|=”>

<Lhs>
<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”18″ type=”” value=”4″/> </Rhs>

</JBinaryExpression > </JStatementExpression >

<JStatementExpression line=”19″>
<JBinaryExpression line=”19″ type=”” operator=”&amp;=”>

<Lhs>
<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”19″ type=”” value=”8″/> </Rhs>

</JBinaryExpression > </JStatementExpression >

<JStatementExpression line=”20″> <JMessageExpression line=”20″ name=”println”>

<Arguments > <Argument >

<JBinaryExpression line=”20″ type=”” operator=”&lt;”> <Lhs>

<JVariable name=”x”/> </Lhs>

<Rhs>
<JLiteralInt line=”20″ type=”” value=”100″/>

</Rhs> </JBinaryExpression >

</Argument> </Arguments >

</JMessageExpression > </JStatementExpression >

<JStatementExpression line=”21″> <JMessageExpression line=”21″ name=”println”>

<Arguments > <Argument >

<JBinaryExpression line=”21″ type=”” operator=”&gt;=”> <Lhs>

<JVariable name=”x”/> </Lhs>

<Rhs>

<JLiteralInt line=”21″ type=”” value=”50″/> </Rhs>

</JBinaryExpression > </Argument>

</Arguments > </JMessageExpression >

</JStatementExpression > </JBlock>

6 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

</Body>

</JMethodDeclaration > </ClassBlock >

</JClassDeclaration > </TypeDeclarations >

</JCompilationUnit >

Problem 3. (Conditional Expression) Add support for conditional expression (e1 ? e2 : e3).

$ $j/j–bin/j– -p tests/ConditionalExpression.java

<?xml version=”1.0″ encoding=”utf-8″?> <JCompilationUnit line=”1″>

<Source fileName=”tests/ConditionalExpression.java”/> <Imports >

<Import name=”java.lang.Integer”/> <Import name=”java.lang.System”/>

</Imports> <TypeDeclarations >

<JClassDeclaration line=”4″ name=”ConditionalExpression” super=”java.lang.Object”>

<Modifiers >
<Modifier name=”public”/>

</Modifiers > <ClassBlock >

<JMethodDeclaration line=”5″ name=”main” returnType=”void”> <Modifiers >

<Modifier name=”public”/> <Modifier name=”static”/>

</Modifiers > <FormalParameters >

<JFormalParameter line=”5″ name=”args” type=”String[]”/> </FormalParameters >

<Body>
<JBlock line=”5″>

<JVariableDeclaration > <Modifiers >

</Modifiers > <VariableDeclarators >

<JVariableDeclarator line=”6″ name=”x” type=”int”> <Initializer >

<JMessageExpression line=”6″ name=”parseInt”> <Arguments >

<Argument > <JArrayExpression >

<TheArray >
<JVariable name=”args”/>

</TheArray> <IndexExpression >

<JLiteralInt line=”6″ type=”” value=”0″/> </IndexExpression >

</JArrayExpression > </Argument>

</Arguments > </JMessageExpression >

</Initializer > </JVariableDeclarator >

</VariableDeclarators > </JVariableDeclaration >

<JStatementExpression line=”7″> <JMessageExpression line=”7″ name=”println”>

<Arguments > <Argument >

<JConditionalExpression line=”7″ type=”” operator=”?”> <TestExpression >

<JBinaryExpression line=”7″ type=”” operator=”==”> <Lhs>

<JBinaryExpression line=”7″ type=”” operator=”%”> <Lhs>

7 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<JVariable name=”x”/>

</Lhs> <Rhs>

<JLiteralInt line=”7″ type=”” value=”2″/> </Rhs>

</JBinaryExpression > </Lhs>

<Rhs>
<JLiteralInt line=”7″ type=”” value=”0″/>

</Rhs> </JBinaryExpression >

</TestExpression > <TrueClause >

<JLiteralString line=”7″ type=”” value=”&quot;even&quot;”/> </TrueClause >

<FalseClause >
<JLiteralString line=”7″ type=”” value=”&quot;odd&quot;”/>

</FalseClause > </JConditionalExpression >

</Argument> </Arguments >

</JMessageExpression > </JStatementExpression >

</JBlock> </Body>

</JMethodDeclaration > </ClassBlock >

</JClassDeclaration > </TypeDeclarations >

</JCompilationUnit >

Problem 4. (Switch Statement) Add support for a switch statement.

$ $j/j–/bin/j– -p tests/SwitchStatement.java <?xml version=”1.0″ encoding=”utf-8″?>

<JCompilationUnit line=”1″>
<Source fileName=”tests/SwitchStatement.java”/>

<Imports >
<Import name=”java.lang.Integer”/>

<Import name=”java.lang.System”/> </Imports>

<TypeDeclarations >
<JClassDeclaration line=”4″ name=”SwitchStatement” super=”java.lang.Object”>

<Modifiers >
<Modifier name=”public”/>

</Modifiers > <ClassBlock >

<JMethodDeclaration line=”5″ name=”main” returnType=”void”> <Modifiers >

<Modifier name=”public”/> <Modifier name=”static”/>

</Modifiers > <FormalParameters >

<JFormalParameter line=”5″ name=”args” type=”String[]”/> </FormalParameters >

<Body>
<JBlock line=”5″>

<JSwitchStatement line=”6″> <TestExpression >

<JMessageExpression line=”6″ name=”parseInt”> <Arguments >

<Argument > <JArrayExpression >

<TheArray >
<JVariable name=”args”/>

</TheArray> <IndexExpression >

8 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<JLiteralInt line=”6″ type=”” value=”0″/>

</IndexExpression > </JArrayExpression >

</Argument> </Arguments >

</JMessageExpression > </TestExpression >

<SwitchBlockStatementGroup > <CaseLabel >

<JLiteralInt line=”7″ type=”” value=”1″/> </CaseLabel >

<CaseLabel >
<JLiteralInt line=”8″ type=”” value=”2″/>

</CaseLabel > <CaseLabel >

<JLiteralInt line=”9″ type=”” value=”3″/> </CaseLabel >

<CaseLabel >
<JLiteralInt line=”10″ type=”” value=”4″/>

</CaseLabel > <CaseLabel >

<JLiteralInt line=”11″ type=”” value=”5″/> </CaseLabel >

<Body>
<JStatementExpression line=”12″>

<JMessageExpression line=”12″ name=”println”> <Arguments >

<Argument >
<JLiteralString line=”12″ type=”” value=”&quot;Spring&quot;”/>

</Argument> </Arguments >

</JMessageExpression > </JStatementExpression >

</Body> <Body>

<JBreakStatement line=”13″> </JBreakStatement >

</Body> </SwitchBlockStatementGroup >

<SwitchBlockStatementGroup > <CaseLabel >

<JLiteralInt line=”14″ type=”” value=”6″/> </CaseLabel >

<CaseLabel >
<JLiteralInt line=”15″ type=”” value=”7″/>

</CaseLabel > <CaseLabel >

<JLiteralInt line=”16″ type=”” value=”8″/> </CaseLabel >

<Body>
<JStatementExpression line=”17″>

<JMessageExpression line=”17″ name=”println”> <Arguments >

<Argument >
<JLiteralString line=”17″ type=”” value=”&quot;Summer&quot;”/>

</Argument> </Arguments >

</JMessageExpression > </JStatementExpression >

</Body>

<Body>
<JBreakStatement line=”18″>

</JBreakStatement > </Body>

</SwitchBlockStatementGroup > <SwitchBlockStatementGroup >

<CaseLabel >
<JLiteralInt line=”19″ type=”” value=”9″/>

9 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

</CaseLabel >

<CaseLabel >
<JLiteralInt line=”20″ type=”” value=”10″/>

</CaseLabel > <CaseLabel >

<JLiteralInt line=”21″ type=”” value=”11″/> </CaseLabel >

<CaseLabel >
<JLiteralInt line=”22″ type=”” value=”12″/>

</CaseLabel > <Body>

<JStatementExpression line=”23″> <JMessageExpression line=”23″ name=”println”>

<Arguments > <Argument >

<JLiteralString line=”23″ type=”” value=”&quot;Fall&quot;”/> </Argument>

</Arguments > </JMessageExpression >

</JStatementExpression > </Body>

<Body>
<JBreakStatement line=”24″>

</JBreakStatement > </Body>

</SwitchBlockStatementGroup > <SwitchBlockStatementGroup >

<DefaultLabel/> <Body>

<JStatementExpression line=”26″> <JMessageExpression line=”26″ name=”println”>

<Arguments > <Argument >

<JLiteralString line=”26″ type=”” value=”&quot;Error!&quot;”/> </Argument>

</Arguments > </JMessageExpression >

</JStatementExpression > </Body>

</SwitchBlockStatementGroup > </JSwitchStatement >

</JBlock> </Body>

</JMethodDeclaration > </ClassBlock >

</JClassDeclaration > </TypeDeclarations >

</JCompilationUnit >

Problem 5. (Do-while Statement) Add support for a do-while statement.

$ $j/j–/bin/j– -p tests/DoWhileStatement.java <?xml version=”1.0″ encoding=”utf-8″?>

<JCompilationUnit line=”1″>
<Source fileName=”tests/DoWhileStatement.java”/>

<Imports >
<Import name=”java.lang.System”/>

</Imports> <TypeDeclarations >

<JClassDeclaration line=”3″ name=”DoWhileStatement” super=”java.lang.Object”> <Modifiers >

<Modifier name=”public”/> </Modifiers >

<ClassBlock >
<JMethodDeclaration line=”4″ name=”main” returnType=”void”>

<Modifiers >
<Modifier name=”public”/>

10 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<Modifier name=”static”/>

</Modifiers > <FormalParameters >

<JFormalParameter line=”4″ name=”args” type=”String[]”/> </FormalParameters >

<Body>
<JBlock line=”4″>

<JVariableDeclaration > <Modifiers >

</Modifiers > <VariableDeclarators >

<JVariableDeclarator line=”5″ name=”i” type=”int”> <Initializer >

<JLiteralInt line=”5″ type=”” value=”0″/> </Initializer >

</JVariableDeclarator >
<JVariableDeclarator line=”5″ name=”sum” type=”int”>

<Initializer >
<JLiteralInt line=”5″ type=”” value=”0″/>

</Initializer > </JVariableDeclarator >

</VariableDeclarators > </JVariableDeclaration >

<JDoWhileStatement line=”6″> <Body>

<JBlock line=”6″> <JStatementExpression line=”7″>

<JBinaryExpression line=”7″ type=”” operator=”+=”> <Lhs>

<JVariable name=”sum”/> </Lhs>

<Rhs>
<JUnaryExpression line=”7″ type=”” operator=”post++”>

<Operand >
<JVariable name=”i”/>

</Operand> </JUnaryExpression >

</Rhs> </JBinaryExpression >

</JStatementExpression > </JBlock>

</Body> <TestExpression >

<JBinaryExpression line=”8″ type=”” operator=”&lt;=”> <Lhs>

<JVariable name=”i”/> </Lhs>

<Rhs>
<JLiteralInt line=”8″ type=”” value=”10″/>

</Rhs> </JBinaryExpression >

</TestExpression > </JDoWhileStatement >

<JStatementExpression line=”9″> <JMessageExpression line=”9″ name=”println”>

<Arguments > <Argument >

<JVariable name=”sum”/> </Argument>

</Arguments >

</JMessageExpression > </JStatementExpression >

</JBlock> </Body>

</JMethodDeclaration > </ClassBlock >

</JClassDeclaration > </TypeDeclarations >

11 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

</JCompilationUnit >

Problem 6. (For Statement) Add support for a for statement.

$ $j/j–/bin/j– -p tests/ForStatement.java <?xml version=”1.0″ encoding=”utf-8″?>

<JCompilationUnit line=”1″>
<Source fileName=”tests/ForStatement.java”/>

<Imports >
<Import name=”java.lang.System”/>

</Imports> <TypeDeclarations >

<JClassDeclaration line=”3″ name=”ForStatement” super=”java.lang.Object”> <Modifiers >

<Modifier name=”public”/> </Modifiers >

<ClassBlock >
<JMethodDeclaration line=”4″ name=”main” returnType=”void”>

<Modifiers >

<Modifier name=”public”/> <Modifier name=”static”/>

</Modifiers > <FormalParameters >

<JFormalParameter line=”4″ name=”args” type=”String[]”/> </FormalParameters >

<Body>
<JBlock line=”4″>

<JVariableDeclaration > <Modifiers >

</Modifiers > <VariableDeclarators >

<JVariableDeclarator line=”5″ name=”sum” type=”int”> <Initializer >

<JLiteralInt line=”5″ type=”” value=”0″/> </Initializer >

</JVariableDeclarator > </VariableDeclarators >

</JVariableDeclaration > <JForStatement line=”6″>

<InitialExpression > <JVariableDeclaration >

<Modifiers > </Modifiers >

<VariableDeclarators >
<JVariableDeclarator line=”6″ name=”i” type=”int”>

<Initializer >
<JLiteralInt line=”6″ type=”” value=”1″/>

</Initializer > </JVariableDeclarator >

</VariableDeclarators > </JVariableDeclaration >

</InitialExpression > <TestExpression >

<JBinaryExpression line=”6″ type=”” operator=”&lt;=”> <Lhs>

<JVariable name=”i”/> </Lhs>

<Rhs>
<JLiteralInt line=”6″ type=”” value=”10″/>

</Rhs> </JBinaryExpression >

</TestExpression > <UpdateExpression >

<JStatementExpression line=”6″>
<JUnaryExpression line=”6″ type=”” operator=”post++”>

<Operand >
<JVariable name=”i”/>

12 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

</Operand>

</JUnaryExpression > </JStatementExpression >

</UpdateExpression > <Statement >

<JBlock line=”6″> <JStatementExpression line=”7″>

<JBinaryExpression line=”7″ type=”” operator=”+=”> <Lhs>

<JVariable name=”sum”/> </Lhs>

<Rhs>
<JVariable name=”i”/>

</Rhs> </JBinaryExpression >

</JStatementExpression > </JBlock>

</Statement > </JForStatement >

<JStatementExpression line=”9″> <JMessageExpression line=”9″ name=”println”>

<Arguments > <Argument >

<JVariable name=”sum”/> </Argument>

</Arguments > </JMessageExpression >

</JStatementExpression > </JBlock>

</Body> </JMethodDeclaration >

</ClassBlock > </JClassDeclaration >

</TypeDeclarations > </JCompilationUnit >

Problem 7. (Exception Handlers) Add support for exception handling, which involves supporting the try, catch, finally, throw, and throws clauses.

$ $j/j–/bin/j– -p tests/ExceptionHandlers.java <?xml version=”1.0″ encoding=”utf-8″?>

<JCompilationUnit line=”1″>
<Source fileName=”tests/ExceptionHandlers.java”/>

<Imports > </Imports>

<TypeDeclarations >
<JClassDeclaration line=”1″ name=”ExceptionHandlers” super=”java.lang.Object”>

<Modifiers >
<Modifier name=”public”/>

</Modifiers > <ClassBlock >

<JMethodDeclaration line=”2″ name=”f” returnType=”void”> <Modifiers >

<Modifier name=”private”/> <Modifier name=”static”/>

</Modifiers > <FormalParameters >

</FormalParameters > <Exceptions >

<Exception type=”Exception1″/> <Exception type=”Exception2″/>

</Exceptions > <Body>

<JBlock line=”2″> <JThrowStatement line=”3″>

<JNewOp line=”3″ type=”Exception1″/> <Arguments >

13 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

</Arguments >

</JNewOp> </JThrowStatement >

</JBlock> </Body>

</JMethodDeclaration >
<JMethodDeclaration line=”6″ name=”main” returnType=”void”>

<Modifiers >
<Modifier name=”public”/>

<Modifier name=”static”/> </Modifiers >

<FormalParameters >
<JFormalParameter line=”6″ name=”args” type=”String[]”/>

</FormalParameters > <Body>

<JBlock line=”6″> <JTryCatchFinallyStatement line=”7″>

<TryBlock >
<JBlock line=”7″>

<JStatementExpression line=”8″> <JMessageExpression line=”8″ name=”f”>

<Arguments > </Arguments >

</JMessageExpression > </JStatementExpression >

</JBlock> </TryBlock >

<CatchBlock >
<JFormalParameter line=”10″ name=”e1″ type=”Exception1″/>

<JBlock line=”10″> <JEmptyStatement line=”10″/>

</JBlock> </CatchBlock >

<CatchBlock >
<JFormalParameter line=”11″ name=”e2″ type=”Exception2″/>

<JBlock line=”11″> <JEmptyStatement line=”11″/>

</JBlock> </CatchBlock >

<FinallyBlock > <JBlock line=”12″>

<JEmptyStatement line=”12″/> </JBlock>

</FinallyBlock > </JTryCatchFinallyStatement >

</JBlock> </Body>

</JMethodDeclaration > </ClassBlock >

</JClassDeclaration > </TypeDeclarations >

</JCompilationUnit >

Problem 8. (Interface Type Declaration) Implement support for interface declaration.

$ $j/j–/bin/j– -p tests/Interface.java <?xml version=”1.0″ encoding=”utf-8″?>

<JCompilationUnit line=”1″>
<Source fileName=”tests/Interface.java”/>

<Imports > </Imports>

<TypeDeclarations >
<JInterfaceDeclaration line=”1″ name=”A”>

<Modifiers > </Modifiers >

<InterfaceBlock >
<JMethodDeclaration line=”2″ name=”f” returnType=”int”>

14 of 15

CS451/651 Project 3 (Parsing) Swami Iyer

<Modifiers >

<Modifier name=”public”/> </Modifiers >

<FormalParameters >
<JFormalParameter line=”2″ name=”x” type=”int”/>

</FormalParameters > </JMethodDeclaration >

</InterfaceBlock > </JInterfaceDeclaration >

<JClassDeclaration line=”5″ name=”B” super=”java.lang.Object”> <Modifiers >

<Modifier name=”public”/> </Modifiers >

<Implements >
<Implement name=”A”/>

</Implements > <ClassBlock >

<JMethodDeclaration line=”6″ name=”f” returnType=”int”> <Modifiers >

<Modifier name=”public”/> </Modifiers >

<FormalParameters >
<JFormalParameter line=”6″ name=”x” type=”int”/>

</FormalParameters > <Body>

<JBlock line=”6″> <JReturnStatement line=”7″>

<JBinaryExpression line=”7″ type=”” operator=”*”> <Lhs>

<JVariable name=”x”/> </Lhs>

<Rhs>
<JVariable name=”x”/>

</Rhs> </JBinaryExpression >

</JReturnStatement > </JBlock>

</Body> </JMethodDeclaration >

</ClassBlock > </JClassDeclaration >

</TypeDeclarations > </JCompilationUnit >

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:

• Make sure your report isn’t too verbose, doesn’t contain lines that exceed 80 characters, and doesn’t contain spelling/grammatical mistakes

$ cd $j/j–

$ ant clean $ cd ..

$ tar -cvf j–.tar j–/* $ gzip j–.tar

15 of 15