BNF for Java1.1.jj

NON-TERMINALS

/*****************************************
 * THE JAVA LANGUAGE GRAMMAR STARTS HERE *
 *****************************************/

/*
 * Program structuring syntax follows.
 */
CompilationUnit
CompilationUnit ::= ( PackageDeclaration )? ( ImportDeclaration )* ( TypeDeclaration )* <EOF>



PackageDeclaration
PackageDeclaration ::= "package" Name ";"



ImportDeclaration
ImportDeclaration ::= "import" Name ( "." "*" )? ";"



TypeDeclaration
TypeDeclaration ::= ClassDeclaration
| InterfaceDeclaration
| ";"

/*
 * Declaration syntax follows.
 */
ClassDeclaration
ClassDeclaration ::= ( "abstract" | "final" | "public" )* UnmodifiedClassDeclaration



UnmodifiedClassDeclaration
UnmodifiedClassDeclaration ::= "class" <IDENTIFIER> ( "extends" Name )? ( "implements" NameList )? ClassBody



ClassBody
ClassBody ::= "{" ( ClassBodyDeclaration )* "}"



NestedClassDeclaration
NestedClassDeclaration ::= ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* UnmodifiedClassDeclaration



ClassBodyDeclaration
ClassBodyDeclaration ::= Initializer
| NestedClassDeclaration
| NestedInterfaceDeclaration
| ConstructorDeclaration
| MethodDeclaration
| FieldDeclaration

// This production is to determine lookahead only.
MethodDeclarationLookahead
MethodDeclarationLookahead ::= ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" )* ResultType <IDENTIFIER> "("



InterfaceDeclaration
InterfaceDeclaration ::= ( "abstract" | "public" )* UnmodifiedInterfaceDeclaration



NestedInterfaceDeclaration
NestedInterfaceDeclaration ::= ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* UnmodifiedInterfaceDeclaration



UnmodifiedInterfaceDeclaration
UnmodifiedInterfaceDeclaration ::= "interface" <IDENTIFIER> ( "extends" NameList )? "{" ( InterfaceMemberDeclaration )* "}"



InterfaceMemberDeclaration
InterfaceMemberDeclaration ::= NestedClassDeclaration
| NestedInterfaceDeclaration
| MethodDeclaration
| FieldDeclaration



FieldDeclaration
FieldDeclaration ::= ( "public" | "protected" | "private" | "static" | "final" | "transient" | "volatile" )* Type VariableDeclarator ( "," VariableDeclarator )* ";"



VariableDeclarator
VariableDeclarator ::= VariableDeclaratorId ( "=" VariableInitializer )?



VariableDeclaratorId
VariableDeclaratorId ::= <IDENTIFIER> ( "[" "]" )*



VariableInitializer
VariableInitializer ::= ArrayInitializer
| Expression



ArrayInitializer
ArrayInitializer ::= "{" ( VariableInitializer ( "," VariableInitializer )* )? ( "," )? "}"



MethodDeclaration
MethodDeclaration ::= ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" )* ResultType MethodDeclarator ( "throws" NameList )? ( Block | ";" )



MethodDeclarator
MethodDeclarator ::= <IDENTIFIER> FormalParameters ( "[" "]" )*



FormalParameters
FormalParameters ::= "(" ( FormalParameter ( "," FormalParameter )* )? ")"



FormalParameter
FormalParameter ::= ( "final" )? Type VariableDeclaratorId



ConstructorDeclaration
ConstructorDeclaration ::= ( "public" | "protected" | "private" )? <IDENTIFIER> FormalParameters ( "throws" NameList )? "{" ( ExplicitConstructorInvocation )? ( BlockStatement )* "}"



ExplicitConstructorInvocation
ExplicitConstructorInvocation ::= "this" Arguments ";"
| ( PrimaryExpression "." )? "super" Arguments ";"



Initializer
Initializer ::= ( "static" )? Block

/*
 * Type, name and expression syntax follows.
 */
Type
Type ::= ( PrimitiveType | Name ) ( "[" "]" )*



PrimitiveType
PrimitiveType ::= "boolean"
| "char"
| "byte"
| "short"
| "int"
| "long"
| "float"
| "double"



ResultType
ResultType ::= "void"
| Type



Name
Name ::= <IDENTIFIER> ( "." <IDENTIFIER> )*



NameList
NameList ::= Name ( "," Name )*

/*
 * Expression syntax follows.
 */
Expression
Expression ::= ConditionalExpression ( AssignmentOperator Expression )?



AssignmentOperator
AssignmentOperator ::= "="
| "*="
| "/="
| "%="
| "+="
| "-="
| "<<="
| ">>="
| ">>>="
| "&="
| "^="
| "|="



ConditionalExpression
ConditionalExpression ::= ConditionalOrExpression ( "?" Expression ":" ConditionalExpression )?



ConditionalOrExpression
ConditionalOrExpression ::= ConditionalAndExpression ( "||" ConditionalAndExpression )*



ConditionalAndExpression
ConditionalAndExpression ::= InclusiveOrExpression ( "&&" InclusiveOrExpression )*



InclusiveOrExpression
InclusiveOrExpression ::= ExclusiveOrExpression ( "|" ExclusiveOrExpression )*



ExclusiveOrExpression
ExclusiveOrExpression ::= AndExpression ( "^" AndExpression )*



AndExpression
AndExpression ::= EqualityExpression ( "&" EqualityExpression )*



EqualityExpression
EqualityExpression ::= InstanceOfExpression ( ( "==" | "!=" ) InstanceOfExpression )*



InstanceOfExpression
InstanceOfExpression ::= RelationalExpression ( "instanceof" Type )?



RelationalExpression
RelationalExpression ::= ShiftExpression ( ( "<" | ">" | "<=" | ">=" ) ShiftExpression )*



ShiftExpression
ShiftExpression ::= AdditiveExpression ( ( "<<" | ">>" | ">>>" ) AdditiveExpression )*



AdditiveExpression
AdditiveExpression ::= MultiplicativeExpression ( ( "+" | "-" ) MultiplicativeExpression )*



MultiplicativeExpression
MultiplicativeExpression ::= UnaryExpression ( ( "*" | "/" | "%" ) UnaryExpression )*



UnaryExpression
UnaryExpression ::= ( "+" | "-" ) UnaryExpression
| PreIncrementExpression
| PreDecrementExpression
| UnaryExpressionNotPlusMinus



PreIncrementExpression
PreIncrementExpression ::= "++" PrimaryExpression



PreDecrementExpression
PreDecrementExpression ::= "--" PrimaryExpression



UnaryExpressionNotPlusMinus
UnaryExpressionNotPlusMinus ::= ( "~" | "!" ) UnaryExpression
| 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.
CastLookahead
CastLookahead ::= "(" PrimitiveType
| "(" Name "[" "]"
| "(" Name ")" ( "~" | "!" | "(" | <IDENTIFIER> | "this" | "super" | "new" | Literal )



PostfixExpression
PostfixExpression ::= PrimaryExpression ( "++" | "--" )?



CastExpression
CastExpression ::= "(" Type ")" UnaryExpression
| "(" Type ")" UnaryExpressionNotPlusMinus



PrimaryExpression
PrimaryExpression ::= PrimaryPrefix ( PrimarySuffix )*



PrimaryPrefix
PrimaryPrefix ::= Literal
| "this"
| "super" "." <IDENTIFIER>
| "(" Expression ")"
| AllocationExpression
| ResultType "." "class"
| Name



PrimarySuffix
PrimarySuffix ::= "." "this"
| "." AllocationExpression
| "[" Expression "]"
| "." <IDENTIFIER>
| Arguments



Literal
Literal ::= <INTEGER_LITERAL>
| <FLOATING_POINT_LITERAL>
| <CHARACTER_LITERAL>
| <STRING_LITERAL>
| BooleanLiteral
| NullLiteral



BooleanLiteral
BooleanLiteral ::= "true"
| "false"



NullLiteral
NullLiteral ::= "null"



Arguments
Arguments ::= "(" ( ArgumentList )? ")"



ArgumentList
ArgumentList ::= Expression ( "," Expression )*



AllocationExpression
AllocationExpression ::= "new" PrimitiveType ArrayDimsAndInits
| "new" Name ( ArrayDimsAndInits | Arguments ( ClassBody )? )

/*
 * The third LOOKAHEAD specification below is to parse to PrimarySuffix
 * if there is an expression between the "[...]".
 */
ArrayDimsAndInits
ArrayDimsAndInits ::= ( "[" Expression "]" )+ ( "[" "]" )*
| ( "[" "]" )+ ArrayInitializer

/*
 * Statement syntax follows.
 */
Statement
Statement ::= LabeledStatement
| Block
| EmptyStatement
| StatementExpression ";"
| SwitchStatement
| IfStatement
| WhileStatement
| DoStatement
| ForStatement
| BreakStatement
| ContinueStatement
| ReturnStatement
| ThrowStatement
| SynchronizedStatement
| TryStatement



LabeledStatement
LabeledStatement ::= <IDENTIFIER> ":" Statement



Block
Block ::= "{" ( BlockStatement )* "}"



BlockStatement
BlockStatement ::= LocalVariableDeclaration ";"
| Statement
| UnmodifiedClassDeclaration
| UnmodifiedInterfaceDeclaration



LocalVariableDeclaration
LocalVariableDeclaration ::= ( "final" )? Type VariableDeclarator ( "," VariableDeclarator )*



EmptyStatement
EmptyStatement ::= ";"



StatementExpression
StatementExpression ::= PreIncrementExpression
| PreDecrementExpression
| PrimaryExpression ( "++" | "--" | AssignmentOperator Expression )?



SwitchStatement
SwitchStatement ::= "switch" "(" Expression ")" "{" ( SwitchLabel ( BlockStatement )* )* "}"



SwitchLabel
SwitchLabel ::= "case" Expression ":"
| "default" ":"



IfStatement
IfStatement ::= "if" "(" Expression ")" Statement ( "else" Statement )?



WhileStatement
WhileStatement ::= "while" "(" Expression ")" Statement



DoStatement
DoStatement ::= "do" Statement "while" "(" Expression ")" ";"



ForStatement
ForStatement ::= "for" "(" ( ForInit )? ";" ( Expression )? ";" ( ForUpdate )? ")" Statement



ForInit
ForInit ::= LocalVariableDeclaration
| StatementExpressionList



StatementExpressionList
StatementExpressionList ::= StatementExpression ( "," StatementExpression )*



ForUpdate
ForUpdate ::= StatementExpressionList



BreakStatement
BreakStatement ::= "break" ( <IDENTIFIER> )? ";"



ContinueStatement
ContinueStatement ::= "continue" ( <IDENTIFIER> )? ";"



ReturnStatement
ReturnStatement ::= "return" ( Expression )? ";"



ThrowStatement
ThrowStatement ::= "throw" Expression ";"



SynchronizedStatement
SynchronizedStatement ::= "synchronized" "(" Expression ")" Block



TryStatement
TryStatement ::= "try" Block ( "catch" "(" FormalParameter ")" Block )* ( "finally" Block )?