Skip to content

Latest commit

 

History

History
46 lines (44 loc) · 2.18 KB

File metadata and controls

46 lines (44 loc) · 2.18 KB

Grammar without precedence level

- expression    →   literal | unary | binary | grouping ;
- literal       →   NUMBER | STRING | "true" | "false" | "nil" ;
- grouping      →   "(" expression ")" ;
- unary         →   ( "!" | "-" ) expression ;
- binary        →   expression operator expression ;
- operator      →   "==" | "!=" | "<" | "<=" | ">" | ">=" | "+" | "-" | "*" | "/" ;

Grammar with precedence level

- expression    →   assignment ;
- assignment    →   ( call "." )? IDENTIFIER "=" assignment | logic_or ;
- logic_or      →   logic_and ( "or" logic_and )* ;
- logic_and     →   equality ( "and" equality )* ;
- equality      →   comparison ( ( "==" | "!=" ) comparison )* ;
- comparison    →   term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
- term          →   factor ( ( "-" | "+" ) factor )* ;
- factor        →   unary ( ( "*" | "/" ) unary )* ;
- unary         →   ( "!" | "-" ) unary | call ;
- call          →   primary ( "(" argument? ")" | "." IDENTIFIER )* ;
- argument      →   expression ( "," expression )* ;
- primary       →   NUMBER | STRING | "true" | "false" | "nil" |
                    "(" expression ")" | IDENTIFIER | "this" | "super" "." IDENTIFIER ;

Grammer for statement

- program       →   declaration* EOF ;
- declaration   →   classDecl | funDecl | varDecl | statement ;
- classDecl     →   "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ;
- funDecl       →   "fun" function ;
- function      →   IDENTIFIER "(" parameters? ")" block ;
- parameters    →   IDENTIFIER ( "," IDENTIFIER )* ;
- varDecl       →   "var" IDENTIFIER ( "=" expression )? ";" ;
- statement     →   exprStmt | ifStmt | printStmt | whileStmt | forStmt | returnStmt | block ;
- ifStmt        →   "if" "(" expression ")" statement ( "else" statement)? ;
- whileStmt     →   "while" "(" expression ")" statement ;
- forStmt       →   "for" "(" ( varDecl | exprStmt |
                    ";" ) expression? ";" expression? ")" statement ;
- returnStmt    →   "return" expression? ";" ;
- block         →   "{" declaration* "}" ;
- exprStmt      →   expression ";" ;
- printStmt     →   "print" expression ";" ;