/***************************************** * THE JAVA LANGUAGE GRAMMAR STARTS HERE * *****************************************/ /* * Program structuring syntax follows. */
| CompilationUnit | ::= | ( PackageDeclaration )? ( ImportDeclaration )* ( TypeDeclaration )* <EOF> |
| PackageDeclaration | ::= | "package" Name ";" |
| ImportDeclaration | ::= | "import" Name ( "." "*" )? ";" |
| TypeDeclaration | ::= | ClassDeclaration |
| | | InterfaceDeclaration | |
| | | ";" |
/* * Declaration syntax follows. */
| ClassDeclaration | ::= | ( "abstract" | "final" | "public" )* UnmodifiedClassDeclaration |
| UnmodifiedClassDeclaration | ::= | "class" <IDENTIFIER> ( "extends" Name )? ( "implements" NameList )? ClassBody |
| ClassBody | ::= | "{" ( ClassBodyDeclaration )* "}" |
| NestedClassDeclaration | ::= | ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* UnmodifiedClassDeclaration |
| ClassBodyDeclaration | ::= | Initializer |
| | | NestedClassDeclaration | |
| | | NestedInterfaceDeclaration | |
| | | ConstructorDeclaration | |
| | | MethodDeclaration | |
| | | FieldDeclaration |
// This production is to determine lookahead only.
| MethodDeclarationLookahead | ::= | ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" )* ResultType <IDENTIFIER> "(" |
| InterfaceDeclaration | ::= | ( "abstract" | "public" )* UnmodifiedInterfaceDeclaration |
| NestedInterfaceDeclaration | ::= | ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* UnmodifiedInterfaceDeclaration |
| UnmodifiedInterfaceDeclaration | ::= | "interface" <IDENTIFIER> ( "extends" NameList )? "{" ( InterfaceMemberDeclaration )* "}" |
| InterfaceMemberDeclaration | ::= | NestedClassDeclaration |
| | | NestedInterfaceDeclaration | |
| | | MethodDeclaration | |
| | | FieldDeclaration |
| FieldDeclaration | ::= | ( "public" | "protected" | "private" | "static" | "final" | "transient" | "volatile" )* Type VariableDeclarator ( "," VariableDeclarator )* ";" |
| VariableDeclarator | ::= | VariableDeclaratorId ( "=" VariableInitializer )? |
| VariableDeclaratorId | ::= | <IDENTIFIER> ( "[" "]" )* |
| VariableInitializer | ::= | ArrayInitializer |
| | | Expression |
| ArrayInitializer | ::= | "{" ( VariableInitializer ( "," VariableInitializer )* )? ( "," )? "}" |
| MethodDeclaration | ::= | ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" )* ResultType MethodDeclarator ( "throws" NameList )? ( Block | ";" ) |
| MethodDeclarator | ::= | <IDENTIFIER> FormalParameters ( "[" "]" )* |
| FormalParameters | ::= | "(" ( FormalParameter ( "," FormalParameter )* )? ")" |
| FormalParameter | ::= | ( "final" )? Type VariableDeclaratorId |
| ConstructorDeclaration | ::= | ( "public" | "protected" | "private" )? <IDENTIFIER> FormalParameters ( "throws" NameList )? "{" ( ExplicitConstructorInvocation )? ( BlockStatement )* "}" |
| ExplicitConstructorInvocation | ::= | "this" Arguments ";" |
| | | ( PrimaryExpression "." )? "super" Arguments ";" |
| Initializer | ::= | ( "static" )? Block |
/* * Type, name and expression syntax follows. */
| Type | ::= | ( PrimitiveType | Name ) ( "[" "]" )* |
| PrimitiveType | ::= | "boolean" |
| | | "char" | |
| | | "byte" | |
| | | "short" | |
| | | "int" | |
| | | "long" | |
| | | "float" | |
| | | "double" |
| ResultType | ::= | "void" |
| | | Type |
| Name | ::= | <IDENTIFIER> ( "." <IDENTIFIER> )* |
| NameList | ::= | Name ( "," Name )* |
/* * Expression syntax follows. */
| Expression | ::= | ConditionalExpression ( AssignmentOperator Expression )? |
| AssignmentOperator | ::= | "=" |
| | | "*=" | |
| | | "/=" | |
| | | "%=" | |
| | | "+=" | |
| | | "-=" | |
| | | "<<=" | |
| | | ">>=" | |
| | | ">>>=" | |
| | | "&=" | |
| | | "^=" | |
| | | "|=" |
| ConditionalExpression | ::= | ConditionalOrExpression ( "?" Expression ":" ConditionalExpression )? |
| ConditionalOrExpression | ::= | ConditionalAndExpression ( "||" ConditionalAndExpression )* |
| ConditionalAndExpression | ::= | InclusiveOrExpression ( "&&" InclusiveOrExpression )* |
| InclusiveOrExpression | ::= | ExclusiveOrExpression ( "|" ExclusiveOrExpression )* |
| ExclusiveOrExpression | ::= | AndExpression ( "^" AndExpression )* |
| AndExpression | ::= | EqualityExpression ( "&" EqualityExpression )* |
| EqualityExpression | ::= | InstanceOfExpression ( ( "==" | "!=" ) InstanceOfExpression )* |
| InstanceOfExpression | ::= | RelationalExpression ( "instanceof" Type )? |
| RelationalExpression | ::= | ShiftExpression ( ( "<" | ">" | "<=" | ">=" ) ShiftExpression )* |
| ShiftExpression | ::= | AdditiveExpression ( ( "<<" | ">>" | ">>>" ) AdditiveExpression )* |
| AdditiveExpression | ::= | MultiplicativeExpression ( ( "+" | "-" ) MultiplicativeExpression )* |
| MultiplicativeExpression | ::= | UnaryExpression ( ( "*" | "/" | "%" ) UnaryExpression )* |
| UnaryExpression | ::= | ( "+" | "-" ) UnaryExpression |
| | | PreIncrementExpression | |
| | | PreDecrementExpression | |
| | | UnaryExpressionNotPlusMinus |
| PreIncrementExpression | ::= | "++" PrimaryExpression |
| PreDecrementExpression | ::= | "--" PrimaryExpression |
| 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 | ::= | "(" PrimitiveType |
| | | "(" Name "[" "]" | |
| | | "(" Name ")" ( "~" | "!" | "(" | <IDENTIFIER> | "this" | "super" | "new" | Literal ) |
| PostfixExpression | ::= | PrimaryExpression ( "++" | "--" )? |
| CastExpression | ::= | "(" Type ")" UnaryExpression |
| | | "(" Type ")" UnaryExpressionNotPlusMinus |
| PrimaryExpression | ::= | PrimaryPrefix ( PrimarySuffix )* |
| PrimaryPrefix | ::= | Literal |
| | | "this" | |
| | | "super" "." <IDENTIFIER> | |
| | | "(" Expression ")" | |
| | | AllocationExpression | |
| | | ResultType "." "class" | |
| | | Name |
| PrimarySuffix | ::= | "." "this" |
| | | "." AllocationExpression | |
| | | "[" Expression "]" | |
| | | "." <IDENTIFIER> | |
| | | Arguments |
| Literal | ::= | <INTEGER_LITERAL> |
| | | <FLOATING_POINT_LITERAL> | |
| | | <CHARACTER_LITERAL> | |
| | | <STRING_LITERAL> | |
| | | BooleanLiteral | |
| | | NullLiteral |
| BooleanLiteral | ::= | "true" |
| | | "false" |
| NullLiteral | ::= | "null" |
| Arguments | ::= | "(" ( ArgumentList )? ")" |
| ArgumentList | ::= | Expression ( "," Expression )* |
| 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 | ::= | ( "[" Expression "]" )+ ( "[" "]" )* |
| | | ( "[" "]" )+ ArrayInitializer |
/* * Statement syntax follows. */
| Statement | ::= | LabeledStatement |
| | | Block | |
| | | EmptyStatement | |
| | | StatementExpression ";" | |
| | | SwitchStatement | |
| | | IfStatement | |
| | | WhileStatement | |
| | | DoStatement | |
| | | ForStatement | |
| | | BreakStatement | |
| | | ContinueStatement | |
| | | ReturnStatement | |
| | | ThrowStatement | |
| | | SynchronizedStatement | |
| | | TryStatement |
| LabeledStatement | ::= | <IDENTIFIER> ":" Statement |
| Block | ::= | "{" ( BlockStatement )* "}" |
| BlockStatement | ::= | LocalVariableDeclaration ";" |
| | | Statement | |
| | | UnmodifiedClassDeclaration | |
| | | UnmodifiedInterfaceDeclaration |
| LocalVariableDeclaration | ::= | ( "final" )? Type VariableDeclarator ( "," VariableDeclarator )* |
| EmptyStatement | ::= | ";" |
| StatementExpression | ::= | PreIncrementExpression |
| | | PreDecrementExpression | |
| | | PrimaryExpression ( "++" | "--" | AssignmentOperator Expression )? |
| SwitchStatement | ::= | "switch" "(" Expression ")" "{" ( SwitchLabel ( BlockStatement )* )* "}" |
| SwitchLabel | ::= | "case" Expression ":" |
| | | "default" ":" |
| IfStatement | ::= | "if" "(" Expression ")" Statement ( "else" Statement )? |
| WhileStatement | ::= | "while" "(" Expression ")" Statement |
| DoStatement | ::= | "do" Statement "while" "(" Expression ")" ";" |
| ForStatement | ::= | "for" "(" ( ForInit )? ";" ( Expression )? ";" ( ForUpdate )? ")" Statement |
| ForInit | ::= | LocalVariableDeclaration |
| | | StatementExpressionList |
| StatementExpressionList | ::= | StatementExpression ( "," StatementExpression )* |
| ForUpdate | ::= | StatementExpressionList |
| BreakStatement | ::= | "break" ( <IDENTIFIER> )? ";" |
| ContinueStatement | ::= | "continue" ( <IDENTIFIER> )? ";" |
| ReturnStatement | ::= | "return" ( Expression )? ";" |
| ThrowStatement | ::= | "throw" Expression ";" |
| SynchronizedStatement | ::= | "synchronized" "(" Expression ")" Block |
| TryStatement | ::= | "try" Block ( "catch" "(" FormalParameter ")" Block )* ( "finally" Block )? |