出典: golang/GoParser.g4 @ ed5e15c348d6125840384bc271bd76c437c556a4 (ed5e15c) / antlr4 antlr4-parser-v1 / 2026-02-08
この図はパーサ向けの文法を表しています。言語仕様そのものではなく、字句解析器の状態や意味検査による制約は図に表れません。
上流のANTLR文法を収録しています。日付は文法の更新時期であり、言語のリリース日ではありません。
BSD-3-Clause
sourceFile ::= packageClause eos sourceFile__1 sourceFile__2 EOF
文法から導出したトークン列です。実際のコードとして成立するには、字句解析や意味検査の条件を満たす必要があります。
package IDENTIFIER EOF
packageClause ::= PACKAGE packageName
package IDENTIFIER
packageName ::= identifier
IDENTIFIER
identifier ::= IDENTIFIER
importDecl ::= IMPORT importDecl__1
import RAW_STRING_LIT
importSpec ::= importSpec__1 importPath
RAW_STRING_LIT
importPath ::= string_
declaration ::= constDecl | typeDecl | varDecl
const IDENTIFIER
constDecl ::= CONST constDecl__1
constSpec ::= identifierList constSpec__1
identifierList ::= IDENTIFIER identifierList__1
expressionList ::= expression expressionList__1
typeDecl ::= TYPE typeDecl__1
type ( )
typeSpec ::= aliasDecl | typeDef
IDENTIFIER IDENTIFIER
aliasDecl ::= IDENTIFIER aliasDecl__1 ASSIGN type_
IDENTIFIER = IDENTIFIER
typeDef ::= IDENTIFIER typeDef__1 type_
typeParameters ::= L_BRACKET typeParameterDecl typeParameters__1 R_BRACKET
[ IDENTIFIER IDENTIFIER ]
typeParameterDecl ::= identifierList typeElement
typeElement ::= typeTerm typeElement__1
typeTerm ::= typeTerm__1 type_
functionDecl ::= FUNC IDENTIFIER functionDecl__1 signature functionDecl__2
func IDENTIFIER ( )
package main func main() { println("hello") }
methodDecl ::= FUNC receiver IDENTIFIER signature methodDecl__1
func ( ) IDENTIFIER ( )
receiver ::= parameters
( )
varDecl ::= VAR varDecl__1
var ( )
varSpec ::= identifierList varSpec__1
block ::= L_CURLY statementList R_CURLY
{ }
statementList ::= statementList__1
ε
statement ::= declaration | labeledStmt | simpleStmt | goStmt | returnStmt | breakStmt | continueStmt | gotoStmt | fallthroughStmt | block | ifStmt | switchStmt | selectStmt | forStmt | deferStmt
fallthrough
simpleStmt ::= sendStmt | incDecStmt | assignment | expressionStmt | shortVarDecl
expressionStmt ::= expression
sendStmt ::= expression RECEIVE expression
IDENTIFIER <- IDENTIFIER
incDecStmt ::= expression incDecStmt__1
IDENTIFIER ++
assignment ::= expressionList assign_op expressionList
assign_op ::= assign_op__1 ASSIGN
=
shortVarDecl ::= identifierList DECLARE_ASSIGN expressionList
IDENTIFIER := IDENTIFIER
labeledStmt ::= IDENTIFIER COLON labeledStmt__1
IDENTIFIER :
returnStmt ::= RETURN returnStmt__1
return
breakStmt ::= BREAK breakStmt__1
break
continueStmt ::= CONTINUE continueStmt__1
continue
gotoStmt ::= GOTO IDENTIFIER
goto IDENTIFIER
fallthroughStmt ::= FALLTHROUGH
deferStmt ::= DEFER expression
defer IDENTIFIER
ifStmt ::= IF ifStmt__1 block ifStmt__4
if IDENTIFIER { }
switchStmt ::= exprSwitchStmt | typeSwitchStmt
switch { }
exprSwitchStmt ::= SWITCH exprSwitchStmt__1 L_CURLY exprSwitchStmt__5 R_CURLY
exprCaseClause ::= exprSwitchCase COLON statementList
default :
exprSwitchCase ::= CASE expressionList | DEFAULT
default
typeSwitchStmt ::= SWITCH typeSwitchStmt__1 L_CURLY typeSwitchStmt__2 R_CURLY
switch IDENTIFIER . ( type ) { }
typeSwitchGuard ::= typeSwitchGuard__1 primaryExpr DOT L_PAREN TYPE R_PAREN
IDENTIFIER . ( type )
typeCaseClause ::= typeSwitchCase COLON statementList
typeSwitchCase ::= CASE typeList | DEFAULT
typeList ::= typeList__1 typeList__2
nil
selectStmt ::= SELECT L_CURLY selectStmt__1 R_CURLY
select { }
commClause ::= commCase COLON statementList
commCase ::= CASE commCase__1 | DEFAULT
recvStmt ::= recvStmt__1 expression
forStmt ::= FOR forStmt__1 block
for { }
condition ::= expression
forClause ::= forClause__1 eos forClause__2 eos forClause__3
rangeClause ::= rangeClause__1 RANGE expression
range IDENTIFIER
goStmt ::= GO expression
go IDENTIFIER
type_ ::= typeName type___1 | typeLit | L_PAREN type_ R_PAREN
typeArgs ::= L_BRACKET typeList typeArgs__1 R_BRACKET
[ nil ]
typeName ::= qualifiedIdent | IDENTIFIER
typeLit ::= arrayType | structType | pointerType | functionType | interfaceType | sliceType | mapType | channelType
* IDENTIFIER
arrayType ::= L_BRACKET arrayLength R_BRACKET elementType
[ IDENTIFIER ] IDENTIFIER
arrayLength ::= expression
elementType ::= type_
pointerType ::= STAR type_
interfaceType ::= INTERFACE L_CURLY interfaceType__1 R_CURLY
interface { }
sliceType ::= L_BRACKET R_BRACKET elementType
[ ] IDENTIFIER
mapType ::= MAP L_BRACKET type_ R_BRACKET elementType
map [ IDENTIFIER ] IDENTIFIER
channelType ::= channelType__1 elementType
chan IDENTIFIER
methodSpec ::= IDENTIFIER parameters result | IDENTIFIER parameters
IDENTIFIER ( )
functionType ::= FUNC signature
func ( )
signature ::= parameters signature__1
result ::= parameters | type_
parameters ::= L_PAREN parameters__1 R_PAREN
parameterDecl ::= parameterDecl__1 parameterDecl__2 type_
expression ::= primaryExpr | expression__1 expression | expression expression__2 expression | expression expression__3 expression | expression expression__4 expression | expression LOGICAL_AND expression | expression LOGICAL_OR expression
primaryExpr ::= primaryExpr__1 primaryExpr__2
conversion ::= type_ L_PAREN expression conversion__1 R_PAREN
IDENTIFIER ( IDENTIFIER )
operand ::= literal | operandName operand__1 | L_PAREN expression R_PAREN
literal ::= basicLit | compositeLit | functionLit
basicLit ::= NIL_LIT | integer | string_ | FLOAT_LIT
integer ::= DECIMAL_LIT | BINARY_LIT | OCTAL_LIT | HEX_LIT | IMAGINARY_LIT | RUNE_LIT
DECIMAL_LIT
operandName ::= IDENTIFIER | qualifiedIdent
qualifiedIdent ::= IDENTIFIER DOT IDENTIFIER
IDENTIFIER . IDENTIFIER
compositeLit ::= literalType literalValue
IDENTIFIER { }
literalType ::= structType | arrayType | L_BRACKET ELLIPSIS R_BRACKET elementType | sliceType | mapType | typeName literalType__1
literalValue ::= L_CURLY literalValue__1 R_CURLY
elementList ::= keyedElement elementList__1
keyedElement ::= keyedElement__1 element
key ::= expression | literalValue
element ::= expression | literalValue
structType ::= STRUCT L_CURLY structType__1 R_CURLY
struct { }
fieldDecl ::= fieldDecl__1 fieldDecl__2
string_ ::= RAW_STRING_LIT | INTERPRETED_STRING_LIT
embeddedField ::= embeddedField__1 typeName embeddedField__2
functionLit ::= FUNC signature block
func ( ) { }
index ::= L_BRACKET expression R_BRACKET
[ IDENTIFIER ]
slice_ ::= L_BRACKET slice___1 R_BRACKET
[ : ]
typeAssertion ::= DOT L_PAREN type_ R_PAREN
. ( IDENTIFIER )
arguments ::= L_PAREN arguments__1 R_PAREN
methodExpr ::= type_ DOT IDENTIFIER
eos ::= SEMI | EOS | ε
sourceFile__1 ::= ε | importDecl eos sourceFile__1
sourceFile__2 ::= ε | sourceFile__3 eos sourceFile__2
sourceFile__3 ::= functionDecl | methodDecl | declaration
importDecl__1 ::= importSpec | L_PAREN importDecl__2 R_PAREN
importDecl__2 ::= ε | importSpec eos importDecl__2
importSpec__1 ::= ε | DOT | packageName
constDecl__1 ::= constSpec | L_PAREN constDecl__2 R_PAREN
constDecl__2 ::= ε | constSpec eos constDecl__2
constSpec__1 ::= ε | constSpec__2 ASSIGN expressionList
constSpec__2 ::= ε | type_
identifierList__1 ::= ε | COMMA IDENTIFIER identifierList__1
expressionList__1 ::= ε | COMMA expression expressionList__1
typeDecl__1 ::= typeSpec | L_PAREN typeDecl__2 R_PAREN
typeDecl__2 ::= ε | typeSpec eos typeDecl__2
aliasDecl__1 ::= ε | typeParameters
typeDef__1 ::= ε | typeParameters
typeParameters__1 ::= ε | COMMA typeParameterDecl typeParameters__1
typeElement__1 ::= ε | OR typeTerm typeElement__1
typeTerm__1 ::= ε | UNDERLYING
functionDecl__1 ::= ε | typeParameters
functionDecl__2 ::= ε | block
methodDecl__1 ::= ε | block
varDecl__1 ::= varSpec | L_PAREN varDecl__2 R_PAREN
varDecl__2 ::= ε | varSpec eos varDecl__2
varSpec__1 ::= type_ varSpec__2 | ASSIGN expressionList
varSpec__2 ::= ε | ASSIGN expressionList
statementList__1 ::= ε | statementList__2 statement eos statementList__1
statementList__2 ::= SEMI | EOS | ε
incDecStmt__1 ::= PLUS_PLUS | MINUS_MINUS
++
assign_op__1 ::= ε | PLUS | MINUS | OR | CARET | STAR | DIV | MOD | LSHIFT | RSHIFT | AMPERSAND | BIT_CLEAR
labeledStmt__1 ::= ε | statement
returnStmt__1 ::= ε | expressionList
breakStmt__1 ::= ε | IDENTIFIER
continueStmt__1 ::= ε | IDENTIFIER
ifStmt__1 ::= expression | ifStmt__2 expression | simpleStmt ifStmt__3 expression
ifStmt__2 ::= SEMI | EOS
;
ifStmt__3 ::= SEMI | EOS
ifStmt__4 ::= ε | ELSE ifStmt__5
ifStmt__5 ::= ifStmt | block
exprSwitchStmt__1 ::= exprSwitchStmt__2 | exprSwitchStmt__3 eos exprSwitchStmt__4
exprSwitchStmt__2 ::= ε | expression
exprSwitchStmt__3 ::= ε | simpleStmt
exprSwitchStmt__4 ::= ε | expression
exprSwitchStmt__5 ::= ε | exprCaseClause exprSwitchStmt__5
typeSwitchStmt__1 ::= typeSwitchGuard | eos typeSwitchGuard | simpleStmt eos typeSwitchGuard
typeSwitchStmt__2 ::= ε | typeCaseClause typeSwitchStmt__2
typeSwitchGuard__1 ::= ε | IDENTIFIER DECLARE_ASSIGN
typeList__1 ::= type_ | NIL_LIT
typeList__2 ::= ε | COMMA typeList__3 typeList__2
typeList__3 ::= type_ | NIL_LIT
selectStmt__1 ::= ε | commClause selectStmt__1
commCase__1 ::= sendStmt | recvStmt
recvStmt__1 ::= ε | expressionList ASSIGN | identifierList DECLARE_ASSIGN
forStmt__1 ::= ε | condition | forClause | rangeClause
forClause__1 ::= ε | simpleStmt
forClause__2 ::= ε | expression
forClause__3 ::= ε | simpleStmt
rangeClause__1 ::= ε | expressionList ASSIGN | identifierList DECLARE_ASSIGN
type___1 ::= ε | typeArgs
typeArgs__1 ::= ε | COMMA
interfaceType__1 ::= ε | interfaceType__2 eos interfaceType__1
interfaceType__2 ::= methodSpec | typeElement
channelType__1 ::= CHAN | CHAN RECEIVE | RECEIVE CHAN
chan
signature__1 ::= ε | result
parameters__1 ::= ε | parameterDecl parameters__2 parameters__3
parameters__2 ::= ε | COMMA parameterDecl parameters__2
parameters__3 ::= ε | COMMA
parameterDecl__1 ::= ε | identifierList
parameterDecl__2 ::= ε | ELLIPSIS
expression__1 ::= PLUS | MINUS | EXCLAMATION | CARET | STAR | AMPERSAND | RECEIVE
+
expression__2 ::= STAR | DIV | MOD | LSHIFT | RSHIFT | AMPERSAND | BIT_CLEAR
*
expression__3 ::= PLUS | MINUS | OR | CARET
expression__4 ::= EQUALS | NOT_EQUALS | LESS | LESS_OR_EQUALS | GREATER | GREATER_OR_EQUALS
==
primaryExpr__1 ::= operand | conversion | methodExpr
primaryExpr__2 ::= ε | DOT IDENTIFIER primaryExpr__2 | index primaryExpr__2 | slice_ primaryExpr__2 | typeAssertion primaryExpr__2 | arguments primaryExpr__2
conversion__1 ::= ε | COMMA
operand__1 ::= ε | typeArgs
literalType__1 ::= ε | typeArgs
literalValue__1 ::= ε | elementList literalValue__2
literalValue__2 ::= ε | COMMA
elementList__1 ::= ε | COMMA keyedElement elementList__1
keyedElement__1 ::= ε | key COLON
structType__1 ::= ε | fieldDecl eos structType__1
fieldDecl__1 ::= identifierList type_ | embeddedField
fieldDecl__2 ::= ε | string_
embeddedField__1 ::= ε | STAR
embeddedField__2 ::= ε | typeArgs
slice___1 ::= slice___2 COLON slice___3 | slice___4 COLON expression COLON expression
:
slice___2 ::= ε | expression
slice___3 ::= ε | expression
slice___4 ::= ε | expression
arguments__1 ::= ε | arguments__2 arguments__4 arguments__5
arguments__2 ::= type_ arguments__3 | expressionList
arguments__3 ::= ε | COMMA expressionList
arguments__4 ::= ε | ELLIPSIS
arguments__5 ::= ε | COMMA
/で検索、jとkで前後の規則へ移動、?でこのヘルプを表示します。Escapeで閉じます。