Go/EBNF

出典: フリー教科書『ウィキブックス(Wikibooks)』
< Go
ナビゲーションに移動 検索に移動

The Go Programming Language Specification からEBNFで書かれた構文を抜粋しました[1]

構文
newline        = (* the Unicode code point U+000A *) ;
unicode_char   = (* an arbitrary Unicode code point except newline *) ;
unicode_letter = (* a Unicode code point classified as "Letter" *) ;
unicode_digit  = (* a Unicode code point classified as "Number, decimal digit" *) ;
letter        = unicode_letter | "_" ;
decimal_digit = "0"  "9" ;
binary_digit  = "0" | "1" ;
octal_digit   = "0"  "7" ;
hex_digit     = "0"  "9" | "A"  "F" | "a"  "f" ;
identifier = letter { letter | unicode_digit } ;
int_lit        = decimal_lit | binary_lit | octal_lit | hex_lit ;
decimal_lit    = "0" | ( "1"  "9" ) [ [ "_" ] decimal_digits ] ;
binary_lit     = "0" ( "b" | "B" ) [ "_" ] binary_digits ;
octal_lit      = "0" [ "o" | "O" ] [ "_" ] octal_digits ;
hex_lit        = "0" ( "x" | "X" ) [ "_" ] hex_digits ;

decimal_digits = decimal_digit { [ "_" ] decimal_digit } ;
binary_digits  = binary_digit { [ "_" ] binary_digit } ;
octal_digits   = octal_digit { [ "_" ] octal_digit } ;
hex_digits     = hex_digit { [ "_" ] hex_digit } ;
float_lit         = decimal_float_lit | hex_float_lit ;

decimal_float_lit = decimal_digits "." [ decimal_digits ] [ decimal_exponent ] |
                    decimal_digits decimal_exponent |
                    "." decimal_digits [ decimal_exponent ] ;
decimal_exponent  = ( "e" | "E" ) [ "+" | "-" ] decimal_digits ;

hex_float_lit     = "0" ( "x" | "X" ) hex_mantissa hex_exponent ;
hex_mantissa      = [ "_" ] hex_digits "." [ hex_digits ] |
                    [ "_" ] hex_digits |
                    "." hex_digits ;
hex_exponent      = ( "p" | "P" ) [ "+" | "-" ] decimal_digits ;
imaginary_lit = (decimal_digits | int_lit | float_lit) "i" ;
rune_lit         = "'" ( unicode_value | byte_value ) "'" ;
unicode_value    = unicode_char | little_u_value | big_u_value | escaped_char ;
byte_value       = octal_byte_value | hex_byte_value ;
octal_byte_value = `\` octal_digit octal_digit octal_digit ;
hex_byte_value   = `\` "x" hex_digit hex_digit ;
little_u_value   = `\` "u" hex_digit hex_digit hex_digit hex_digit ;
big_u_value      = `\` "U" hex_digit hex_digit hex_digit hex_digit
                           hex_digit hex_digit hex_digit hex_digit ;
escaped_char     = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) ;
Type      = TypeName | TypeLit | "(" Type ")" ;
TypeName  = identifier | QualifiedIdent ;
TypeLit   = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
	    SliceType | MapType | ChannelType ;
ArrayType   = "[" ArrayLength "]" ElementType ;
SliceType = "[" "]" ElementType ;
StructType    = "struct" "{" { FieldDecl ";" } "}" ;
FieldDecl     = (IdentifierList Type | EmbeddedField) [ Tag ] ;
EmbeddedField = [ "*" ] TypeName ;
Tag           = string_lit ;
PointerType = "*" BaseType ;
BaseType    = Type ;
FunctionType   = "func" Signature ;
Signature      = Parameters [ Result ] ;
Result         = Parameters | Type ;
Parameters     = "(" [ ParameterList [ "," ] ] ")" ;
ParameterList  = ParameterDecl { "," ParameterDecl } ;
ParameterDecl  = [ IdentifierList ] [ "..." ] Type ;
InterfaceType      = "interface" "{" { ( MethodSpec | InterfaceTypeName ) ";" } "}" ;
MethodSpec         = MethodName Signature ;
MethodName         = identifier ;
InterfaceTypeName  = TypeName ;
MapType     = "map" "[" KeyType "]" ElementType ;
KeyType     = Type ;
ChannelType = ( "chan" | "chan" "<-" | "<-" "chan" ) ElementType ;
Block = "{" StatementList "}" ;
StatementList = { Statement ";" } ;
Declaration   = ConstDecl | TypeDecl | VarDecl ;
TopLevelDecl  = Declaration | FunctionDecl | MethodDecl ;
ConstDecl      = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) ;
TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) ;
TypeSpec = AliasDecl | TypeDef ;
AliasDecl = identifier "=" Type ;
TypeDef = identifier Type ;
VarDecl     = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) ;
FunctionDecl = "func" FunctionName Signature [ FunctionBody ] ;
FunctionName = identifier ;
FunctionBody = Block ;
MethodDecl = "func" Receiver MethodName Signature [ FunctionBody ] ;
Receiver   = Parameters ;
QualifiedIdent = PackageName "." identifier ;
CompositeLit  = LiteralType LiteralValue ;
LiteralType   = StructType | ArrayType | "[" "..." "]" ElementType |
                SliceType | MapType | TypeName ;
LiteralValue  = "{" [ ElementList [ "," ] ] "}" ;
ElementList   = KeyedElement { "," KeyedElement } ;
KeyedElement  = [ Key ":" ] Element ;
FunctionLit = "func" Signature FunctionBody ;
PrimaryExpr =
	Operand |
	Conversion |
	MethodExpr |
	PrimaryExpr Selector |
	PrimaryExpr Index |
	PrimaryExpr Slice |
	PrimaryExpr TypeAssertion |
	PrimaryExpr Arguments ;

Selector       = "." identifier ;
MethodExpr    = ReceiverType "." MethodName ;
ReceiverType  = Type ;
Statement =
	Declaration | LabeledStmt | SimpleStmt |
	GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
	FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
	DeferStmt ;

EmptyStmt = ;
LabeledStmt = Label ":" Statement ;
Label       = identifier ;
SwitchStmt = ExprSwitchStmt | TypeSwitchStmt ;
TypeSwitchStmt  = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" ;
TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" ;
TypeCaseClause  = TypeSwitchCase ":" StatementList ;
TypeSwitchCase  = "case" TypeList | "default" ;
TypeList        = Type { "," Type } ;
ForStmt = "for" [ Condition | ForClause | RangeClause ] Block ;
ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] ;
InitStmt = SimpleStmt ;
PostStmt = SimpleStmt ;
SelectStmt = "select" "{" { CommClause } "}" ;
CommClause = CommCase ":" StatementList ;
CommCase   = "case" ( SendStmt | RecvStmt ) | "default" ;
BreakStmt = "break" [ Label ] ;
ContinueStmt = "continue" [ Label ] ;
GotoStmt = "goto" Label ;
FallthroughStmt = "fallthrough" ;
SourceFile       = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } ;
PackageClause  = "package" PackageName ;
PackageName    = identifier ;
ImportDecl       = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) ;
ImportSpec       = [ "." | PackageName ] ImportPath ;
ImportPath       = string_lit ;

脚註[編集]

  1. ^ The Go Programming Language Specification. The Go website. (Jul 26, 2021). https://golang.org/ref/spec.