BRAIDGROUP
RESEARCH & DEV
49. Documentation

Grammar Specification

Formal EBNF grammar specification for the Braid language, based on the recursive descent parser implementation.

Notation

  • 'terminal' — literal token in quotes
  • nonterminal — grammar rule name
  • | — alternative production
  • {` ... `} — zero or more repetitions
  • [ ... ] — optional (zero or one)
  • ( ... ) — grouping

Lexical Grammar (Tokens)

letter     = 'a'..'z' | 'A'..'Z' | '_'
digit      = '0'..'9'
hex_digit  = digit | 'a'..'f' | 'A'..'F'

identifier = letter { letter | digit }
integer    = digit { digit }
float      = digit { digit } '.' digit { digit }
string     = '"' { char } '"' | "'" { char } "'"
comment    = '#' { char } newline
           | '//' { char } newline
           | '/*' { char } '*/'

keywords   = 'fn' | 'let' | 'if' | 'else' | 'while'
           | 'return' | 'struct' | 'enum' | 'match'
           | 'import' | 'extern' | 'impl' | 'diameter'
           | 'pole' | 'observe' | 'evolve' | 'native'
           | 'model' | 'true' | 'false' | 'with'

Syntax Grammar

Program Structure

program         = { declaration }

declaration     = import_decl
                | fn_decl
                | native_decl
                | extern_decl
                | struct_decl
                | enum_decl
                | diameter_decl
                | model_def
                | decorator_decl
                | impl_decl

Declarations

import_decl     = 'import' qualified_identifier

fn_decl         = 'fn' identifier '(' [params] ')' [ '->' type ] block

native_decl     = 'native' 'fn' identifier '(' [params] ')' ';'

extern_decl     = 'extern' 'fn' identifier '(' [params] ')' [ '->' type ] ';'

struct_decl     = 'struct' identifier '{' { field_decl } '}'

enum_decl       = 'enum' identifier '{' identifier { ',' identifier } '}'

diameter_decl   = 'diameter' ':' '{' pole_decl pole_decl '}'

model_def       = 'model' identifier '=' identifier '{' { key_value } '}'

impl_decl       = 'impl' identifier '{' { fn_decl } '}'

decorator_decl  = '@' 'autograd' fn_decl
                | '@' 'layer' struct_decl

pole_decl       = 'pole' identifier [ ':' ] block

key_value       = identifier ':' expression ';'

field_decl      = identifier ':' type ';'

params          = param { ',' param }

param           = identifier [ ':' type ]

qualified_identifier = identifier { '.' identifier }

Statements

statement       = var_decl ';'
                | return_stmt ';'
                | if_stmt
                | while_stmt
                | match_stmt
                | assignment ';'
                | expression ';'

block           = '{' { statement } '}'

var_decl        = 'let' identifier [ ':' type ] '=' expression

return_stmt     = 'return' [ expression ]

if_stmt         = 'if' expression block
                  [ 'else' ( if_stmt | block ) ]

while_stmt      = 'while' expression block

match_stmt      = 'match' expression '{' { match_case } '}'

match_case      = pattern '=>' ( expression ',' | block )

assignment      = expression '=' expression

Expressions

expression      = logical_or

logical_or      = logical_and { '||' logical_and }

logical_and     = comparison { '&&' comparison }

comparison      = addition { ( '<' | '<=' | '>' | '>=' | '==' | '!=' ) addition }

addition        = term { ( '+' | '-' ) term }

term            = unary { ( '*' | '/' | '%' ) unary }

unary           = ( '-' | '!' ) unary | postfix

postfix         = primary { postfix_op }

postfix_op      = '.' identifier           // member access
                | '(' [args] ')'           // function call
                | '[' expression ']'       // index access
                | '[' slice_spec ']'       // slice access

primary         = integer
                | float
                | string
                | bool_literal
                | 'nil'
                | identifier
                | identifier '{' { field_init } '}'    // struct literal
                | '[' { expression ',' } ']'           // array literal
                | '[[' { '[' { expression ',' } ']' } ',' ']]'  // tensor literal
                | '(' expression ')'
                | 'fn' '(' [params] ')' [ '->' type ] block    // anonymous fn
                | 'evolve' '(' expression ')'   // diameter evolve
                | 'observe' '(' expression ')'  // diameter observe

args = expression { ',' expression }

field_init = identifier ':' expression [ ',' ]

slice_spec = [ expression ] ':' [ expression ] [ ':' [ expression ] ]
           | '...'
           | expression             // single index

bool_literal = 'true' | 'false'

pattern = integer | string | bool_literal | 'nil' | identifier | '_'

Types

type            = identifier [ '<' type { ',' type } '>' ]

primitive_types = 'int' | 'float' | 'bool' | 'string' | 'nil'

identifier      = letter { letter | digit }

ML-Specific Constructs

tensor_literal  = '[[' { '[' { expression } ']' ',' } ']]'

slice_access    = postfix '[' slice_spec { ',' slice_spec } ']'

with_device     = 'with' 'device' '(' expression ')' block

device_expr     = expression '.' 'to' '(' string ')'

Operator Precedence (High to Low)

LevelOperatorsAssociativity
1() [] .Left
2- !Right (unary)
3* / %Left
4+ -Left
5< <= > >= == !=Left
6&&Left
7||Left
8=Right

Related Pages