Java 2026-07
2026-09 2026-07
Grammar changes
Tokens
Source: java/java/JavaParser.g4 @ 7d16f6dd457301340d0ae11e7d9d03269f682afd (7d16f6d) / antlr4 antlr4-parser-v1 / 2026-07-12
These diagrams describe a parser grammar, rather than the complete language specification. Lexer state and semantic checks may impose additional restrictions.
This is an upstream ANTLR grammar snapshot. Its date identifies the grammar revision, rather than a language release.
BSD-3-Clause
BNF compilationUnit ::= compilationUnit__1 compilationUnit__2 compilationUnit__3 EOF
| modularCompulationUnit EOF
Used by (0)
References (4)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
EOF
compilationUnit › modularCompulationUnit
BNF modularCompulationUnit ::= modularCompulationUnit__1 moduleDeclaration
Used by (1)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
module IDENTIFIER { }
compilationUnit › compilationUnit__1 › packageDeclaration
BNF packageDeclaration ::= packageDeclaration__1 PACKAGE qualifiedName ';'
Used by (1)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
package IDENTIFIER ;
compilationUnit › compilationUnit__2 › importDeclaration
BNF importDeclaration ::= IMPORT importDeclaration__1 qualifiedName importDeclaration__2 ';'
Used by (2)
References (3)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
import IDENTIFIER ;
compilationUnit › compilationUnit__3 › typeDeclaration
BNF typeDeclaration ::= typeDeclaration__1 typeDeclaration__2
Used by (1)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
enum IDENTIFIER { }
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › classDeclaration
BNF classDeclaration ::= CLASS identifier classDeclaration__1 classDeclaration__2 classDeclaration__3 classDeclaration__4 classBody
Used by (5)
References (6)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
class IDENTIFIER { }
Code example class Hello {
public static void main(String[] args) {
System.out.println("hello");
}
}
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › enumDeclaration › enumDeclaration__4 › enumBodyDeclarations
BNF enumBodyDeclarations ::= ';' enumBodyDeclarations__1
Used by (1)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
;
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › classDeclaration › classBody
BNF classBody ::= '{' classBody__1 '}'
Used by (3)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
{ }
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › interfaceDeclaration › interfaceBody
BNF interfaceBody ::= '{' interfaceBody__1 '}'
Used by (1)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
{ }
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › classDeclaration › classBody › classBody__1 › classBodyDeclaration
BNF classBodyDeclaration ::= ';'
| classBodyDeclaration__1 block
| classBodyDeclaration__2 memberDeclaration
Used by (3)
References (4)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
;
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › classDeclaration › classBody › classBody__1 › classBodyDeclaration › memberDeclaration › methodDeclaration › methodBody
BNF methodBody ::= block
| ';'
Used by (2)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
;
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › interfaceDeclaration › interfaceBody › interfaceBody__1 › interfaceBodyDeclaration
BNF interfaceBodyDeclaration ::= interfaceBodyDeclaration__1 interfaceMemberDeclaration
| ';'
Used by (1)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
;
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › interfaceDeclaration › interfaceBody › interfaceBody__1 › interfaceBodyDeclaration › interfaceMemberDeclaration › interfaceMethodDeclaration › interfaceCommonBodyDeclaration
BNF interfaceCommonBodyDeclaration ::= interfaceCommonBodyDeclaration__1 typeTypeOrVoid identifier formalParameters interfaceCommonBodyDeclaration__2 interfaceCommonBodyDeclaration__3 methodBody
Used by (2)
References (7)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
void IDENTIFIER ( ) ;
Unreachable
BNF altAnnotationQualifiedName ::= altAnnotationQualifiedName__1 '@' identifier
Used by (0)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
@ IDENTIFIER
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › annotationTypeDeclaration › annotationTypeBody
BNF annotationTypeBody ::= '{' annotationTypeBody__1 '}'
Used by (1)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
{ }
compilationUnit › modularCompulationUnit › moduleDeclaration
BNF moduleDeclaration ::= moduleDeclaration__1 moduleDeclaration__2 MODULE qualifiedName '{' moduleDeclaration__3 '}'
Used by (1)
References (4)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
module IDENTIFIER { }
compilationUnit › modularCompulationUnit › moduleDeclaration › moduleDeclaration__3 › moduleDirective
BNF moduleDirective ::= REQUIRES moduleDirective__1 qualifiedName ';'
| EXPORTS qualifiedName moduleDirective__2 ';'
| OPENS qualifiedName moduleDirective__4 ';'
| USES qualifiedName ';'
| PROVIDES qualifiedName WITH qualifiedName moduleDirective__6 ';'
Used by (1)
References (5)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
requires IDENTIFIER ;
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › recordDeclaration › recordBody
BNF recordBody ::= '{' recordBody__1 '}'
Used by (1)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
{ }
compilationUnit › compilationUnit__1 › packageDeclaration › qualifiedName › identifier
BNF identifier ::= IDENTIFIER
| MODULE
| OPEN
| REQUIRES
| EXPORTS
| OPENS
| TO
| USES
| PROVIDES
| WHEN
| WITH
| TRANSITIVE
| YIELD
| SEALED
| PERMITS
| RECORD
| VAR
Used by (43)
References (0)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
IDENTIFIER
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › classDeclaration › classBody › classBody__1 › classBodyDeclaration › block › block__1 › blockStatement › statement
BNF statement ::= block
| ASSERT expression statement__1 ';'
| IF '(' expression ')' statement statement__2
| FOR '(' forControl ')' statement
| WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';'
| TRY block statement__3
| TRY resourceSpecification block statement__6 statement__7
| SWITCH '(' expression ')' '{' statement__8 statement__9 '}'
| SYNCHRONIZED '(' expression ')' block
| RETURN statement__10 ';'
| THROW expression ';'
| BREAK statement__11 ';'
| CONTINUE statement__12 ';'
| YIELD expression ';'
| SEMI
| expression ';'
| switchExpression statement__13
| identifier ':' statement
Used by (3)
References (18)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
;
compilationUnit › compilationUnit__1 › packageDeclaration › packageDeclaration__1 › annotation › annotation__1 › annotationFieldValues › annotationFieldValues__1 › annotationFieldValue › annotationValue › expression
BNF expression ::= primary
| expression '[' expression ']'
| expression '.' expression__1
| methodCall
| expression '::' expression__3 identifier
| typeType '::' expression__4
| classType '::' expression__6 NEW
| switchExpression
| expression expression__7
| expression__8 expression
| '(' expression__9 typeType expression__10 ')' expression
| NEW creator
| expression expression__11 expression
| expression expression__12 expression
| expression expression__13 expression
| expression expression__14 expression
| expression INSTANCEOF expression__15
| expression expression__16 expression
| expression '&' expression
| expression '^' expression
| expression '|' expression
| expression '&&' expression
| expression '||' expression
| expression '?' expression ':' expression
| expression expression__17 expression
| lambdaExpression
Used by (19)
References (24)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
this
compilationUnit › compilationUnit__1 › packageDeclaration › packageDeclaration__1 › annotation › annotation__1 › annotationFieldValues › annotationFieldValues__1 › annotationFieldValue › annotationValue › expression › lambdaExpression › lambdaBody
BNF lambdaBody ::= expression
| block
Used by (1)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
this
Inline rule
compilationUnit › compilationUnit__1
BNF compilationUnit__1 ::= ε
| packageDeclaration
Used by (1)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__2
BNF compilationUnit__2 ::= ε
| importDeclaration compilationUnit__2
| ';' compilationUnit__2
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3
BNF compilationUnit__3 ::= ε
| typeDeclaration compilationUnit__3
| ';' compilationUnit__3
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › modularCompulationUnit › modularCompulationUnit__1
BNF modularCompulationUnit__1 ::= ε
| importDeclaration modularCompulationUnit__1
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2
BNF typeDeclaration__2 ::= classDeclaration
| enumDeclaration
| interfaceDeclaration
| annotationTypeDeclaration
| recordDeclaration
Used by (1)
References (5)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
enum IDENTIFIER { }
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › enumDeclaration › enumDeclaration__4 › enumBodyDeclarations › enumBodyDeclarations__1
BNF enumBodyDeclarations__1 ::= ε
| classBodyDeclaration enumBodyDeclarations__1
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › classDeclaration › classBody › classBody__1
BNF classBody__1 ::= ε
| classBodyDeclaration classBody__1
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › interfaceDeclaration › interfaceBody › interfaceBody__1
BNF interfaceBody__1 ::= ε
| interfaceBodyDeclaration interfaceBody__1
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › classDeclaration › classBody › classBody__1 › classBodyDeclaration › classBodyDeclaration__1
BNF classBodyDeclaration__1 ::= ε
| STATIC
Used by (1)
References (0)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › classDeclaration › classBody › classBody__1 › classBodyDeclaration › classBodyDeclaration__2
BNF classBodyDeclaration__2 ::= ε
| modifier classBodyDeclaration__2
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › interfaceDeclaration › interfaceBody › interfaceBody__1 › interfaceBodyDeclaration › interfaceBodyDeclaration__1
BNF interfaceBodyDeclaration__1 ::= ε
| modifier interfaceBodyDeclaration__1
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › interfaceDeclaration › interfaceBody › interfaceBody__1 › interfaceBodyDeclaration › interfaceMemberDeclaration › interfaceMethodDeclaration › interfaceCommonBodyDeclaration › interfaceCommonBodyDeclaration__1
BNF interfaceCommonBodyDeclaration__1 ::= ε
| annotation interfaceCommonBodyDeclaration__1
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › interfaceDeclaration › interfaceBody › interfaceBody__1 › interfaceBodyDeclaration › interfaceMemberDeclaration › interfaceMethodDeclaration › interfaceCommonBodyDeclaration › interfaceCommonBodyDeclaration__2
BNF interfaceCommonBodyDeclaration__2 ::= ε
| '[' ']' interfaceCommonBodyDeclaration__2
Used by (2)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › interfaceDeclaration › interfaceBody › interfaceBody__1 › interfaceBodyDeclaration › interfaceMemberDeclaration › interfaceMethodDeclaration › interfaceCommonBodyDeclaration › interfaceCommonBodyDeclaration__3
BNF interfaceCommonBodyDeclaration__3 ::= ε
| THROWS qualifiedNameList
Used by (1)
References (1)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
Unreachable
BNF altAnnotationQualifiedName__1 ::= ε
| identifier DOT altAnnotationQualifiedName__1
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › annotationTypeDeclaration › annotationTypeBody › annotationTypeBody__1
BNF annotationTypeBody__1 ::= ε
| annotationTypeElementDeclaration annotationTypeBody__1
Used by (2)
References (2)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε
Inline rule
compilationUnit › compilationUnit__3 › typeDeclaration › typeDeclaration__2 › recordDeclaration › recordBody › recordBody__1
BNF recordBody__1 ::= ε
| classBodyDeclaration recordBody__1
| compactConstructorDeclaration recordBody__1
Used by (2)
References (3)
Shortest derivation This is a token sequence derived from the grammar. It may require lexer context or semantic checks to be valid code.
ε