DOCUMENT START NON-TERMINALS /***************************************** * THE JAVA LANGUAGE GRAMMAR STARTS HERE * *****************************************/ /* * Program structuring syntax follows. */ /* CompilationUnit OLDCompilationUnit() : { Expression e; FormalParameters params; } { params = FormalParameters() "=" "(" e = Expression() ")" { return new CompilationUnit(params, e); } } */ /** This is a test comment. */ CompilationUnit := PackageDeclaration ClassDeclaration NormalCompilationUnit := ( PackageDeclaration )? ( ImportDeclaration )* ( TypeDeclaration )* PackageDeclaration := "package" Name ";" ImportDeclaration := "import" Name ( "." "*" )? ";" TypeDeclaration := ClassDeclaration | InterfaceDeclaration | ";" /* * Declaration syntax follows. */ NormalClassDeclaration := ( "abstract" | "final" | "public" | "strictfp" )* UnmodifiedClassDeclaration ClassDeclaration := "class" "{" ( MethodDeclaration )+ "}" UnmodifiedClassDeclaration := "class" ( "extends" Name )? ( "implements" NameList )? ClassBody ClassBody := "{" ( ClassBodyDeclaration )* "}" NestedClassDeclaration := ( "static" | "abstract" | "final" | "public" | "protected" | "private" | "strictfp" )* UnmodifiedClassDeclaration ClassBodyDeclaration := Initializer | NestedClassDeclaration | NestedInterfaceDeclaration | ConstructorDeclaration | MethodDeclaration | FieldDeclaration // This production is to determine lookahead only. MethodDeclarationLookahead := ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" | "strictfp" )* ResultType "(" InterfaceDeclaration := ( "abstract" | "public" | "strictfp" )* UnmodifiedInterfaceDeclaration NestedInterfaceDeclaration := ( "static" | "abstract" | "final" | "public" | "protected" | "private" | "strictfp" )* UnmodifiedInterfaceDeclaration UnmodifiedInterfaceDeclaration := "interface" ( "extends" NameList )? "{" ( InterfaceMemberDeclaration )* "}" InterfaceMemberDeclaration := NestedClassDeclaration | NestedInterfaceDeclaration | MethodDeclaration | FieldDeclaration FieldDeclaration := ( "public" | "protected" | "private" | "static" | "final" | "transient" | "volatile" )* Type VariableDeclarator ( "," VariableDeclarator )* ";" VariableDeclarator := VariableDeclaratorId ( "=" VariableInitializer )? VariableDeclaratorId := ( "[" "]" )* VariableInitializer := ArrayInitializer | Expression ArrayInitializer := "{" ( VariableInitializer ( "," VariableInitializer )* )? ( "," )? "}" NormalMethodDeclaration := ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" | "strictfp" )* ResultType MethodDeclarator ( "throws" NameList )? ( Block | ";" ) MethodDeclaration := ResultType FormalParameters Block MethodDeclarator := FormalParameters ( "[" "]" )* FormalParameters := "(" ( FormalParameter ( "," FormalParameter )* )? ")" FormalParameter := ( "final" )? Type VariableDeclaratorId ConstructorDeclaration := ( "public" | "protected" | "private" )? FormalParameters ( "throws" NameList )? "{" ( ExplicitConstructorInvocation )? ( BlockStatement )* "}" ExplicitConstructorInvocation := "this" Arguments ";" | ( PrimaryExpression "." )? "super" Arguments ";" Initializer := ( "static" )? Block /* * Type, name and expression syntax follows. */ Type := ( PrimitiveType | TypeName ) ( "[" "]" )* TypeName := ( "." )* PrimitiveType := ( "boolean" | "char" | "byte" | "short" | "int" | "long" | "float" | "double" ) ResultType := "void" | Type Name := ( "." )* 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 ")" ( "~" | "!" | "(" | | "this" | "super" | "new" | Literal ) PostfixExpression := PrimaryExpression ( "++" | "--" )? CastExpression := ( "(" Type ")" UnaryExpression | "(" Type ")" UnaryExpressionNotPlusMinus ) PrimaryExpression := PrimaryPrefix ( PrimarySuffix )* PrimaryPrefix := ( Literal | "this" | "super" "." | "(" Expression ")" | AllocationExpression | ResultType "." "class" | ( ( "." )* ) ) PrimarySuffix := ( "." "this" | "." AllocationExpression | "[" Expression "]" | "." | Arguments ) Literal := | | | | BooleanLiteral | NullLiteral BooleanLiteral := "true" | "false" NullLiteral := "null" Arguments := "(" ( ArgumentList )? ")" ArgumentList := Expression ( "," Expression )* AllocationExpression := ( "new" PrimitiveType ArrayDimsAndInits | "new" Name ( ArrayDimsAndInits | Arguments ( ClassBody )? ) ) /* * The second 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 := ":" 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" ( )? ";" ContinueStatement := "continue" ( )? ";" ReturnStatement := "return" ( Expression )? ";" ThrowStatement := "throw" Expression ";" SynchronizedStatement := "synchronized" "(" Expression ")" Block TryStatement := "try" Block ( "catch" "(" FormalParameter ")" Block )* ( "finally" Block )? DOCUMENT END