Skip to content
Snippets Groups Projects
grammar.txt 4.39 KiB
Newer Older
Peter Matta's avatar
Peter Matta committed
Program -> eof                               // eof | epsilon
Peter Matta's avatar
Peter Matta committed
Program -> Decl ';' Program                  // 'const', 'var'
Program -> Expr ';' Program                  // 'const', 'var'
Peter Matta's avatar
Peter Matta committed
Program -> Func Program                      // 'func'
Peter Matta's avatar
Peter Matta committed
Program -> Stmt Program                      // 'for', 'while', 'if'
Peter Matta's avatar
Peter Matta committed

Peter Matta's avatar
Peter Matta committed
Decl -> 'var' identifier DeclVal ';'         // 'var'
Decl -> 'const' identifier DeclVal ';'       // 'cons'
DeclVal -> '=' Expr                          // '='
Peter Matta's avatar
Peter Matta committed

Peter Matta's avatar
Peter Matta committed
// Decl_ -> '[' DeclArray                       // '['
// DeclArray -> Expr                            // '(', identifier, number_literal, '-'
// DeclArray -> ']' '=' ArrayLiteral            // ']'
Peter Matta's avatar
Peter Matta committed

Peter Matta's avatar
Peter Matta committed
Func -> 'func' identifier '(' Args ')' Block // 'func'
Peter Matta's avatar
Peter Matta committed

Peter Matta's avatar
Peter Matta committed
Block -> '{' Block_ '}'                      // '{'
Block_ -> epsilon                            // _ | '}'
Block_ -> Decl ';' Block_                    // 'var' | '}'
Block_ -> Expr ';' Block_                    // identifier, number_literal, '(' | '}'
Block_ -> Stmt Block_                        // 'for', 'while', 'if' | '}'
Peter Matta's avatar
Peter Matta committed

Expr          -> AssignExpr                  // '(', identifier, number_literal, '-'

AssignExpr    -> LogExpr AssignExpr_         // '(', identifier, number_literal, '-'
Peter Matta's avatar
Peter Matta committed
AssignExpr_   -> epsilon                     // _ | '..', '...', ')', '{', ']', ',', ';'
AssignExpr_   -> '=' Expr                    // '='
Peter Matta's avatar
Peter Matta committed

LogExpr       -> ArithAddExpr LogExpr_       // '(', identifier, number_literal, '-'
LogExpr_      -> epsilon                     // _ | '=', ')', ';'
Peter Matta's avatar
Peter Matta committed
LogExpr_      -> LogOp ArithAddExpr          // '==', '!=', '<', '>', '<=', '>='

ArithAddExpr  -> ArithMulExpr ArithAddExpr_  // '(', identifier, number_literal, '-'
Peter Matta's avatar
Peter Matta committed
ArithAddExpr_ -> epsilon                     // _ | '==', '!=', '<', '>', '<=', '>=', '=', '..', '...', ')', '{', ']', ',', ';'
ArithAddExpr_ -> AddOp ArithMulExpr          // '+', '-'
Peter Matta's avatar
Peter Matta committed

ArithMulExpr  -> PrimaryExpr ArithMulExpr_   // '(', identifier, number_literal, '-'
Peter Matta's avatar
Peter Matta committed
ArithMulExpr_ -> epsilon                     // _ | '+', '-', '==', '!=', '<', '>', '<=', '>=', '=', '..', '...', ')', '{', ']', ',', ';'
Peter Matta's avatar
Peter Matta committed
ArithMulExpr_ -> MulOp PrimaryExpr           // '*', '/'

PrimaryExpr   -> '(' Expr ')'                // '('
PrimaryExpr   -> identiier IdExpr            // identifier
Peter Matta's avatar
Peter Matta committed
PrimaryExpr   -> number_literal              // number_literal
PrimaryExpr   -> '-' PrimaryExpr             // '-'
PrimaryExpr   -> '!' PrimaryExpr             // '!'
Peter Matta's avatar
Peter Matta committed

Peter Matta's avatar
Peter Matta committed
IdExpr        -> epsilon                     // _ | '*', '/', '%', '+', '-', '==', '!=', '<', '>', '<=', '>=', '=', '..', '...', ')', '{', ']', ',', ';'
IdExpr        -> '(' Args ')'                // '('
IdExpr        -> '[' Expr ']'                // '['
Peter Matta's avatar
Peter Matta committed


Stmt -> For                                  // 'for'
Stmt -> While                                // 'while'
Stmt -> If                                   // 'if'

FuncCall -> epsilon                          // _ | Operator, '..', '...', ')', '{', ']', ',', ';'
FuncCall -> '(' Args ')'                     // '('

ExprPattern -> '(' ExprPattern_ ')'          // '('
ExprPattern_ -> epsilon                      // _ | ')'
ExprPattern_ -> Expr ExprPattern__           // identifier, number_literal, '('
ExprPattern__ -> epsilon                     // ')'
ExprPattern__ -> ',' Expr ExprPattern__      // ','

VarPattern -> '(' VarPattern_ ')'            // '('
VarPattern_ -> epsilon                       // ')'
VarPattern_ -> identifier VarPattern__       // identifier
VarPattern__ -> epsilon                      // ')'
VarPattern__ -> ',' identifier               // ','
                VarPattern__


For -> 'for' identifier 'in' Range Block     // 'for'
While -> 'while' Expr Block                  // 'while'
If -> 'if' Expr Block Else                   // 'if'
Else -> epsilon                              // _ | 'var', identifier, number_literal, '(', 'for', 'while', 'if', '}'
Else -> 'else' Block                         // 'else'

Args -> epsilon                              // _ | ')'
Args -> identifier Args_                     // identifier
Args_ -> epsilon                             // _ | ')'
Args_ -> ',' identifier Args_                // ','

Peter Matta's avatar
Peter Matta committed
ArrayLiteral -> '[' ArrayBody ']'            //
ArrayBody -> epsilon                         //
ArrayBody -> Expr ',' ArrayBody                 //

Peter Matta's avatar
Peter Matta committed
Range -> Expr Range_                         // identifier, number_literal, '('
Range_ -> '..' Expr                          // '..'
Range_ -> '...' Expr                         // '...'