Skip to content

Instantly share code, notes, and snippets.

@c2nes
Created September 18, 2012 14:26
Show Gist options
  • Save c2nes/3743422 to your computer and use it in GitHub Desktop.
Save c2nes/3743422 to your computer and use it in GitHub Desktop.
Java grammar extracted from JavacParser.java in the OpenJDK 7
Ident:
IDENTIFIER
Qualident:
Ident { DOT Ident }
Literal:
INTLITERAL
| LONGLITERAL
| FLOATLITERAL
| DOUBLELITERAL
| CHARLITERAL
| STRINGLITERAL
| TRUE
| FALSE
| NULL
Type:
Ident { "." Ident } [TypeArguments] {TypeSelector} BracketsOpt
| BasicType
BasicType:
BYTE | SHORT | CHAR | INT
| LONG | FLOAT | DOUBLE | BOOLEAN
TypeNoParams:
Ident { "." Ident } BracketsOpt
TypeSelector:
"." Ident [TypeArguments]
TypeArgumentsOpt:
[ TypeArguments ]
TypeArguments:
"<" TypeArgument {"," TypeArgument} ">"
TypeArgument:
Type
| "?"
| "?" EXTENDS Type {"&" Type}
| "?" SUPER Type
Expression:
Expression1 [ExpressionRest]
ExpressionRest:
[AssignmentOperator Expression1]
AssignmentOperator:
"=" | "+=" | "-=" | "*=" | "/="
| "&=" | "|=" | "^=" | "%="
| "<<=" | ">>=" | ">>>="
Expression1:
Expression2 [Expression1Rest]
StatementExpression:
Expression
ConstantExpression:
Expression
Expression1Rest:
["?" Expression ":" Expression1]
Expression2:
Expression3 [Expression2Rest]
Expression2Rest:
{infixop Expression3}
| Expression3 instanceof Type
infixop:
"||"
| "&&"
| "|"
| "^"
| "&"
| "==" | "!="
| "<" | ">" | "<=" | ">="
| "<<" | ">>" | ">>>"
| "+" | "-"
| "*" | "/" | "%"
PrefixOp:
"++" | "--" | "!" | "~" | "+" | "-"
PostfixOp:
"++" | "--"
Expression3:
PrefixOp Expression3
| "(" Expression | TypeNoParams ")" Expression3
| Primary {Selector} {PostfixOp}
Primary:
"(" Expression ")"
| Literal
| [TypeArguments] THIS [Arguments]
| [TypeArguments] SUPER SuperSuffix
| NEW [TypeArguments] Creator
| Ident { "." Ident }
[ "[" ( "]" BracketsOpt "." CLASS | Expression "]" )
| Arguments
| "." ( CLASS | THIS | [TypeArguments] SUPER Arguments | NEW [TypeArguments] InnerCreator )
]
| BasicType BracketsOpt "." CLASS
Selector:
"." [TypeArguments] Ident [Arguments]
| "." THIS
| "." [TypeArguments] SUPER SuperSuffix
| "." NEW [TypeArguments] InnerCreator
| "[" Expression "]"
SuperSuffix:
Arguments | "." [TypeArguments] Ident [Arguments]
ArgumentsOpt:
[ Arguments ]
Arguments:
"(" [Expression { COMMA Expression }] ")"
BracketsOpt:
{"[" "]"}
BracketsSuffixExpr:
"." CLASS
BracketsSuffixType:
Creator:
Qualident [TypeArguments] ( ArrayCreatorRest | ClassCreatorRest )
InnerCreator:
Ident [TypeArguments] ClassCreatorRest
ClassCreatorRest:
Arguments [ClassBody]
ArrayCreatorRest:
"[" ( "]" BracketsOpt ArrayInitializer
| Expression "]" {"[" Expression "]"} BracketsOpt )
ArrayInitializer:
"{" [VariableInitializer {"," VariableInitializer}] [","] "}"
VariableInitializer:
ArrayInitializer | Expression
ParExpression:
"(" Expression ")"
Block:
"{" BlockStatements "}"
BlockStatements:
{ BlockStatement }
BlockStatement:
LocalVariableDeclarationStatement
| ClassOrInterfaceOrEnumDeclaration
| [Ident ":"] Statement
LocalVariableDeclarationStatement:
{ FINAL | '@' Annotation } Type VariableDeclarators ";"
Statement:
Block
| IF ParExpression Statement [ELSE Statement]
| FOR "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement
| FOR "(" FormalParameter : Expression ")" Statement
| WHILE ParExpression Statement
| DO Statement WHILE ParExpression ";"
| TRY Block ( Catches | [Catches] FinallyPart )
| TRY "(" ResourceSpecification ";"opt ")" Block [Catches] [FinallyPart]
| SWITCH ParExpression "{" SwitchBlockStatementGroups "}"
| SYNCHRONIZED ParExpression Block
| RETURN [Expression] ";"
| THROW Expression ";"
| BREAK [Ident] ";"
| CONTINUE [Ident] ";"
| ASSERT Expression [ ":" Expression ] ";"
| ";"
| ExpressionStatement
| Ident ":" Statement
CatchClause:
CATCH "(" FormalParameter ")" Block
SwitchBlockStatementGroups:
{ SwitchBlockStatementGroup }
SwitchBlockStatementGroup:
SwitchLabel BlockStatements
SwitchLabel:
CASE ConstantExpression ":" | DEFAULT ":"
MoreStatementExpressions:
{ COMMA StatementExpression }
ForInit:
StatementExpression MoreStatementExpressions
| { FINAL | '@' Annotation } Type VariableDeclarators
ForUpdate:
StatementExpression MoreStatementExpressions
AnnotationsOpt:
{ '@' Annotation }
ModifiersOpt:
{ Modifier }
Modifier:
PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL
| NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE | "@"
| "@" Annotation
Annotation:
"@" Qualident [ "(" AnnotationFieldValues ")" ]
AnnotationFieldValues:
"(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */
AnnotationFieldValue:
AnnotationValue
| Identifier "=" AnnotationValue
AnnotationValue:
ConditionalExpression
| Annotation
| "{" [ AnnotationValue { "," AnnotationValue } ] [","] "}"
VariableDeclarators:
VariableDeclarator { "," VariableDeclarator }
VariableDeclaratorsRest:
VariableDeclaratorRest { "," VariableDeclarator }
ConstantDeclaratorsRest:
ConstantDeclaratorRest { "," ConstantDeclarator }
VariableDeclarator:
Ident VariableDeclaratorRest
ConstantDeclarator:
Ident ConstantDeclaratorRest
VariableDeclaratorRest:
BracketsOpt ["=" VariableInitializer]
ConstantDeclaratorRest:
BracketsOpt "=" VariableInitializer
VariableDeclaratorId:
Ident BracketsOpt
Resources:
Resource { ";" Resources }
Resource:
VariableModifiersOpt Type VariableDeclaratorId = Expression
CompilationUnit:
[ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
ImportDeclaration:
IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";"
TypeDeclaration:
ClassOrInterfaceOrEnumDeclaration
| ";"
ClassOrInterfaceOrEnumDeclaration:
ModifiersOpt (ClassDeclaration | InterfaceDeclaration | EnumDeclaration)
ClassDeclaration:
CLASS Ident TypeParametersOpt [EXTENDS Type] [IMPLEMENTS TypeList] ClassBody
InterfaceDeclaration:
INTERFACE Ident TypeParametersOpt [EXTENDS TypeList] InterfaceBody
EnumDeclaration:
ENUM Ident [IMPLEMENTS TypeList] EnumBody
EnumBody:
"{" { EnumeratorDeclarationList } [","] [ ";" {ClassBodyDeclaration} ] "}"
EnumeratorDeclaration:
AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ]
TypeList:
Type {"," Type}
ClassBody:
"{" {ClassBodyDeclaration} "}"
ClassBodyDeclaration:
";"
| [STATIC] Block
| ModifiersOpt
( Type Ident ( VariableDeclaratorsRest ";" | MethodDeclaratorRest )
| VOID Ident MethodDeclaratorRest
| TypeParameters (Type | VOID) Ident MethodDeclaratorRest
| Ident ConstructorDeclaratorRest
| TypeParameters Ident ConstructorDeclaratorRest
| ClassOrInterfaceOrEnumDeclaration
)
InterfaceBody:
"{" {InterfaceBodyDeclaration} "}"
InterfaceBodyDeclaration:
";"
| ModifiersOpt Type Ident ( ConstantDeclaratorsRest | InterfaceMethodDeclaratorRest ";" )
MethodDeclaratorRest:
FormalParameters BracketsOpt [Throws TypeList] ( MethodBody | [DEFAULT AnnotationValue] ";")
VoidMethodDeclaratorRest:
FormalParameters [Throws TypeList] ( MethodBody | ";")
InterfaceMethodDeclaratorRest:
FormalParameters BracketsOpt [THROWS TypeList] ";"
VoidInterfaceMethodDeclaratorRest:
FormalParameters [THROWS TypeList] ";"
ConstructorDeclaratorRest:
"(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody
QualidentList:
Qualident {"," Qualident}
TypeParametersOpt:
["<" TypeParameter {"," TypeParameter} ">"]
TypeParameter:
TypeVariable [TypeParameterBound]
TypeParameterBound:
EXTENDS Type {"&" Type}
TypeVariable:
Ident
FormalParameters:
"(" [ FormalParameterList ] ")"
FormalParameterList:
[ FormalParameterListNovarargs , ] LastFormalParameter
FormalParameterListNovarargs:
[ FormalParameterListNovarargs , ] FormalParameter
FormalParameter:
{ FINAL | '@' Annotation } Type VariableDeclaratorId
LastFormalParameter:
{ FINAL | '@' Annotation } Type '...' Ident | FormalParameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment