Project 3: Parser

Project 3 Errata list.

Due date: 2004 Sep 29th (Wed) 12:00 Noon

Description

Use JavaCC to construct a parser for our dialect of MiniJava, (as specified below,) that accepts MiniJava source programs and produces abstract syntax trees as specified below.

Project 3 is to be completed in pairs.

Directions

Other Useful Links

Specification

The MiniJava grammar:

Program

::=

MainClass ( ClassDeclaration )* <EOF>

MainClass

::=

"class" ID "{" "public" "static" "void" "main" "(" "String" "[" "]" ID ")" "{"  (Statement)* "}" "}"

ClassDeclaration

::=

"class" ID "{" ( VarDeclaration )* ( MethodDeclaration )* "}"

 

|

"class" ID "extends" ID"{" ( VarDeclaration )* ( MethodDeclaration )* "}"

VarDeclaration

::=

Type ID ";"

MethodDeclaration

::=

"public" Type ID "(" FormalList ")" [ "throws" "java.io.IOException") ] "{" ( VarDeclaration )* ( Statement )* "return" Exp ";" "}"

FormalList

::=

Type ID ( FormalRest)*

 

|

 

FormalRest

::=

"," Type ID

Type

::=

Type "[" "]"

 

|

"boolean"

 

|

"int"

 

|

ID

 

|

"String"

Statement

::=

"{" ( Statement )* "}"

 

|

"if" "(" Exp ")" Statement [ "else" Statement ]

 

|

"while" "(" Exp ")" Statement

 

|

"System.out.println" "("  ")" ";"

 

|

"System.out.print" "(" Exp ")" ";"

 

|

 "System.out.write" "(" Exp ")" ";"

 

|

ID "=" Exp ";"

 

|

ID "[" Exp "]" "=" Exp ";"

Exp

::=

Exp op Exp

 

|

Exp "[" Exp "]"

 

|

Exp "." "length"

 

|

Exp "." ID "(" ExpList ")"

กก  | Exp ("." ID )+

 

|

<DEC_INTEGER_LITERAL>

 

|

<STRING_LITERAL>

 

|

<HEX_INTEGER_LITERAL>

 

|

<OCT_INTEGER_LITERAL>

|

"System.in.read" "(" ")"

 

|

"true"

 

|

"false"

 

|

"null"

 

|

ID

 

|

"this"

 

|

"new" Type "[" Exp "]" ( "[" "]" )*

 

|

"new" ID "(" ")"

 

|

"!" Exp

 

|

"-" Exp

 

|

"~" Exp

 

|

"(" Exp ")"

ExpList

::=

Exp ( ExpRest )*

 

|

 

ExpRest

::=

"," Exp

op

::=

"&&"

 

|

"||"

 

|

"&"

 

|

"|"

 

|

"^"

 

|

"+"

 

|

"-"

 

|

"*"

 

|

"/"

 

|

"!="

 

|

"=="

 

|

"<"

 

|

">"

ID

::=

<IDENTIFIER>

กก

*NOTE:

1. Tokens to be handled are the same as in Project 2, that is, you should use the same definition of operators, punctuation, identifiers, reserved words, integers, (octal, decimal, and hexidecimal,) simple strings, and comments in Project3. (See Specification for Project 2 specification and Appendix A in the textbook.)

2. In the above grammar rules,   "a" denotes the terminal a.

The Abstract Syntax Tree Output:

In this lab you are also required to produce an abstract syntax tree for the parsed program. Unlike previous semesters, you will be creating your own Absyn package and classes to represent the final abstract syntax tree. We will not be providing skeleton code for these classes, nor should you use automated tree builder tools like the Java Tree Builder (JTB). You have a free hand to construct your abstract trees however you like, as long as the work is your own, and the final output conforms to our specifications. Bear in mind, however, that code in both your parser and your abstract suntax trees will be useful in subsequent projects, so write this code with longevity in mind.

The format of the final abstract syntax trees is very simple, and can be gleaned easily from running the reference implementation. Note that whitespace is relevant for readability only, and we will be comparing your output using the -w flag to diff.

What to Turn in


Back

[Rev 1.05 2004 Sep 26 23:55 DWB]