Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: [日] 青木峰郎 著, 严圣逸, 绝云 译

Rating No ratings yet

本书将带领读者从头开始制作一门语言的编译器。笔者特意为本书设计了CЬ语言,CЬ可以说是C语言的子集,实现了包括指针运算等在内的C语言的主要部分。本书所实现的编译器就是CЬ语言的编译器, 是实实在在的编译器,而非有诸多限制的玩具。另外,除编译器之外,本书对以编译器为中心的编程语言的运行环境,即编译器、汇编器、链接器、硬件、运行时环境等都有所提及,介绍了程序运行的所有环节。

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 This book is a hands-on guide to building a real compiler for C♭, a C-like language, on x86 Linux, covering everything from parsing to code generation and linking. It’s for developers who want to understand how compilers and runtime environments actually work by building one from scratch. 【Book Arc】 - **Opening (~0%–11%)**: Introduces the book’s philosophy—building a practical compiler rather than a toy—and outlines the full toolchain (compiler, assembler, linker, hardware, runtime). It sets expectations for the reader’s prerequisites (Java, C, Linux basics) and explains why the author chose x86 and Linux for concreteness. - **Early (~11%–25%)**: Covers the C♭ language and the structure of the cbc compiler, then dives into lexical analysis using JavaCC. You learn how to define tokens, handle whitespace and comments, and scan structured words like strings and numbers. - **Early (~25%–36%)**: Moves to parser construction with JavaCC, using EBNF notation to describe grammar. It addresses ambiguity, token lookahead, and how to structure rules for statements, expressions, and terms. - **Middle (~36%–50%)**: Transitions to abstract syntax trees (ASTs) and intermediate code. You see how JavaCC actions build ASTs for declarations, statements, and expressions, then start semantic analysis—specifically resolving variable references using a scope tree and Visitor pattern. - **Late (~50%–end)**: Continues semantic analysis (type checking, etc.), then moves to code generation, assembly, linking, and runtime considerations. The book concludes with producing executable ELF files and understanding the full program execution pipeline. 【Key Takeaways】 - **Build a real compiler, not a toy** (Opening): The book’s core value is implementing a working compiler for C♭, a C subset with pointers, that generates executable ELF files on x86 Linux. This gives you practical insight into how C programs are compiled and run. - **Concrete platforms beat abstract theory** (Early): By fixing the environment (x86, Linux, JavaCC), the author makes the entire toolchain explainable end-to-end. This approach is more intuitive than generic discussions and lets you trace every step from source to binary. - **JavaCC is the workhorse for lexing and parsing** (Early): You learn to describe tokens and grammar using JavaCC’s regular expressions and EBNF notation. This includes handling ambiguity, longest-match rules, and state-based scanning for structured tokens like comments and strings. - **ASTs are the bridge between parsing and semantics** (Middle): JavaCC actions build abstract syntax trees for declarations, statements, and expressions. Understanding AST structure is essential for the next phase—semantic analysis—where you resolve references and check types. - **Semantic analysis uses Visitor patterns and scope trees** (Middle): The book shows how to traverse ASTs using Visitor patterns and manage variable scopes with a tree structure. This is where you resolve variable references and prepare for code generation. - **The full pipeline includes assembler, linker, and runtime** (Late): Beyond the compiler, the book covers how generated code is assembled, linked, and loaded on Linux. This holistic view is rare in compiler books and helps you understand program execution from start to finish. - **Open-source tools are leveraged, not reinvented** (Throughout): The author uses GCC, GNU Assembler, JavaCC, and Linux, showing that “self-made” doesn’t mean writing every line yourself. This teaches you to build on existing tools while understanding their roles. 【Reading Tips】 - **Skim the early chapters on JavaCC basics** if you’re already familiar with lexers and parsers; focus instead on how C♭’s grammar is structured and where the author makes design choices. - **Deep-read the semantic analysis and code generation sections**—these are the most complex and where the book’s unique value lies. Pay attention to how the scope tree and Visitor pattern are implemented. - **Follow along with the code** by setting up the cbc compiler on your own Linux machine. The book is practical, so running examples and modifying them will solidify your understanding. - **Brush up on Java 5 features** (generics, foreach) and basic C pointers before starting, as the book assumes this knowledge and doesn’t explain them in detail. - **Use the book as a reference for the full toolchain**—if you get stuck on linking or ELF format, revisit the later chapters rather than jumping to external resources. 【Coverage Limits】 This guide covers the book’s structure and key concepts through the semantic analysis stage (~50% of the book). The later chapters on code generation, assembly, and runtime are summarized based on the book’s stated scope but not detailed from the excerpts.
Page 4
看到自制的成果,又能向优秀的开源软件学习。如果要深入学习、研究,那么开源软 件的源代码以及活跃的社区等都是非常有帮助的。而如果把自制的软件也作为开源软件上传到 Github 上供大家使用,并根据其他开发者提出的 Pull Request 不断改进软件,那就更好了。 最后我要由衷地感谢本书的另一位译者绝云老师以及图...
View in text
Page 10
............................................................ 21 第 1部分 代码分析 第3章 语法分析的概要 24 3.1  语法分析的方法 .........................................................
View in text
Page 9
.......... 62 提取左侧共通部分●............................................................................................................. 63 token 的超前扫描●............
View in text
Page 14
...................................... 121 if 语句的抽象语法树●.........................................................................................................
View in text
Page 9
............ 172 TyperChecker 类的启动●.................................................................................................. 173 二元运算符的类型检查●...........
View in text
Page 17
................................................................................ 231 无符号整数的表现形式●................................................................
View in text
Page 9
....... 272 14.3  Linux/x86 下函数调用的细节 ...................................................................................... 274 寄存器的保存和复原●......................
View in text
Page 9
.... 310 函数体编译顺序●............................................................................................................... 310
View in text
Tags
AI categories
ProgrammingcompilerLinux
ISBN: 7115422184
Publish Year: 2016
Language: Chinese
Pages: 445
File Format: PDF
File Size: 12.7 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…