

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Due Tuesday, December 11, 2007, at 5:00 P.M.
Write a compiler for the TinyJava 1 language.
The compiler may be written in any programming language that you wish and implemented on any computer that you wish. In the past, most projects have benefitted from the Java software tools JLex and CUP which allow automatic generation of lexical and syntactic analyzers. These tools expect to interface with programs written in Java. There are similar tools available for C and C++. It is also possible to use alternative languages by adding additional passes in the compiler. For example, CUP could be used to generate a tree representation of the program which could be output in a form compatible with programming language L. Programs in L could then read this intermediate form to complete the additional compilation phases. (^1) TinyJava is a distilled version of Java
program ::= import java.util.* ; class-definition {class-definition} class-definition ::= class class-identifier [extends class-identifier] { {member-list} } member-list ::= member-declaration {member-declaration} member-declaration ::= declarator ; | function-definition declarator ::= protected [static] type { [ ] } object-identifier function-definition ::= function-declaration { { declarator ;} [statement-list] [return expression ;] } function-declaration ::= public [static] type function-identifier ( [argument-declaration-list] ) | main-declaration main-declaration ::= [Scanner in = new Scanner ( System. in ) ;] public static void main ( String [ ] args ) type ::= class-identifier | int | boolean argument-declaration-list ::= argument-declaration {, argument-declaration} argument-declaration ::= type { [ ] } object-identifier compound-statement ::= { statement-list } statement-list ::= statement {statement} statement ::= compound-statement | assignment-statement ; | if ( expression ) statement [else statement] | while ( expression ) statement | System. out. println ( expression ); assignment-statement ::= variable = expression | variable = new class-identifier () | variable = new int [ integer ] { [ integer ] } | variable = in. nextInt () expression ::= term | expression binary-operator expression expression-list ::= expression {, expression} term ::= primary-expression | unary-operator term primary-expression ::= object | integer | ( expression ) object ::= variable | function-call variable ::= this | [object .] object-identifier {[ expression ]} function-call ::= [object .] function-identifier ( [expression-list] )
Syntactic and Semantic Conventions The keywords and the token symbols in the above subset of Java are in bold. The unary operators are +, − and! (negation) Binary operators obey the customary precedence rules, from highest to lowest: multiplicative ∗, / binary additive +, − relational inequality <, >, <=, >= relational equality ==,! = conjunction && disjunction ||
class-identifiers, object-identifiers, and function-identifiers are the same lexical/syntactic items as identifiers but have the semantics given by the appropriate qualifier.