附錄I Appendix I
Here is a complete grammar for Lox. The chapters that introduce each part of the language include the grammar rules there, but this collects them all into one place.
這裡有一份Lox的完整語法。介紹語言每個部分的章節中都包含對應的語法規則,但這裡將它們全部收錄在一起了。
A1 . 1 Syntax Grammar
A1.1 語法
The syntactic grammar is used to parse the linear sequence of tokens into the nested syntax tree structure. It starts with the first rule that matches an entire Lox program (or a single REPL entry).
語法用於將詞法標識(token)的線性序列解析為巢狀的語法樹結構。它從匹配整個Lox程式(或單條REPL輸入)的第一個規則開始。
program → declaration* EOF ;
A1 . 1 . 1 Declarations
A1.1.1 宣告
A program is a series of declarations, which are the statements that bind new identifiers or any of the other statement types.
一個程式就是一系列的宣告,也就是繫結新識別符號或其它statement型別的語句。
declaration → classDecl
| funDecl
| varDecl
| statement ;
classDecl → "class" IDENTIFIER ( "<" IDENTIFIER )?
"{" function* "}" ;
funDecl → "fun" function ;
varDecl → "var" IDENTIFIER ( "=" expression )? ";" ;
A1 . 1 . 2 Statements
A1.1.2 語句
The remaining statement rules produce side effects, but do not introduce bindings.
其餘的語句規則會產生副作用,但不會引入繫結。
statement → exprStmt
| forStmt
| ifStmt
| printStmt
| returnStmt
| whileStmt
| block ;
exprStmt → expression ";" ;
forStmt → "for" "(" ( varDecl | exprStmt | ";" )
expression? ";"
expression? ")" statement ;
ifStmt → "if" "(" expression ")" statement
( "else" statement )? ;
printStmt → "print" expression ";" ;
returnStmt → "return" expression? ";" ;
whileStmt → "while" "(" expression ")" statement ;
block → "{" declaration* "}" ;
Note that
blockis a statement rule, but is also used as a nonterminal in a couple of other rules for things like function bodies.
請注意,block是一個語句規則,但在其它規則中也作為非終止符使用,用於表示函式體等內容。
A1 . 1 . 3 Expressions
A1.1.3 表示式
Expressions produce values. Lox has a number of unary and binary operators with different levels of precedence. Some grammars for languages do not directly encode the precedence relationships and specify that elsewhere. Here, we use a separate rule for each precedence level to make it explicit.
表示式會產生值。Lox有許多具有不同優先順序的一元或二元運算子。一些語言的語法中沒有直接編碼優先順序關係,而是在其它地方指定。在這裡,我們為每個優先順序使用單獨的規則,使其明確。
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 ( "(" arguments? ")" | "." IDENTIFIER )* ;
primary → "true" | "false" | "nil" | "this"
| NUMBER | STRING | IDENTIFIER | "(" expression ")"
| "super" "." IDENTIFIER ;
A1 . 1 . 4 Utility rules
A1.1.4 實用規則
In order to keep the above rules a little cleaner, some of the grammar is split out into a few reused helper rules.
為了使上面的規則更簡潔一點,一些語法被拆分為幾個重複使用的輔助規則。
function → IDENTIFIER "(" parameters? ")" block ;
parameters → IDENTIFIER ( "," IDENTIFIER )* ;
arguments → expression ( "," expression )* ;
A1 . 2 Lexical Grammar
A1.2 詞法
The lexical grammar is used by the scanner to group characters into tokens. Where the syntax is context free, the lexical grammar is regular—note that there are no recursive rules.
詞法被掃描器用來將字元分組為詞法標識(token)。語法是上下文無關的,詞法是正則的——注意這裡沒有遞迴規則。
NUMBER → DIGIT+ ( "." DIGIT+ )? ;
STRING → "\"" <any char except "\"">* "\"" ;
IDENTIFIER → ALPHA ( ALPHA | DIGIT )* ;
ALPHA → "a" ... "z" | "A" ... "Z" | "_" ;
DIGIT → "0" ... "9" ;