目录
正在加载目录…
正在阅读专栏 Crafting Interpreters:手写解释器 第 34 篇 · 共 35 篇 · 97%

Lox Grammar:完整语法与词法规范

发布于 2026-09-16 19:32 · 最后编辑于 2026-09-17 22:07 · 版本 v2 · 字数 760 · 预计阅读 2 分钟 👁 13 次阅读

Crafting Interpreters:Appendix I:Lox Grammar

汇总 Lox 的完整 syntax grammar 与 lexical grammar,作为前面章节实现 parser/scanner 的可检索规范。

TL;DR

  • syntax grammar 描述 declarations、statements 和 expressions 的结构组合。
  • lexical grammar 描述 token、literal、identifier、keyword 和 whitespace。
  • grammar 中的 precedence 从 expression、assignment、logic 到 primary 逐层收紧。
  • 本附录是实现或扩展 Lox parser 时的权威速查表。

目录

章节内容
Syntax grammar语法产生式
Lexical grammar词法产生式

Syntax grammar

本节按 declarations、statements、expressions 和 utility rules 汇总完整 grammar。产生式中的大写名称表示 token,其他名称表示 non-terminal。

Lexical grammar

词法 grammar 由 scanner 直接实现:它决定字符如何组成 token,以及 parser 看到的 token 序列边界。扩展语言时,应先修改 lexical grammar,再同步 token type 和 scanner。

原文 grammar 片段

以下产生式按官方页面顺序保留。

参考资料

Grammar block 1

text
program        → declaration* EOF ;

Grammar block 2

text
declaration    → classDecl
               | funDecl
               | varDecl
               | statement ;

classDecl      → "class" IDENTIFIER ( "<" IDENTIFIER )?
                 "{" function* "}" ;
funDecl        → "fun" function ;
varDecl        → "var" IDENTIFIER ( "=" expression )? ";" ;

Grammar block 3

text
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* "}" ;

Grammar block 4

text
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 ;

Grammar block 5

text
function       → IDENTIFIER "(" parameters? ")" block ;
parameters     → IDENTIFIER ( "," IDENTIFIER )* ;
arguments      → expression ( "," expression )* ;

Grammar block 6

text
NUMBER         → DIGIT+ ( "." DIGIT+ )? ;
STRING         → "\"" <any char except "\"">* "\"" ;
IDENTIFIER     → ALPHA ( ALPHA | DIGIT )* ;
ALPHA          → "a" ... "z" | "A" ... "Z" | "_" ;
DIGIT          → "0" ... "9" ;

参考资料

← 返回列表
← 上一篇 Optimization:性能测量、hash probing 与 NaN boxing 下一篇 → Generated Syntax Tree Classes:Expr 与 Stmt AST

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论