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

Crafting Interpreters:A Map of the Territory

发布于 2026-09-16 16:31 · 最后编辑于 2026-09-17 22:06 · 版本 v2 · 字数 5,062 · 预计阅读 11 分钟 👁 10 次阅读

Crafting Interpreters:A Map of the Territory

作者:Robert Nystrom
原文章节:2 A Map of the Territory

logotype

TL;DR

本章从全景角度介绍 language implementation 的主要阶段:scanning、parsing、static analysis、IR、optimization、code generation、VM 与 runtime;随后比较 interpreter、compiler、transpiler 和 JIT 等路线。

目录

章节说明
A Map of the Territory本章导言
The Parts of a Languagelanguage implementation pipeline
Shortcuts and Alternate Routes不同实现路线
Compilers and Interpreters两者关系
Our Journey本书路线

A Map of the Territory

You must have a map, no matter how rough. Otherwise you wander all over the place.
——《The Lord of the Rings》

I never made anyone go farther than he could on a given day.
——J. R. R. Tolkien

为了不在旅途中四处迷路,出发前先看看前人实现 programming language 时走过的 territory。这能帮助我们理解目的地,以及其他人走过的 alternate route。

先约定一个 shorthand。本书大量讨论的是 language 的 implementation,而不是某个 Platonic ideal 中的 language itself。“stack”、“bytecode”和“recursive descent”都是某一实现可能采用的 nuts and bolts。从 user 角度,只要最终装置忠实遵守 language specification,它们就都是 implementation detail。除非 distinction 很重要,否则下文会用 language 同时指 language、implementation,或者两者。

The Parts of a Language

从 computing 的 Dark Ages 开始,engineer 就一直在构建 programming language。机器一能与人沟通,我们就发现直接沟通太难,于是让机器帮忙。如今 machine 快了百万倍、storage 多了几个数量级,但构建 programming language 的方式几乎没有改变。

Language designer 探索的区域很大,但他们开出的道路并不多。不同 language 偶尔会走 shortcut,但总体从 Grace Hopper 的第一台 COBOL compiler 到新的 transpile-to-JavaScript language,都令人安心地相似。

可以把 implementation 可能选择的 path 想象成一座山:山脚是 raw source text,一串 characters;每个 phase 分析 program,并把它变换成更高层的 representation,使 semantics——author 希望 computer 做什么——变得更明显。

到达山顶时,我们拥有 user program 的 bird’s-eye view,能够看懂 code 的 meaning;随后从另一侧下山,把最高层 representation 逐步转换成更低层的形式,越来越接近 CPU 能够执行的东西。

mountain

Scanning

第一步是 scanning,也叫 lexing,或者 lexical analysis。Scanner(lexer)接收线性的 character stream,把 characters 组合成更像“words”的一系列 token。有些 token 是单个 character,比如括号;另一些较长,比如 number、string literal 和 identifier。

Source file 中有些 characters 没有实际意义。Whitespace 通常 insignificant,comment 按定义会被 language 忽略。Scanner 通常丢弃它们,只留下有意义的 token sequence。

text
var average = (min + max) / 2;

string

Scanner 之后会得到 token sequence:

tokens

Parsing

下一步是 parsing。这里 language 获得 grammar:把更大的 expression 和 statement 从更小的部分组合起来的能力。Parser 接收扁平的 token sequence,构建反映 grammar 嵌套结构的 tree。这些 tree 叫 parse tree 或 abstract syntax tree,实践中通常简称 syntax tree、AST 或 tree。

Parser 还负责报告 syntax error。Programming language 的 grammar 比 human language 简单,但人仍然会把简单 grammar 写错。

ast

Static analysis

完成 parsing 后,我们知道 code 的 syntactic structure,却还不知道 identifier 指向什么。Static analysis 会做 binding 或 resolution:找到每个 name 的 declaration,并把两者连接起来。这里会涉及 scope,也就是某个 name 可以引用某个 declaration 的 source-code region。

如果 language 是 statically typed,这里还会进行 type checking。Lox 是 dynamically typed language,因此 type checking 会推迟到 runtime。

分析得到的 semantic information 可以存为 syntax tree node 的 attributes,也可以放进 symbol table。更强大的方式,是把 tree 转成更直接表达 semantics 的新 data structure。

到这里属于 front end。之后通常叫 back end;中间新增的 phase 被称为 middle end。

Intermediate representations

Compiler 可以看作 pipeline:每个 stage 都把表示 user code 的 data 组织成更便于下一 stage 实现的形式。Front end 针对 source language,back end 关注最终运行的平台;中间可以使用与 source 和 destination 都不紧密绑定的 intermediate representation(IR)。

IR 是两种 language 之间的 interface。常见风格包括 control flow graph、static single-assignment、continuation-passing style 和 three-address code。

共享 IR 能显著减少多 source language、多 target architecture 的 compiler 数量:每种 source language 一个 front end 生成 IR,每种 target architecture 一个 back end 生成 native code。GCC 能支持众多 language 与 architecture,正是因为 front end 主要生成 GIMPLE 和 RTL 等 IR,target back end 再将其转成 native code。

Optimization

理解 program meaning 后,可以把它替换成 semantics 相同但更 efficient 的另一个 program,这就是 optimization。最简单的例子是 constant folding:如果 expression 的 operands 都是 constant,就在 compile time 计算结果,而不是 runtime 再算一次。

Optimization 必须保持 semantics 不变;“更快”不能以改变 program behavior 为代价。

plants

Code generation

下山阶段最终要生成 target code。Code generator 把 IR 或更高层 representation 映射为 CPU instruction、register、memory layout 和 calling convention。不同 target architecture 会需要不同 back end。

Virtual machine

有时目标不是 native machine code,而是 virtual machine 的 bytecode。VM 提供抽象 instruction set,compiler 生成 bytecode,runtime 的 VM 负责执行它。这样可以用同一份 bytecode 在不同平台运行,只需为各平台实现 VM。

Runtime

Runtime 是 language 在执行时依赖的 support system,包括 memory allocation、garbage collection、object representation、I/O、call stack 和 native library interface。许多 language feature 最终都需要 runtime 配合。

Shortcuts and Alternate Routes

完整 pipeline 并非唯一路线。资源有限或 language 简单时,可以合并 phase,省略 IR,或直接从 AST 解释。

Single-pass compilers

Single-pass compiler 尽量边读取 source 边生成 output,不构建完整 AST 或 IR。它速度快、memory 使用少,但要求 language syntax 和 declaration rules 适合前向处理,较难做全局 optimization。

Tree-walk interpreters

Tree-walk interpreter 直接遍历 AST 并执行。它实现简单、适合教学和小型 language,但每次执行都要重复走 tree,通常不如 bytecode VM 或 native code 高效。

Transpilers

Transpiler 把一种 high-level language 翻译成另一种 high-level language,例如把 language 翻译成 JavaScript。它可以复用目标 language 的 runtime、toolchain 与 platform support,但需要处理两门 language 的 semantics 差异。

Just-in-time compilation

JIT 在 runtime 收集执行信息,把频繁执行的 code 编译成更快的 machine code。它结合 interpreter 的启动速度和 compiler 的长期性能,但实现复杂,需要 profiling、deoptimization 与 runtime support。

Compilers and Interpreters

Compiler 与 interpreter 不是互斥的两类程序,而是执行 language 的不同路线。Compiler 预先把 code 转换成另一种 representation;interpreter 在运行时读取 representation 并执行。实际系统常常混合二者:先编译成 bytecode,再由 VM 解释或 JIT。

venn

Our Journey

本书的路线是先实现 tree-walk interpreter,再实现 bytecode VM。第一套 interpreter 用 Java,重点是 language concept 与 semantics;第二套用 C,重点是 representation、memory、performance 与 runtime。

我们会从 scanning、parsing 和 AST 开始,逐步加入 expression、statement、control flow、function、closure、class 与 inheritance;随后重建底层路线,使用 bytecode 和 VM 实现同一门 Lox。

参考资料

版权说明:官方页面标注 © 2015–2021。原作版权归 Robert Nystrom 及相关权利人所有。本笔记为个人学习用途的中文翻译。

← 返回列表
← 上一篇 Introduction:为什么要实现 programming language 下一篇 → Crafting Interpreters:The Lox Language

评论 (0)

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