Go/EBNF
< 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 categorized as "Letter" *) ; unicode_digit = (* a Unicode code point categorized 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" | `\` | "'" | `"` ) ; string_lit = raw_string_lit | interpreted_string_lit ; raw_string_lit = "`" { unicode_char | newline } "`" ; interpreted_string_lit = `"` { unicode_value | byte_value } `"` ; Type = TypeName [ TypeArgs ] | TypeLit | "(" Type ")" ; TypeName = identifier | QualifiedIdent ; TypeArgs = "[" TypeList [ "," ] "]" ; TypeList = Type { "," Type } ; TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType | SliceType | MapType | ChannelType ; ArrayType = "[" ArrayLength "]" ElementType ; ArrayLength = Expression ; ElementType = Type ; SliceType = "[" "]" ElementType ; StructType = "struct" "{" { FieldDecl ";" } "}" ; FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] ; EmbeddedField = [ "*" ] TypeName [ TypeArgs ] ; 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" "{" { InterfaceElem ";" } "}" ; InterfaceElem = MethodElem | TypeElem ; MethodElem = MethodName Signature ; MethodName = identifier ; TypeElem = TypeTerm { "|" TypeTerm } ; TypeTerm = Type | UnderlyingType ; UnderlyingType = "~" Type ; 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 ";" } ")" ) ; ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] ; IdentifierList = identifier { "," identifier } ; ExpressionList = Expression { "," Expression } ; TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) ; TypeSpec = AliasDecl | TypeDef ; AliasDecl = identifier "=" Type ; TypeDef = identifier [ TypeParameters ] Type ; TypeParameters = "[" TypeParamList [ "," ] "]" ; TypeParamList = TypeParamDecl { "," TypeParamDecl } ; TypeParamDecl = IdentifierList TypeConstraint ; TypeConstraint = TypeElem ; VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) ; VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) ; ShortVarDecl = IdentifierList ":=" ExpressionList ; FunctionDecl = "func" FunctionName [ TypeParameters ] Signature [ FunctionBody ] ; FunctionName = identifier ; FunctionBody = Block ; MethodDecl = "func" Receiver MethodName Signature [ FunctionBody ] ; Receiver = Parameters ; Operand = Literal | OperandName [ TypeArgs ] | "(" Expression ")" ; Literal = BasicLit | CompositeLit | FunctionLit ; BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit ; OperandName = identifier | QualifiedIdent ; QualifiedIdent = PackageName "." identifier ; CompositeLit = LiteralType LiteralValue ; LiteralType = StructType | ArrayType | "[" "..." "]" ElementType | SliceType | MapType | TypeName [ TypeArgs ] ; LiteralValue = "{" [ ElementList [ "," ] ] "}" ; ElementList = KeyedElement { "," KeyedElement } ; KeyedElement = [ Key ":" ] Element ; Key = FieldName | Expression | LiteralValue ; FieldName = identifier ; Element = Expression | LiteralValue ; FunctionLit = "func" Signature FunctionBody ; PrimaryExpr = Operand | Conversion | MethodExpr | PrimaryExpr Selector | PrimaryExpr Index | PrimaryExpr Slice | PrimaryExpr TypeAssertion | PrimaryExpr Arguments ; Selector = "." identifier ; Index = "[" Expression [ "," ] "]" ; Slice = "[" [ Expression ] ":" [ Expression ] "]" | "[" [ Expression ] ":" Expression ":" Expression "]" ; TypeAssertion = "." "(" Type ")" ; Arguments = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" ; MethodExpr = ReceiverType "." MethodName ; ReceiverType = Type ; Expression = UnaryExpr | Expression binary_op Expression ; UnaryExpr = PrimaryExpr | unary_op UnaryExpr ; binary_op = "||" | "&&" | rel_op | add_op | mul_op ; rel_op = "==" | "!=" | "<" | "<=" | ">" | ">=" ; add_op = "+" | "-" | "|" | "^" ; mul_op = "*" | "/" | "%" | "<<" | ">>" | "&" | "&^" ; unary_op = "+" | "-" | "!" | "^" | "*" | "&" | "<-" ; Conversion = Type "(" Expression [ "," ] ")" ; Statement = Declaration | LabeledStmt | SimpleStmt | GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt | FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt | DeferStmt ; SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl ; EmptyStmt = ; LabeledStmt = Label ":" Statement ; Label = identifier ; ExpressionStmt = Expression ; SendStmt = Channel "<-" Expression ; Channel = Expression ; IncDecStmt = Expression ( "++" | "--" ) ; Assignment = ExpressionList assign_op ExpressionList ; assign_op = [ add_op | mul_op ] "=" ; IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] ; SwitchStmt = ExprSwitchStmt | TypeSwitchStmt ; ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" ; ExprCaseClause = ExprSwitchCase ":" StatementList ; ExprSwitchCase = "case" ExpressionList | "default" ; TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" ; TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" ; TypeCaseClause = TypeSwitchCase ":" StatementList ; TypeSwitchCase = "case" TypeList | "default" ; ForStmt = "for" [ Condition | ForClause | RangeClause ] Block ; Condition = Expression ; ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] ; InitStmt = SimpleStmt ; PostStmt = SimpleStmt ; RangeClause = [ ExpressionList "=" | IdentifierList ":=" ] "range" Expression ; GoStmt = "go" Expression ; SelectStmt = "select" "{" { CommClause } "}" ; CommClause = CommCase ":" StatementList ; CommCase = "case" ( SendStmt | RecvStmt ) | "default" ; RecvStmt = [ ExpressionList "=" | IdentifierList ":=" ] RecvExpr ; RecvExpr = Expression ; ReturnStmt = "return" [ ExpressionList ] ; BreakStmt = "break" [ Label ] ; ContinueStmt = "continue" [ Label ] ; GotoStmt = "goto" Label ; FallthroughStmt = "fallthrough" ; DeferStmt = "defer" Expression ; SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } ; PackageClause = "package" PackageName ; PackageName = identifier ; ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) ; ImportSpec = [ "." | PackageName ] ImportPath ; ImportPath = string_lit ;
脚註
編集- ^ The Go Programming Language Specification. The Go website. (June 13, 2024) .