A1
附录 I
这里给出了 Lox 语言的完整语法。介绍语言每个部分的章节中都包含了相应的语法规则,但这里将它们集中在一起。
A1 . 1语法语法
语法语法用于将线性序列的标记解析为嵌套的语法树结构。它从第一个规则开始,该规则匹配整个 Lox 程序(或单个 REPL 输入)。
program → declaration* EOF ;
A1 . 1 . 1声明
程序是一系列声明,声明是绑定新标识符或任何其他类型的语句。
declaration → classDecl | funDecl | varDecl | statement ; classDecl → "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ; funDecl → "fun" function ; varDecl → "var" IDENTIFIER ( "=" expression )? ";" ;
A1 . 1 . 2语句
其余的语句规则产生副作用,但不引入绑定。
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* "}" ;
请注意,block
是一个语句规则,但它也用作其他几个规则中的非终结符,例如函数体。
A1 . 1 . 3表达式
表达式生成值。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实用程序规则
为了使上述规则更简洁,一些语法被拆分为几个可重复使用的辅助规则。
function → IDENTIFIER "(" parameters? ")" block ; parameters → IDENTIFIER ( "," IDENTIFIER )* ; arguments → expression ( "," expression )* ;
A1 . 2词汇语法
词法语法由扫描器用于将字符分组为标记。语法是上下文无关的,词法语法是正则的—请注意,没有递归规则。
NUMBER → DIGIT+ ( "." DIGIT+ )? ; STRING → "\"" <any char except "\"">* "\"" ; IDENTIFIER → ALPHA ( ALPHA | DIGIT )* ; ALPHA → "a" ... "z" | "A" ... "Z" | "_" ; DIGIT → "0" ... "9" ;