/* The Teaching Machine - Java Parser * JJTree Source * * Copyright (C) 2002 Theodore S. Norvell * Permission is grnated to use this file * provided you respect the Sun copyright below * and that you understand that there may be * errors in this file. * Based on Java1.2-b.jjt (original header follows) */ /** * Copyright (C) 1996, 1997 Sun Microsystems Inc. * * Use of this file and the system it is part of is constrained by the * file COPYRIGHT in the root directory of this system. You may, however, * make any modifications you wish to this file. * * Java files generated by running JavaCC on this file (or modified versions * of this file) may be used in exactly the same manner as Java files * generated from any grammar developed by you. * * Author: Sriram Sankar * Date: 3/5/97 * * This file contains a Java grammar and actions that implement a front-end. */ options { JAVA_UNICODE_ESCAPE = true; STATIC=false; MULTI=false; VISITOR=true; NODE_USES_PARSER=true; } PARSER_BEGIN(JavaParser) package JavaLang.Parser; import Clc.Analysis.IdTable ; import Clc.Analysis.ScopedName ; import JavaLang.Analysis.Java_SpecifierSet ; import JavaLang.Analysis.Java_Specifiers ; public class JavaParser { public static void main (String [] args) { JavaParser parser; String filename = null; long initTime = 0; long parseTime = 0; long startTime = 0; long stopTime = 0; if (args.length == 0) { System.out.println("Java Parser Version 1.1 (for Java1.2 code): Reading from standard input . . ."); parser = new JavaParser(System.in); } else if (args.length == 1) { filename = args[0]; System.out.println("Java Parser Version 1.1 (for Java1.2 code): Reading from file " + filename + " . . ."); try { startTime = System.currentTimeMillis(); parser = new JavaParser(new java.io.FileInputStream(filename)); stopTime = System.currentTimeMillis(); initTime = stopTime - startTime; } catch (java.io.FileNotFoundException e) { System.out.println("Java Parser Version 1.1 (for Java1.2 code): File " + filename + " not found."); return; } } else { System.out.println("Java Parser Version 1.1 (for Java1.2 code): Usage is one of:"); System.out.println(" java JavaParser < inputfile"); System.out.println("OR"); System.out.println(" java JavaParser inputfile"); return; } try { startTime = System.currentTimeMillis(); parser.CompilationUnit(); stopTime = System.currentTimeMillis(); parseTime = stopTime - startTime; System.out.println("Java Parser Version 1.1 (for Java1.2 code): "); System.out.println(" Java program parsed " + filename + " successfully in " + (initTime + parseTime) + " ms."); System.out.println(" parser initialization time was " + initTime + " ms."); System.out.println(" parser parse time was " + parseTime + " ms."); } catch (ParseException e) { System.out.println(e.getMessage()); System.out.println("Java Parser Version 1.1 (for Java1.2 code): Encountered errors during parse."); } } IdTable idTable = new IdTable() ; } PARSER_END(JavaParser) /* WHITE SPACE */ SPECIAL_TOKEN : { " " | "\t" | "\n" | "\r" | "\f" } /* COMMENTS */ MORE : { "//" : IN_SINGLE_LINE_COMMENT | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT | "/*" : IN_MULTI_LINE_COMMENT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } MORE : { < ~[] > } /* RESERVED WORDS AND LITERALS */ TOKEN : { < ABSTRACT: "abstract" > | < BOOLEAN: "boolean" > | < BREAK: "break" > | < BYTE: "byte" > | < CASE: "case" > | < CATCH: "catch" > | < CHAR: "char" > | < CLASS: "class" > | < CONST: "const" > | < CONTINUE: "continue" > | < _DEFAULT: "default" > | < DO: "do" > | < DOUBLE: "double" > | < ELSE: "else" > | < EXTENDS: "extends" > | < FALSE: "false" > | < FINAL: "final" > | < FINALLY: "finally" > | < FLOAT: "float" > | < FOR: "for" > | < GOTO: "goto" > | < IF: "if" > | < IMPLEMENTS: "implements" > | < IMPORT: "import" > | < INSTANCEOF: "instanceof" > | < INT: "int" > | < INTERFACE: "interface" > | < LONG: "long" > | < NATIVE: "native" > | < NEW: "new" > | < NULL: "null" > | < PACKAGE: "package"> | < PRIVATE: "private" > | < PROTECTED: "protected" > | < PUBLIC: "public" > | < RETURN: "return" > | < SHORT: "short" > | < STATIC: "static" > | < SUPER: "super" > | < SWITCH: "switch" > | < SYNCHRONIZED: "synchronized" > | < THIS: "this" > | < THROW: "throw" > | < THROWS: "throws" > | < TRANSIENT: "transient" > | < TRUE: "true" > | < TRY: "try" > | < VOID: "void" > | < VOLATILE: "volatile" > | < WHILE: "while" > | < STRICTFP: "strictfp" > } /* LITERALS */ TOKEN : { < DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | < HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | < OCTAL_LITERAL: "0" (["0"-"7"])* > | < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* ()? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"] > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | < CHARACTER_LITERAL: "'" ( (~["'","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) ) "'" > | < STRING_LITERAL: "\"" ( (~["\"","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "\"" > } /* IDENTIFIERS */ TOKEN : { < IDENTIFIER: (|)* > | < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > | < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > } /* SEPARATORS */ TOKEN : { < LPAREN: "(" > | < RPAREN: ")" > | < LBRACE: "{" > | < RBRACE: "}" > | < LBRACKET: "[" > | < RBRACKET: "]" > | < SEMICOLON: ";" > | < COMMA: "," > | < DOT: "." > } /* OPERATORS */ TOKEN : { < ASSIGN: "=" > | < GT: ">" > | < LT: "<" > | < BANG: "!" > | < TILDE: "~" > | < HOOK: "?" > | < COLON: ":" > | < EQ: "==" > | < LE: "<=" > | < GE: ">=" > | < NE: "!=" > | < SC_OR: "||" > | < SC_AND: "&&" > | < INCR: "++" > | < DECR: "--" > | < PLUS: "+" > | < MINUS: "-" > | < STAR: "*" > | < SLASH: "/" > | < BIT_AND: "&" > | < BIT_OR: "|" > | < XOR: "^" > | < REM: "%" > | < LSHIFT: "<<" > | < RSIGNEDSHIFT: ">>" > | < RUNSIGNEDSHIFT: ">>>" > | < PLUSASSIGN: "+=" > | < MINUSASSIGN: "-=" > | < STARASSIGN: "*=" > | < SLASHASSIGN: "/=" > | < ANDASSIGN: "&=" > | < ORASSIGN: "|=" > | < XORASSIGN: "^=" > | < REMASSIGN: "%=" > | < LSHIFTASSIGN: "<<=" > | < RSIGNEDSHIFTASSIGN: ">>=" > | < RUNSIGNEDSHIFTASSIGN: ">>>=" > } /***************************************** * THE JAVA LANGUAGE GRAMMAR STARTS HERE * *****************************************/ /* * Program structuring syntax follows. */ SimpleNode CompilationUnit() : {} { OptPackageDeclaration() ImportDeclarations() ( TypeDeclaration() )* { return jjtThis; } } void OptPackageDeclaration() : { ScopedName name ; } { [ "package" name = Name() ";" { jjtThis.setName( name ) ; } { jjtThis.setBool( true ) ; } ] } void ImportDeclarations() : {} { ( ImportDeclaration() )* } void ImportDeclaration() : { ScopedName name ; } { "import" name = Name() { jjtThis.setName( name ) ; } [ "." "*" {jjtThis.setBool( true );} ] ";" } void TypeDeclaration() #void : {} { LOOKAHEAD( ( "abstract" | "final" | "public" | "strictfp" )* "class" ) ClassDeclaration() | InterfaceDeclaration() | ";" } /* * Declaration syntax follows. */ void ClassDeclaration() #void : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ( Abstract( specSet ) | Final( specSet ) | Public( specSet) | Strictfp( specSet ) )* UnmodifiedClassDeclaration( specSet ) } void NestedClassDeclaration() #void : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ( Static( specSet ) | Abstract( specSet ) | Final( specSet ) | Public( specSet ) | Protected( specSet ) | Private( specSet ) | Strictfp( specSet ) )* UnmodifiedClassDeclaration( specSet ) } void UnmodifiedClassDeclaration( Java_SpecifierSet specSet ) : { String id ; ScopedName name ; } { { jjtThis.setSpecSet( specSet ) ; } "class" id=Identifier() { jjtThis.setString( id ) ; } OptClassExtendsClause() OptImplementsClause() ClassBody() } void OptClassExtendsClause() : { ScopedName name ; } { [ "extends" name=Name() { jjtThis.setName( name ) ; } { jjtThis.setBool( true ) ; } ] } void OptImplementsClause() : {} { [ "implements" NameList() { jjtThis.setBool( true ) ; } ] } void ClassBody() : {} { "{" ( ClassBodyDeclaration() )* "}" } void ClassBodyDeclaration() #void : {} { LOOKAHEAD(2) Initializer() | LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" | "strictfp")* "class" ) NestedClassDeclaration() | LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" | "strictfp")* "interface" ) NestedInterfaceDeclaration() | LOOKAHEAD( [ "public" | "protected" | "private" ] Name() "(" ) ConstructorDeclaration() | LOOKAHEAD( MethodDeclarationLookahead() ) MethodDeclaration() | FieldDeclaration() | ";" } // This production is to determine lookahead only. void MethodDeclarationLookahead() #void : {} { ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" | "strictfp")* ResultType(null) "(" } void InterfaceDeclaration() #void : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ( Abstract( specSet ) | Public( specSet ) | Strictfp( specSet ))* UnmodifiedInterfaceDeclaration( specSet ) } void NestedInterfaceDeclaration() #void : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ( Static( specSet ) | Abstract( specSet ) | Final( specSet ) | Public( specSet )| Protected( specSet ) | Private( specSet )| Strictfp( specSet ))* UnmodifiedInterfaceDeclaration( specSet ) } void UnmodifiedInterfaceDeclaration( Java_SpecifierSet specSet ) : { String id ; } { { jjtThis.setSpecSet( specSet ) ; } "interface" id=Identifier() { jjtThis.setString( id ) ; } OptInterfaceExtendsClause() InterfaceBody() } void InterfaceBody() : {} { "{" ( InterfaceMemberDeclaration() )* "}" } void OptInterfaceExtendsClause() : {} { [ "extends" NameList() { jjtThis.setBool( true ) ; } ] } void InterfaceMemberDeclaration() # void : {} { LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" | "strictfp")* "class" ) NestedClassDeclaration() | LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" | "strictfp")* "interface" ) NestedInterfaceDeclaration() | LOOKAHEAD( MethodDeclarationLookahead() ) MethodDeclaration() | FieldDeclaration() | ";" } void FieldDeclaration() #void : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ( Public( specSet ) | Protected( specSet ) | Private( specSet ) | Static( specSet ) | Final( specSet ) | Transient( specSet ) | Volatile( specSet ) )* Type( specSet ) VariableDeclarator( specSet ) ( "," VariableDeclarator( specSet ) )* ";" } void VariableDeclarator( Java_SpecifierSet specSet ) : { String id ; int arrayDimCount = 0 ; } { { jjtThis.setSpecSet( specSet ) ; } id = Identifier() { jjtThis.setString( id ) ; } ( "[" "]" { arrayDimCount++ ;} )* { jjtThis.setInt( arrayDimCount ) ; } [ "=" VariableInitializer() ] } void VariableInitializer() #void : {} { ArrayInitializer() | Expression() } void ArrayInitializer() : {} { "{" [ VariableInitializer() ( LOOKAHEAD(2) "," VariableInitializer() )* ] [ "," ] "}" } void ConstructorDeclaration() : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { [ Public( specSet ) | Protected( specSet ) | Private( specSet ) ] ConstructorDeclarator( specSet ) OptThrowsClause() ConstructorBlock() } void ConstructorDeclarator( Java_SpecifierSet specSet ) : { String id ; int arrayDimCount = 0 ; } { { jjtThis.setSpecSet( specSet ) ; } id=Identifier() { jjtThis.setString( id ) ; } FormalParameters() } void OptThrowsClause() : {} { [ "throws" NameList() { jjtThis.setBool( true ) ; } ] } void MethodDeclaration() : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ( Public( specSet ) | Protected( specSet )| Private( specSet ) | Static( specSet ) | Abstract( specSet ) | Final( specSet ) | Native( specSet ) | Synchronized( specSet ) | Strictfp( specSet ) )* ResultType( specSet ) MethodDeclarator( specSet ) // TSN 16 Aug 2002. We could capture information about whether throws clause is present. OptThrowsClause() ( Block() | ";" ) } void MethodDeclarator( Java_SpecifierSet specSet ) : { String id ; int arrayDimCount = 0 ; } { { jjtThis.setSpecSet( specSet ) ; } id=Identifier() { jjtThis.setString( id ) ; } FormalParameters() ( "[" "]" {arrayDimCount++;} )* { jjtThis.setInt( arrayDimCount ) ; } } void FormalParameters() : {} { "(" [ FormalParameter() ( "," FormalParameter() )* ] ")" } void FormalParameter() : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; String id ; int arrayDimCount = 0 ; } { [ Final( specSet) ] Type( specSet ) { jjtThis.setSpecSet( specSet ) ; } id = Identifier() { jjtThis.setString( id ) ; } ( "[" "]" { arrayDimCount++ ;} )* { jjtThis.setInt( arrayDimCount ) ; } } void ConstructorBlock() : {} { "{" [ LOOKAHEAD(ExplicitConstructorInvocation()) ExplicitConstructorInvocation() { jjtThis.setBool( true ) ; } ] ( BlockStatement() )* "}" } void ExplicitConstructorInvocation() #void : {} // TSN 16 Aug 2002. We could make the two cases easier to distinguish. { LOOKAHEAD("this" Arguments() ";") ThisExplicitConstructorInvocation() | SuperExplicitConstructorInvocation() } void ThisExplicitConstructorInvocation() : {} { "this" Arguments() ";" } void SuperExplicitConstructorInvocation() : {} { [ LOOKAHEAD(2) PrimaryExpression() "." { jjtThis.setBool( true ) ; } ] "super" Arguments() ";" } void Initializer() : {} { [ "static" {jjtThis.setBoolean( true ) ; } ] Block() } /* * Type, name and expression syntax follows. */ void Type( Java_SpecifierSet specSet ) #void : { int arrayDimCount = 0 ; ScopedName name ; } { ( PrimitiveType( specSet ) | name=Name() { specSet.add_type_name( name ) ; } ) ( "[" "]" {arrayDimCount++;} )* { specSet.setArrayDimCount( arrayDimCount ) ; } } void PrimitiveType( Java_SpecifierSet specSet )#void : {} // TSN 16 Aug 2002. We could note the token. { "boolean" { specSet.add( Java_Specifiers.SP_BOOLEAN ) ; } | "char" { specSet.add( Java_Specifiers.SP_CHAR ) ; } | "byte" { specSet.add( Java_Specifiers.SP_BYTE ) ; } | "short" { specSet.add( Java_Specifiers.SP_SHORT ) ; } | "int" { specSet.add( Java_Specifiers.SP_INT ) ; } | "long" { specSet.add( Java_Specifiers.SP_LONG ) ; } | "float" { specSet.add( Java_Specifiers.SP_FLOAT ) ; } | "double" { specSet.add( Java_Specifiers.SP_DOUBLE ) ; } } void ResultType( Java_SpecifierSet specSet ) #void : {} { "void" { specSet.add( Java_Specifiers.SP_VOID ) ; } | Type( specSet ) } ScopedName Name() #void : /* * A lookahead of 2 is required below since "Name" can be followed * by a ".*" when used in the context of an "ImportDeclaration". */ { ScopedName name ; String id ; } { id=Identifier() { name = new ScopedName( id ) ; } ( LOOKAHEAD(2) "." id=Identifier() { name.append( id ) ; } )* { return name ; } } void NameList() #void : {} { NameExp() ( "," NameExp() )* } /* * Expression syntax follows. */ void Expression() #void : /* * This expansion has been written this way instead of: * Assignment() | ConditionalExpression() * for performance reasons. * However, it is a weakening of the grammar for it allows the LHS of * assignments to be any conditional expression whereas it can only be * a primary expression. Consider adding a semantic predicate to work * around this. */ { Token t ; } { ConditionalExpression() [ t=AssignmentOperator() Expression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) ] } Token AssignmentOperator() #void : { Token t ; } { ( t="=" | t="*=" | t="/=" | t="%=" | t="+=" | t="-=" | t="<<=" | t=">>=" | t=">>>=" | t="&=" | t="^=" | t="|=" ) {return t ;} } void ConditionalExpression() #void : {} { ( ConditionalOrExpression() [ "?" Expression() ":" ConditionalExpression() #Conditional( 3 ) ] ) } void ConditionalOrExpression() #void : { Token t; } { ConditionalAndExpression() ( t="||" ConditionalAndExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void ConditionalAndExpression() #void : { Token t; } { InclusiveOrExpression() ( t="&&" InclusiveOrExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void InclusiveOrExpression() #void : { Token t ; } { ExclusiveOrExpression() ( t="|" ExclusiveOrExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void ExclusiveOrExpression() #void : { Token t ; } { AndExpression() ( t="^" AndExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void AndExpression() #void : { Token t ; } { EqualityExpression() ( t="&" EqualityExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void EqualityExpression() #void : { Token t ; } { InstanceOfExpression() ( ( t="==" | t="!=" ) InstanceOfExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void InstanceOfExpression() #void : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ( RelationalExpression() [ "instanceof" Type(specSet) { jjtThis.setSpecSet( specSet ) ; } #InstanceOf( 1 ) ] ) } void RelationalExpression() #void : { Token t ; } { ShiftExpression() ( ( t="<" | t=">" | t="<=" | t=">=") ShiftExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void ShiftExpression() #void : { Token t ; } { AdditiveExpression() ( ( t="<<" | t=">>" | t=">>>") AdditiveExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void AdditiveExpression() #void : { Token t ; } { MultiplicativeExpression() ( ( t="+" | t="-") MultiplicativeExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void MultiplicativeExpression() #void : { Token t ; } { UnaryExpression() ( ( t="*" | t="/" | t="%") UnaryExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) )* } void UnaryExpression() #void : { Token t ; } { ( t="+" | t="-") UnaryExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #Unary_prefix( 1 ) | PreIncDecExpression() | UnaryExpressionNotPlusMinus() } void PreIncDecExpression() #void : { Token t ; } { t=IncDecOperator() PrimaryExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #Unary_prefix( 1 ) } Token IncDecOperator() #void : { Token t ; } { ( t="++" | t="--") {return t ;} } void UnaryExpressionNotPlusMinus() #void : { Token t ; } { ( t="~" | t="!") UnaryExpression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #Unary_prefix( 1 ) | LOOKAHEAD( CastLookahead() ) CastExpression() | PostfixExpression() } // This production is to determine lookahead only. The LOOKAHEAD specifications // below are not used, but they are there just to indicate that we know about // this. void CastLookahead() #void : {} { LOOKAHEAD(2) "(" PrimitiveType( null ) | LOOKAHEAD("(" Name() "[") "(" Name() "[" "]" | "(" Name() ")" ( "~" | "!" | "(" | | "this" | "super" | "new" | Literal() ) } void PostfixExpression() #void : { Token t ; } { PrimaryExpression() [ t=IncDecOperator() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #Unary_postfix( 1 ) ] } void CastExpression() : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ( LOOKAHEAD("(" PrimitiveType()) "(" Type(specSet) ")" UnaryExpression() | "(" Type(specSet) ")" UnaryExpressionNotPlusMinus() ) { jjtThis.setSpecSet( specSet ) ; } } void PrimaryExpression() #void : {} { PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )* } void PrimaryPrefix() #void : {} { Literal() | ThisExp() | SuperExp() "." IdentifierNode() #Dot(2) | "(" Expression() ")" #Parens(1) | AllocationExpression() | LOOKAHEAD( ResultType() "." "class" ) ClassExp() | NameExp() } void ThisExp() : {} { "this" } void SuperExp() : {} { "super" } void ClassExp() : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { ResultType(specSet) "." "class" { jjtThis.setSpecSet( specSet ) ; } } void NameExp() : { ScopedName name ; } { name=Name() { jjtThis.setName( name ) ; } } void PrimarySuffix() #void : {} { LOOKAHEAD(2) "." ThisExp() #Dot( 2 ) | LOOKAHEAD(2) "." AllocationExpression() #Dot( 2 ) | "[" Expression() "]" #Subscript( 2 ) | "." IdentifierNode() #Dot( 2 ) | Arguments() #Call( 2 ) } void IdentifierNode() : { String id; } { id = Identifier() { jjtThis.setString( id ) ; } } void Literal() : { Token t ;} { ( t= | t= | t= | t= | t= | t= | t="true" | t="false" | t="null" ) { jjtThis.setInt( t.kind ) ; } { jjtThis.setString( idTable.inter( t.image ) ) ; } } void Arguments() : {} { "(" [ Expression() ( "," Expression() )* ] ")" } void AllocationExpression() : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; ScopedName name ; } { ( // Array ultimately of primitive type. LOOKAHEAD(2) "new" PrimitiveType(specSet) ArrayDimsAndInits() | "new" name=Name() { specSet.add_type_name( name ) ; } ( // Array ultimately of nonprimitive type. ArrayDimsAndInits() | // Not an array. Arguments() [ ClassBody() ] {jjtThis.setBool( true ) ;} ) ) {jjtThis.setSpecSet( specSet );} } /* * The second LOOKAHEAD specification below is to parse to PrimarySuffix * if there is an expression between the "[...]". */ void ArrayDimsAndInits() : { int arrayDimCount = 0 ; } // If bool parameter is true, then there is an array initializer, // which is the only child. // For example new A[][]{{1,2},{3,4}}. // If the bool parameter is false then there is no array initiailizer // and the children are the array dimensions. // For example new A[3][4][][]. // In either case the int parameter is the number of "extra" // dimensions. I.e. dimensions in the type, but that are not // allocated along. { LOOKAHEAD(2) ( LOOKAHEAD(2) "[" Expression() "]" )+ ( LOOKAHEAD(2) "[" "]" {++arrayDimCount;} )* {jjtThis.setInt( arrayDimCount ) ; } | ( "[" "]" {++arrayDimCount;} )+ ArrayInitializer() {jjtThis.setInt( arrayDimCount ) ; } { jjtThis.setBool( true ) ; } } /* * Statement syntax follows. */ void Statement() #void : {} { LOOKAHEAD(2) LabeledStatement() | Block() | EmptyStatement() | ExpressionStatement() | SwitchStatement() | IfStatement() | WhileStatement() | DoStatement() | ForStatement() | BreakStatement() | ContinueStatement() | ReturnStatement() | ThrowStatement() | SynchronizedStatement() | TryStatement() } void LabeledStatement() : { String id ; } { id=Identifier() ":" Statement() { jjtThis.setString( id ) ; } } void Block() : {} { "{" ( BlockStatement() )* "}" } void BlockStatement() #void : {} { LOOKAHEAD([ "final" ] Type() ) LocalVariableDeclaration() ";" | Statement() | UnmodifiedClassDeclaration(new Java_SpecifierSet()) | UnmodifiedInterfaceDeclaration(new Java_SpecifierSet()) } void LocalVariableDeclaration() # void : { Java_SpecifierSet specSet = new Java_SpecifierSet() ; } { [ Final( specSet ) ] Type( specSet ) VariableDeclarator( specSet ) ( "," VariableDeclarator( specSet ) )* } void EmptyStatement() : {} { ";" } void ExpressionStatement() : {} { StatementExpression() ";" } void StatementExpression() #void : /* * The last expansion of this production accepts more than the legal * Java expansions for StatementExpression. This expansion does not * use PostfixExpression for performance reasons. */ { Token t ; } { PreIncDecExpression() | PrimaryExpression() [ t=IncDecOperator() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #Unary_postfix( 1 ) | t=AssignmentOperator() Expression() {jjtThis.setInt( t.kind ); jjtThis.setString( idTable.inter( t.image ) );} #BinOp(2) ] } void SwitchStatement() : {} { "switch" "(" Expression() ")" "{" ( Case() )* "}" } void Case() : {} { SwitchLabel() ( BlockStatement() )* } void SwitchLabel() : {} { "case" Expression() ":" { jjtThis.setBool( true ) ; } | "default" ":" } void IfStatement() : /* * The disambiguating algorithm of JavaCC automatically binds dangling * else's to the innermost if statement. The LOOKAHEAD specification * is to tell JavaCC that we know what we are doing. */ {} { "if" "(" Expression() ")" Statement() [ LOOKAHEAD(1) "else" Statement() ] } void WhileStatement() : {} { "while" "(" Expression() ")" Statement() } void DoStatement() : {} { "do" Statement() "while" "(" Expression() ")" ";" } void ForStatement() : {} { "for" "(" OptForInit() ";" OptExpression() ";" OptForUpdate() ")" Statement() } void OptForInit() : {} { [ ForInit() { jjtThis.setBool( true ) ; } ] } void ForInit() : {} { LOOKAHEAD( [ "final" ] Type() ) LocalVariableDeclaration() {jjtThis.setBool( true ) ; } | StatementExpressionList() } void StatementExpressionList() #void : {} { StatementExpression() ( "," StatementExpression() )* } void OptExpression() : {} { [ Expression() { jjtThis.setBool( true ) ; } ] } void OptForUpdate() : {} { [ StatementExpressionList() ] } void BreakStatement() : { String id ; } { "break" [ id=Identifier() { jjtThis.setBool( true ) ; jjtThis.setString(id); }] ";" } void ContinueStatement() : { String id ; } { "continue" [ id=Identifier() { jjtThis.setBool( true ) ; jjtThis.setString(id); }] ";" } void ReturnStatement() : {} { "return" [ Expression() { jjtThis.setBool( true );} ] ";" } void ThrowStatement() : {} { "throw" Expression() ";" } void SynchronizedStatement() : {} { "synchronized" "(" Expression() ")" Block() } void TryStatement() : {} { "try" Block() ( LOOKAHEAD( "catch" ) Catches() ["finally" Block() { jjtThis.setBool( true );}] | Catches() "finally" Block() { jjtThis.setBool( true );} ) } void Catches() : {} { ( Catch() )* } void Catch() : {} { "catch" "(" FormalParameter() ")" Block() } String Identifier() #void : { Token t ; } { t = { return idTable.inter( t.image ) ; } } void Abstract(Java_SpecifierSet specSet) #void : {} { "abstract" { specSet.add( Java_Specifiers.SP_ABSTRACT ) ; } } void Final(Java_SpecifierSet specSet) #void : {} { "final" { specSet.add( Java_Specifiers.SP_FINAL ) ; } } void Native(Java_SpecifierSet specSet) #void : {} { "native" { specSet.add( Java_Specifiers.SP_NATIVE ) ; } } void Public(Java_SpecifierSet specSet) #void : {} { "public" { specSet.add( Java_Specifiers.SP_PUBLIC ) ; } } void Private(Java_SpecifierSet specSet) #void : {} { "private" { specSet.add( Java_Specifiers.SP_PRIVATE ) ; } } void Protected(Java_SpecifierSet specSet) #void : {} { "protected" { specSet.add( Java_Specifiers.SP_PROTECTED ) ; } } void Static(Java_SpecifierSet specSet) #void : {} { "static" { specSet.add( Java_Specifiers.SP_STATIC ) ; } } void Strictfp(Java_SpecifierSet specSet) #void : {} { "strictfp" { specSet.add( Java_Specifiers.SP_STRICTFP ) ; } } void Synchronized(Java_SpecifierSet specSet) #void : {} { "synchronized" { specSet.add( Java_Specifiers.SP_SYNCHRONIZED ) ; } } void Transient(Java_SpecifierSet specSet) #void : {} { "transient" { specSet.add( Java_Specifiers.SP_TRANSIENT ) ; } } void Volatile(Java_SpecifierSet specSet) #void : {} { "volatile" { specSet.add( Java_Specifiers.SP_VOLATILE ) ; } }