Grammar Overview
This complete grammar reference covers both ML++ extensions (revolutionary AI/ML features) and the complete C++ standard grammar (based on N5008).
ML++ is a true superset of C++ - all valid C++ code is valid ML++ code. The language adds:
- • 60+ new keywords for tensors, neural networks, and training
- • 30+ new operators for tensor operations and automatic differentiation
- • Native automatic differentiation via the ∇ operator
- • Compile-time shape verification for all tensor operations
- • Zero-cost neural network abstractions as language constructs
Note: This grammar accepts a superset of valid constructs. Disambiguation rules, access control, and type rules distinguish valid constructs from syntactically valid but semantically invalid ones.
ML++ Extensions NEW
ML++ Keywords
Core Tensor Keywords
tensor ; Tensor type declaration shape ; Shape specification rank ; Tensor rank specification dtype ; Data type specification layout ; Memory layout specification
Neural Network Keywords
layer ; Neural network layer definition model ; Model definition (composition of layers) forward ; Forward pass declaration backward ; Backward pass declaration (optional) module ; Reusable module component network ; Alias for model
Differentiation Keywords
grad ; Gradient computation autodiff ; Enable automatic differentiation requires_grad ; Mark tensor as requiring gradients no_grad ; Disable gradient computation detach ; Detach from computation graph jacobian ; Jacobian matrix computation hessian ; Hessian matrix computation
Training & Optimization Keywords
train ; Training mode/loop declaration eval ; Evaluation mode optimizer ; Optimizer specification loss ; Loss function metric ; Evaluation metric epoch ; Training epoch batch ; Batch specification step ; Optimization step
Complete ML++ Keyword List
tensor shape rank dtype layout layer model forward backward module network grad autodiff requires_grad no_grad detach jacobian hessian train eval optimizer loss metric epoch batch step on device distributed parallel sync async cached checkpoint owned borrowed pinned stream quantized quantize dequantize qscheme calibrate reshape broadcast squeeze unsqueeze permute flatten unfold every until when pipeline sequence attention embedding recurrent convolution pooling normalization activation regularization augmentation
ML++ Operators
Complete Operator List
; Tensor Arithmetic Operators @ ; Matrix multiplication (a @ b) @@ ; Batch matrix multiplication ⊗ ; Tensor product (Kronecker product) ⊙ ; Element-wise (Hadamard) product ⊕ ; Concatenation operator ; Element-wise Operators .* ; Element-wise multiplication .+ ; Element-wise addition .- ; Element-wise subtraction ./ ; Element-wise division .^ ; Element-wise power ; Differentiation Operators ∇ ; Gradient operator (del/nabla) ∂ ; Partial derivative grad() ; Gradient function (ASCII alternative) ; Pipeline & Shape Operators |> ; Pipeline operator (layer composition) <> ; Shape constraint operator ~> ; Broadcast operator ; Reduction Operators ∑ ; Sum reduction ∏ ; Product reduction ⋁ ; Maximum reduction ⋀ ; Minimum reduction
Tensor Type Grammar
tensor-type: tensor < element-type , shape-spec [, option-list] > | matrix < element-type , integer , integer > | vector < element-type , integer > | scalar < element-type > | parameter < element-type , shape-spec > | buffer < element-type , shape-spec > element-type: float | double | int | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | bool | half | bfloat16 shape-spec: [ dimension-list ] dimension-list: dimension dimension-list , dimension dimension: integer ; Static dimension | ? ; Dynamic dimension | identifier ; Named dimension | ... ; Variable rank | * ; Wildcard option-list: option option-list , option option: requires_grad = bool-literal | device = device-spec | layout = layout-spec | pinned = bool-literal
Layer Declaration Grammar
layer-decl: attribute-specifier-seqopt layer identifier template-parameter-listopt base-layer-listopt { layer-member-list } layer-member-list: layer-member layer-member-list layer-member layer-member: parameter-decl | buffer-decl | sublayer-decl | forward-decl | backward-decl | init-decl | method-decl parameter-decl: identifier : parameter < type , shape > initializeropt ; forward-decl: attribute-specifier-seqopt forward ( parameter-list ) trailing-return-typeopt compound-stmt
Model Declaration Grammar
model-decl: attribute-specifier-seqopt model identifier template-parameter-listopt base-model-listopt { model-member-list } model-member-list: model-member model-member-list model-member model-member: sublayer-decl | parameter-decl | buffer-decl | forward-decl | training-step-decl | validation-step-decl | method-decl sublayer-decl: identifier : layer-type ; training-step-decl: training_step ( parameter-list ) trailing-return-typeopt compound-stmt
Training Statement Grammar
train-stmt: train ( model-expr , data-expr ) with-clauseopt ; with-clause: with { training-config } training-config: config-item training-config config-item config-item: config-key : config-value ; config-key: one of optimizer loss epochs batch_size device distributed mixed_precision gradient_accumulation gradient_clip checkpoint validation callbacks metrics
Automatic Differentiation Grammar
grad-expr: ∇ ( expression , var-list ) | grad ( expression , var-list ) | ∂ expression / ∂ identifier | jacobian ( expression , var-list ) | hessian ( expression , identifier ) var-list: identifier | [ identifier-list ] identifier-list: identifier identifier-list , identifier
C++ Standard Grammar ISO/IEC N5008
C++ Keywords
Context-dependent keywords are introduced by typedef, namespace, class, enumeration, and template declarations.
typedef-name: identifier simple-template-id namespace-name: identifier namespace-alias class-name: identifier simple-template-id enum-name: identifier template-name: identifier
Lexical Conventions
Preprocessing Tokens
preprocessing-token: header-name import-keyword module-keyword export-keyword identifier pp-number character-literal user-defined-character-literal string-literal user-defined-string-literal preprocessing-op-or-punc each non-whitespace character that cannot be one of the above operator-or-punctuator: one of { } [ ] ( ) <: :> <% %> ; : ... ? :: . .* -> ->* ~ ! + - * / % ^ & | = += -= *= /= %= ^= &= |= == != < > <= >= <=> && || << >> <<= >>= ++ -- , and or xor not bitand bitor compl and_eq or_eq xor_eq not_eq
Identifiers
identifier: identifier-start identifier identifier-continue identifier-start: nondigit an element of the translation character set with Unicode property XID_Start identifier-continue: digit nondigit an element of the translation character set with Unicode property XID_Continue nondigit: one of a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _ digit: one of 0 1 2 3 4 5 6 7 8 9
Literals
literal: integer-literal character-literal floating-point-literal string-literal boolean-literal pointer-literal user-defined-literal boolean-literal: false true pointer-literal: nullptr
Expressions
Primary Expressions
primary-expression: literal this ( expression ) id-expression lambda-expression fold-expression requires-expression id-expression: unqualified-id qualified-id pack-index-expression unqualified-id: identifier operator-function-id conversion-function-id literal-operator-id ~ type-name ~ computed-type-specifier template-id
Postfix Expressions
postfix-expression: primary-expression postfix-expression [ expression-listopt ] postfix-expression ( expression-listopt ) simple-type-specifier ( expression-listopt ) typename-specifier ( expression-listopt ) simple-type-specifier braced-init-list typename-specifier braced-init-list postfix-expression . templateopt id-expression postfix-expression -> templateopt id-expression postfix-expression ++ postfix-expression -- dynamic_cast < type-id > ( expression ) static_cast < type-id > ( expression ) reinterpret_cast < type-id > ( expression ) const_cast < type-id > ( expression ) typeid ( expression ) typeid ( type-id )
Lambda Expressions
lambda-expression: lambda-introducer attribute-specifier-seqopt lambda-declarator compound-statement lambda-introducer < template-parameter-list > requires-clauseopt attribute-specifier-seqopt lambda-declarator compound-statement lambda-introducer: [ lambda-captureopt ] lambda-capture: capture-default capture-list capture-default , capture-list capture-default: & = capture-list: capture capture-list , capture capture: simple-capture init-capture
Statements
statement: labeled-statement attribute-specifier-seqopt expression-statement attribute-specifier-seqopt compound-statement attribute-specifier-seqopt selection-statement attribute-specifier-seqopt iteration-statement attribute-specifier-seqopt jump-statement attribute-specifier-seqopt assertion-statement declaration-statement attribute-specifier-seqopt try-block selection-statement: if constexpropt ( init-statementopt condition ) statement if constexpropt ( init-statementopt condition ) statement else statement if !opt consteval compound-statement if !opt consteval compound-statement else statement switch ( init-statementopt condition ) statement iteration-statement: while ( condition ) statement do statement while ( expression ) ; for ( init-statement conditionopt ; expressionopt ) statement for ( init-statementopt for-range-declaration : for-range-initializer ) statement jump-statement: break ; continue ; return expr-or-braced-init-listopt ; coroutine-return-statement goto identifier ;
Declarations
declaration: name-declaration special-declaration name-declaration: block-declaration nodeclspec-function-declaration function-definition friend-type-declaration template-declaration deduction-guide linkage-specification namespace-definition empty-declaration attribute-declaration module-import-declaration simple-declaration: decl-specifier-seq init-declarator-listopt ; attribute-specifier-seq decl-specifier-seq init-declarator-list ; structured-binding-declaration initializer ; decl-specifier: storage-class-specifier defining-type-specifier function-specifier friend typedef constexpr consteval constinit inline
Classes
class-specifier: class-head { member-specificationopt } class-head: class-key attribute-specifier-seqopt class-head-name class-property-specifier-seqopt base-clauseopt class-key attribute-specifier-seqopt base-clauseopt class-key: class struct union member-specification: member-declaration member-specificationopt access-specifier : member-specificationopt member-declaration: attribute-specifier-seqopt decl-specifier-seqopt member-declarator-listopt ; function-definition friend-type-declaration using-declaration using-enum-declaration static_assert-declaration template-declaration
Templates
template-declaration: template-head declaration template-head concept-definition template-head: template < template-parameter-list > requires-clauseopt template-parameter: type-parameter parameter-declaration type-tt-parameter variable-tt-parameter concept-tt-parameter type-parameter: type-parameter-key ...opt identifieropt type-parameter-key identifieropt = type-id type-constraint ...opt identifieropt type-constraint identifieropt = type-id type-parameter-key: class typename
Additional C++ Grammar Sections:
- • Modules - Module declarations, imports, and exports
- • Overloading - Operator overloading and function resolution
- • Exception Handling - Try-catch blocks and exception specifications
- • Preprocessing - Preprocessor directives and macro expansion
For complete C++ grammar details, refer to ISO/IEC N5008 Annex A or the full README.md documentation.
Complete Reference
This page shows key sections. Download the complete ML++ specification with full C++ grammar.