跳转到内容

《编译原理》中与常量、字面量相关的引用

本文梳理了《编译原理》这本书中对于“常量”(Constant)和“字面量”(Literal)术语的所有使用情况。

《编译原理》的英文第一版名为Principles of Compiler Design(称为“绿龙书”),作者为Alfred V. Aho和Jeffrey D. Ullman,出版于1977年。在这一年,C语言已经诞生了5年,但是C++语言尚未诞生,后者发布于1985年。封面如下图所示:

Image

1986年,《编译原理》的英文第二版名为Compilers: Principles, Techniques and Tools(称为“红龙书”),作者加上了Ravi Sethi。封面如下图所示:

Image

2006年,《编译原理》的英文第三版名为Compilers: Principles, Techniques and Tools Second Edition(称为“紫龙书”),作者加上了Monica S. Lam。封面如下图所示:

Image

所以,尽管目前最新的紫龙书的标题中包含“Second Edition”字样,但它是基于红龙书相同标题的,实际上为第三版,称为“龙书3”。

国内机械工业出版社引进、翻译了《龙书3》,由南京大学赵建华、郑滔、戴新宇翻译,首版为2009年1月,第2版为2024年11月,ISBN 978-7-111-25121-7。封面如下图所示:

Image

由于国内大多数技术书籍的翻译水平有限,所以本文将基于《龙书3》的英文原版进行梳理,但也会贴上中文译文作为参考。

例如,在英文原版的§2.6.5 A Lexical Analyzer中,P.82(第82页),“In effect, these fields represent constants.”被翻译为“事实上,这些常量就代表常量”(中文版P.51)——这是极为错误的翻译。术语“field”应该翻译为“字段”,或者虽然古老、但也不是不能用的“域”。正确的译法为:“实际上,这些字段代表常量”。一个字不多,一个字不少。英文原文没有“just”等,那就不要用中文的“就”。

# 英文原版位置 英文原版内容 中文译版位置 中文译版内容
1 TOC, P.X, §2.6.3 Constants 目录,P.IX 常量
2 TOC, P.XIX, §9.4 Constant Propagation 目录,P.XIII 常量传播
3 TOC, P.XIX, §9.4.1 Data-Flow Values for the Constant-Propagation Framework 目录,P.XIII 常量传播框架的数据流值
4 TOC, P.XIX, §9.4.2 The Meet for the Constant-Propagation Framework 目录,P.XIII 常量传播框架的交汇运算
5 TOC, P.XIX, §9.4.3 Transfer Functions for the Constant-Propagation Framework 目录,P.XIII 常量传播框架的传递函数
6 TOC, P.XIX, §9.4.4 Monotonicity of the Constant-Propagation Framework 目录,P.XIII 常量传递框架的单调性
7 TOC, P.XIX, §9.4.5 Nondistributivity of the Constant-Propagation Framework 目录,P.XIII 常量传播框架的不可分配性
8 TOC, P.XXI, §11.3.4 Incorporating Symbolic Constants 目录,P.XV 混合使用符号常量
9 TOC, P.XXII, §11.8.1 A Constant Number of Synchronizations 目录,P.XV 固定多个同步运算
10 §1.2.6, P.11 then multiplies it with floating-point constant 60.0. The # signifies that 60.0 is to be treated as an immediate constant. P.6 然后将其与浮点常数60.0相乘。井号“#”表示60.0应该作为一个立即数处理。
11 §1.6.2, P.27 Declared constants are an exception. P.16 被声明的常量是一个例外。
12 §2.3.1, P.53 If E is a variable or constant, then the postfix notation for E is E itself. P.33 如果E是一个变量或常量,则E的后缀表示是E本身。
13 §2.3.1, P.53 That is, the translations of 9, 5, and 2 are the constants themselves, by rule (1). P.33 也就是说,由规则1可知,9、5和2的翻译结果就是这些常量本身。
14 §2.6.3, P.78 Anytime a single digit appears in a grammar for expressions, it seems reasonable to allow an arbitrary integer constant in its place. Integer constants can be allowed either by creating a terminal symbol, say num, for such constants or by incorporating the syntax of integer constants into the grammar. P.49 在一个表达式的文法中,任何允许出现数位的地方都应该允许出现任意的整型常量。要使得表达式中可以出现整数常量,我们可以创建一个代表整型常量的终结符号,比如num,也可以将整数常量的语法加入到文法中。
15 §2.6.5, P.82 Where the pseudocode had terminals like num and id, the Java code uses integer constants. Class Tag implements such constants: P.51 在伪代码中使用诸如num、id这样的终结符号的地方,Java代码中使用整型常量表示。类Tag实现了这些常量
16 §2.6.5, P.82 In effect, these fields represent constants. A similar effect is achieved in C by using define-statements to allow names such as NUM to be used as symbolic constants, e.g.: P.51 事实上,这些常量就代表常量。在C语言中,可以使用define语句来获得类似的效果。这些define语句使得NUM这样的名字可以被当做符号常量使用,例如:
17 §2.6.5, P.82 The only requirement is that Tag.NUM and Tag.ID must be initialized with distinct values that differ from each other and from the constants representing single-character tokens, such as ‘+’ or ‘*’. P.51 唯一的要求是Tag.NUMTag.ID必须被初始化为互不相同的值,且这些初始化值还必须不同于那些代表单字符词法单元(比如“+”或“*”)的常量
18 §2.7.1, P.87 Such a hash table supports essentially constant-time lookups, at the expense of inserting and deleting entries on block entry and exit. P.54 这样的散列表实际上支持常量时间的查询,但是在进入和离开块时需要插入和删除相应的条目。
19 §2.8.3, P.98 But a constant like 2 is not appropriate on the left side of an assignment, since it has an r-value, but not an l-value. P.61 但2这样的常量不可以出现在一个赋值表达式的左部,因为它有一个右值,但不是左值。
20 §2.8.4, P.99 where x, y, and z are names, constants, or compiler-generated temporaries; P.62 其中x、y和z可以是名字、常量或由编译器生成的临时量;
21 §2.8.4, P.101 We now illustrate the translation of expressions by considering expressions containing binary operators op, array accesses, and assignments, in addition to constants and identifiers. P.63 我们将考虑包含二目运算符op、数组访问和赋值运算,并包含常量及标识符的表达式,以此来说明对表达式的翻译。
22 §2.8.4, P.101 No code is generated for identifiers and constants, since they can appear as addresses in instructions. P.63 不需要为标识符和常量生成任何代码,因为它们可以作为地址出现在指令中。
23 §2.8.4, P.103 if (x is an Id or a *Constant* node) return x; P.65 if (x是一个Id或者*Constant*结点) return x;
24 §2.8.4, P.103 When x represents an identifier or a constant, rvalue returns x itself. P.64 当x代表一个标识符或常量时,rvalue返回x本身。
25 §2.8.4, P.104 The constant 2 generates no three-address code, and its r-value is returned as a Constant node with value 2. P.65 常量2没有生成三地址代码,rvalue返回它的右值,即一个值为2的Constant结点。
26 §2.8.4, P.104 then the right side must be a name, a constant, or a temporary, P.66 那么其右部必然是一个名字、常量或临时变量,
27 §3.1.2, P.112, Figure 3.2 any numeric constant P.69, 图3-2 任何数字常量
28 §3.1.2, P.112 4. One or more tokens representing constants, such as numbers and literal strings. P.70 4)一个或多个表示常量的词法单元,比如数字和字面值字符串。
29 §3.1.3, P.113 In practice, a typical compiler would instead store a character string representing the constant and use as an attribute value for number a pointer to that string. P.71 在实践中,编译器将保存一个代表该常量的字符串,并将一个指向该字符串的指针作为number的属性值。
30 §3.3.6, P.125 (ii) the lexical form of numerical constants, P.78 (ii)各语言的数字常量的词法形式是什么?
31 §3.4 Note that for the six relational operators, symbolic constants LT, LE, and so on are used as the attribute value, in order to indicate which instance of the token relop we have found P.81 请注意,对于其中的6个关系运算符,符号常量LT、LE等被当作属性值返回,其目的是指明我们发现的是词法单元relop的哪个实例。

术语“字面量”(Literal)的使用

Section titled “术语“字面量”(Literal)的使用”