Differences between revisions 4 and 13 (spanning 9 versions)
Revision 4 as of 2021-02-01 17:52:01
Size: 1399
Editor: zbjxb
Comment:
Revision 13 as of 2021-02-18 02:59:19
Size: 2147
Editor: zbjxb
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
 o An introductory chapter describing what a compiler is.  * An introductory chapter describing what a compiler is.
     * A chapter or two on syntax equations, using Backus-Naur Form (BNF).
Line 4: Line 6:
 o A chapter or two on syntax equations, using Backus-Naur Form
   (BNF).

 o
A chapter or two on lexical scanning, with emphasis on
 * A chapter or two on lexical scanning, with emphasis on
Line 10: Line 9:
 o Several chapters on parsing theory, beginning with top-down  * Several chapters on parsing theory, beginning with top-down
Line 13: Line 12:
 o A chapter on intermediate languages, with emphasis on P-code  * A chapter on intermediate languages, with emphasis on P-code
Line 16: Line 15:
 o Many chapters on alternative ways to handle subroutines and  * Many chapters on alternative ways to handle subroutines and
Line 19: Line 18:
 o A chapter toward the end on code generation, usually for some  * A chapter toward the end on code generation, usually for some
Line 23: Line 22:
 o A final chapter or two on optimization. This chapter often  * A final chapter or two on optimization. This chapter often
Line 28: Line 27:
  * 所有的Identifier用single character来代替,不考虑空格(即词法规定不允许用户写空格)   * 所有的Identifier用single character来代替,不考虑空格(即词法规定不允许用户写空格)。作者认为如果他能正确处理I-T-L,那么他也能让IF-THEN-ELSE工作
  * 只处理实时I/O,不处理File I/O。
  * 忽略error correction/recovery。遇到第一个error即停机,但这不是崩溃。
Line 31: Line 32:
 1. 从源代码直接生成汇编代码。不需要中间码
 1. 延后对module的构建,直至真的需要它的时候
  * 把每一个将来可能会用上的功能都提前封装是一个疯狂的做法,几乎肯定是错误的
  * 尽量使用现有的module,除非你真的需要一个更强大的一点的,即便如此,也仅仅把注意力集中到该module上
 1. 不纠缠于P-code等中间码或者想象的CPUs等,从第一天开始做出来的东西就要是可以工作的,即便它不完善

申明不会采用典型教科书上关于编译器的教学顺序,比如:

  • An introductory chapter describing what a compiler is.
  • A chapter or two on syntax equations, using Backus-Naur Form (BNF).
  • A chapter or two on lexical scanning, with emphasis on
    • deterministic and non-deterministic finite automata.
  • Several chapters on parsing theory, beginning with top-down
    • recursive descent, and ending with LALR parsers.
  • A chapter on intermediate languages, with emphasis on P-code
    • and similar reverse polish representations.
  • Many chapters on alternative ways to handle subroutines and
    • parameter passing, type declarations, and such.
  • A chapter toward the end on code generation, usually for some
    • imaginary CPU with a simple instruction set. Most readers (and in fact, most college classes) never make it this far.
  • A final chapter or two on optimization. This chapter often
    • goes unread, too.

作者不会遵照这些,而是带着你直接上手写一个能工作的编译器,采用他的方式:

  1. KISS原则
    • 所有的Identifier用single character来代替,不考虑空格(即词法规定不允许用户写空格)。作者认为如果他能正确处理I-T-L,那么他也能让IF-THEN-ELSE工作
    • 只处理实时I/O,不处理File I/O。
    • 忽略error correction/recovery。遇到第一个error即停机,但这不是崩溃。
  2. 只讨论一种情形的解析:top-down、recursive descent parsing
    • 自顶向下、递归下降的情形,作者认为这是唯一一种支持手写编译器的技术选型
  3. 从源代码直接生成汇编代码。不需要中间码
  4. 延后对module的构建,直至真的需要它的时候
    • 把每一个将来可能会用上的功能都提前封装是一个疯狂的做法,几乎肯定是错误的
    • 尽量使用现有的module,除非你真的需要一个更强大的一点的,即便如此,也仅仅把注意力集中到该module上
  5. 不纠缠于P-code等中间码或者想象的CPUs等,从第一天开始做出来的东西就要是可以工作的,即便它不完善

compiler/Let's build a compiler/ch1 (last edited 2021-02-18 02:59:19 by zbjxb)