首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
当前回答
“…让我们构建一个编译器……”
我认为@sasb是http://compilers.iecc.com/crenshaw/的第二名。暂时忘掉买更多的书吧。
为什么?工具和语言。
所需的语言是Pascal,如果我没记错的话,是基于Turbo-Pascal的。如果你去http://www.freepascal.org/并下载Pascal编译器,所有的例子都可以直接从页面上运行~ http://www.freepascal.org/download.var Free Pascal的好处是,你可以使用它几乎任何你喜欢的处理器或操作系统。
一旦你掌握了课程,然后尝试更高级的“龙书”~ http://en.wikipedia.org/wiki/Dragon_book
其他回答
如果你对为函数式语言(而不是过程式语言)编写编译器感兴趣,Simon Peyton-Jones和David Lester的《实现函数式语言:教程》是一个很好的指南。
函数式计算如何工作的基本概念是通过简单但功能强大的函数式语言“Core”中的示例来指导的。此外,Core语言编译器的每个部分都用Miranda(一种与Haskell非常相似的纯函数式语言)中的代码示例进行了解释。
书中描述了几种不同类型的编译器,但即使你只遵循Core的模板编译器,你也会对函数式编程有一个很好的理解。
龙之书绝对是关于“构建编译器”的书,但是如果您的语言不像当前这一代语言那么复杂,那么您可能想看看设计模式中的解释器模式。
书中的示例设计了一种类似正则表达式的语言,并且经过了深思熟虑,但正如书中所述,它有助于思考整个过程,但实际上只在小型语言上有效。然而,用这种模式为一种小型语言编写解释器要快得多,而不必学习所有不同类型的解析器,yacc和lex等等……
我同意龙书的参考;IMO,它是编译器构造的权威指南。准备好接受一些核心理论吧。
If you want a book that is lighter on theory, Game Scripting Mastery might be a better book for you. If you are a total newbie at compiler theory, it provides a gentler introduction. It doesn't cover more practical parsing methods (opting for non-predictive recursive descent without discussing LL or LR parsing), and as I recall, it doesn't even discuss any sort of optimization theory. Plus, instead of compiling to machine code, it compiles to a bytecode that is supposed to run on a VM that you also write.
这仍然是一本不错的读物,尤其是如果你能在亚马逊上以便宜的价格买到的话。如果你只想简单介绍编译器,《Game Scripting Mastery》是个不错的选择。如果你想先玩硬核游戏,那么你应该选择《龙之书》。
I think Modern Compiler Implementation in ML is the best introductory compiler writing text. There's a Java version and a C version too, either of which might be more accessible given your languages background. The book packs a lot of useful basic material (scanning and parsing, semantic analysis, activation records, instruction selection, RISC and x86 native code generation) and various "advanced" topics (compiling OO and functional languages, polymorphism, garbage collection, optimization and single static assignment form) into relatively little space (~500 pages).
我更喜欢《现代编译器实现》而不是《Dragon》,因为《现代编译器实现》对该领域的调查较少——相反,它确实涵盖了编写一个严肃、体面的编译器所需的所有主题。在你读完这本书之后,如果你需要的话,你就可以直接深入研究论文了。
I must confess I have a serious soft spot for Niklaus Wirth's Compiler Construction. It is available online as a PDF. I find Wirth's programming aesthetic simply beautiful, however some people find his style too minimal (for example Wirth favors recursive descent parsers, but most CS courses focus on parser generator tools; Wirth's language designs are fairly conservative.) Compiler Construction is a very succinct distillation of Wirth's basic ideas, so whether you like his style or not or not, I highly recommend reading this book.
如果你想使用功能强大的高级工具,而不是自己构建一切,那么阅读本课程的项目和阅读材料是一个很好的选择。这是一门语言课程,由Java解析器引擎ANTLR的作者编写。你可以从Pragmatic Programmers网站上获得这门课程的PDF版本。
The course goes over the standard compiler compiler stuff that you'd see elsewhere: parsing, types and type checking, polymorphism, symbol tables, and code generation. Pretty much the only thing that isn't covered is optimizations. The final project is a program that compiles a subset of C. Because you use tools like ANTLR and LLVM, it's feasible to write the entire compiler in a single day (I have an existence proof of this, though I do mean ~24 hours). It's heavy on practical engineering using modern tools, a bit lighter on theory.
顺便说一下,LLVM非常棒。在许多情况下,你可能会编译到汇编,你最好编译到LLVM的中间表示。它是更高级别的、跨平台的,LLVM非常擅长从中生成优化的程序集。