Skip to content
Snippets Groups Projects
grammar.txt 4.88 KiB
Newer Older
Peter Matta's avatar
Peter Matta committed
Program -> eof                               // eof | epsilon
Peter Matta's avatar
Peter Matta committed
Program -> ';'                               // ';'
Program -> Decl ';' Program                  // 'let', 'var'
Peter Matta's avatar
Peter Matta committed
Program -> Func Program                      // 'func'
Peter Matta's avatar
Peter Matta committed

Peter Matta's avatar
Peter Matta committed
Decl -> 'var' identifier DeclVar ';'         // 'var'
Decl -> 'let' identifier DeclLet ';'         // 'let'
Decl -> 'extern' FuncDecl                    // 'extern'
Peter Matta's avatar
Peter Matta committed
DeclVal -> '=' Expr                          // '='
Peter Matta's avatar
Peter Matta committed

Peter Matta's avatar
Peter Matta committed
DeclVar -> ':' 'Int' InitVal                 // ':'
DeclVar -> '=' Expr                          // '='

ValType -> ':' 'Int' InitVal                 // ':'
ValType -> ':' 'Int'                         // ':'

InitVal -> epsilon                           // ';'
InitVal -> '=' Expr                          // '='

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

RetType -> epsilon                           // _ | '{', ';'
Peter Matta's avatar
Peter Matta committed
RetType -> '->' RetType_                     // '->'
Peter Matta's avatar
Peter Matta committed
RetType_ -> 'Void'                           // 'Void'
Peter Matta's avatar
Peter Matta committed
RetType_ -> 'Int'                            // 'Int'
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

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

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          // '==', '!=', '<', '>', '<=', '>='

Peter Matta's avatar
Peter Matta committed
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

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           // '*', '/', '%'
Peter Matta's avatar
Peter Matta committed

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

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


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

FuncCall -> '(' Args ')'                     // '('
Peter Matta's avatar
Peter Matta committed
Subscript -> '[' Expr ']'                    // '['
Peter Matta's avatar
Peter Matta committed

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

VarPattern -> '(' VarPattern_ ')'            // '('
VarPattern_ -> epsilon                       // ')'
Peter Matta's avatar
Peter Matta committed
VarPattern_ -> identifier: Int VarPattern__  // identifier
Peter Matta's avatar
Peter Matta committed
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 -> '[' Expr ArrayBody ']'       // '['
ArrayBody -> epsilon                         // _ | ']'
ArrayBody -> Expr ',' ArrayBody              // '(', identifier, number_literal, '-', '!',  '['
Peter Matta's avatar
Peter Matta committed
Range -> Expr Range_                         // identifier, number_literal, '('
Range_ -> '..' Expr                          // '..'
Range_ -> '...' Expr                         // '...'