|
|
@@ -29,57 +29,69 @@
|
|
|
- **Literals**. Numbers,strings,Booleans, and nil
|
|
|
- **Unary expressions.** A prefix ! to perform a logical not, and - to negate a number.
|
|
|
- **Binary expressions.** The infix arithmetic(+,-,*,/) and logic operators (==, !=, <, <=,>,>=)
|
|
|
-- **Parentheses.** A pair of ( and ) wrapped around and expression.
|
|
|
+ - **Parentheses.** A pair of ( and ) wrapped around and expression.
|
|
|
|
|
|
- - Syntax tree - Expression
|
|
|
+ - Syntax tree - Expression
|
|
|
|
|
|
- ```
|
|
|
- expression → literal
|
|
|
- | unary
|
|
|
- | binary
|
|
|
- | grouping ;
|
|
|
-
|
|
|
- literal → NUMBER | STRING | "true" | "false" | "nil" ;
|
|
|
- grouping → "(" expression ")" ;
|
|
|
- unary → ( "-" | "!" ) expression ;
|
|
|
- binary → expression operator expression ;
|
|
|
- operator → "==" | "!=" | "<" | "<=" | ">" | ">="
|
|
|
- | "+" | "-" | "*" | "/" ;
|
|
|
-
|
|
|
- ```
|
|
|
+ ```
|
|
|
+ expression → literal
|
|
|
+ | unary
|
|
|
+ | binary
|
|
|
+ | grouping ;
|
|
|
|
|
|
-| Name | Operators | Associates |
|
|
|
-|------------|-----------|------------|
|
|
|
-| Equality | == != | Left |
|
|
|
-| Comparison | > >= < <= | Left |
|
|
|
-| Term | - + | Left |
|
|
|
-| Factor | / * | Left |
|
|
|
-| Unary | ! - | Right |
|
|
|
+ literal → NUMBER | STRING | "true" | "false" | "nil" ;
|
|
|
+ grouping → "(" expression ")" ;
|
|
|
+ unary → ( "-" | "!" ) expression ;
|
|
|
+ binary → expression operator expression ;
|
|
|
+ operator → "==" | "!=" | "<" | "<=" | ">" | ">="
|
|
|
+ | "+" | "-" | "*" | "/" ;
|
|
|
|
|
|
-```
|
|
|
-expression → equality ;
|
|
|
-equality → comparison ( ( "!=" | "==" ) comparison )* ;
|
|
|
-comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
|
|
|
-term → factor ( ( "-" | "+" ) factor )* ;
|
|
|
-factor → unary ( ( "/" | "*" ) unary )* ;
|
|
|
-unary → ( "!" | "-" ) unary
|
|
|
- | primary ;
|
|
|
-primary → NUMBER | STRING | "true" | "false" | "nil"
|
|
|
- | "(" expression ")" ;
|
|
|
-```
|
|
|
+ ```
|
|
|
|
|
|
-- Syntax tree - Statement
|
|
|
+ | Name | Operators | Associates |
|
|
|
+ |------------|-----------|------------|
|
|
|
+ | Equality | == != | Left |
|
|
|
+ | Comparison | > >= < <= | Left |
|
|
|
+ | Term | - + | Left |
|
|
|
+ | Factor | / * | Left |
|
|
|
+ | Unary | ! - | Right |
|
|
|
|
|
|
-```
|
|
|
-program → statement* EOF ;
|
|
|
+ ```
|
|
|
+ expression → equality ;
|
|
|
+ equality → comparison ( ( "!=" | "==" ) comparison )* ;
|
|
|
+ comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
|
|
|
+ term → factor ( ( "-" | "+" ) factor )* ;
|
|
|
+ factor → unary ( ( "/" | "*" ) unary )* ;
|
|
|
+ unary → ( "!" | "-" ) unary
|
|
|
+ | primary ;
|
|
|
+ primary → NUMBER | STRING | "true" | "false" | "nil"
|
|
|
+ | "(" expression ")" | IDENTIFIER ;
|
|
|
+ ```
|
|
|
|
|
|
-statement → exprStmt
|
|
|
- | printStmt ;
|
|
|
+ - Syntax tree - Statement
|
|
|
|
|
|
-exprStmt → expression ";" ;
|
|
|
-printStmt → "print" expression ";" ;
|
|
|
-```
|
|
|
+ ```
|
|
|
+ program → statement* EOF ;
|
|
|
+
|
|
|
+ statement → exprStmt
|
|
|
+ | printStmt ;
|
|
|
+
|
|
|
+ exprStmt → expression ";" ;
|
|
|
+ printStmt → "print" expression ";" ;
|
|
|
+ ```
|
|
|
|
|
|
- A program is a list of statements followed by the special "end of file" token.
|
|
|
- The mandatory end token ensures the parse consumes the entire input and don't
|
|
|
- silently ignore erroneous unconsumed tokens at the end of a script.
|
|
|
+ A program is a list of statements followed by the special "end of file" token.
|
|
|
+ The mandatory end token ensures the parse consumes the entire input and don't
|
|
|
+ silently ignore erroneous unconsumed tokens at the end of a script.
|
|
|
+ - Syntax tree - add Variable syntax
|
|
|
+ ```
|
|
|
+ program → declaration* EOF ;
|
|
|
+
|
|
|
+ declaration → varDecl
|
|
|
+ | statement ;
|
|
|
+
|
|
|
+ statement → exprStmt
|
|
|
+ | printStmt ;
|
|
|
+
|
|
|
+ varDecl → "var" IDENTIFIER ( "=" expression )? ";" ;
|
|
|
+ ```
|