首选语言:C/ c++、Java、Ruby。

我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。


当前回答

我同意龙书的参考;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》是个不错的选择。如果你想先玩硬核游戏,那么你应该选择《龙之书》。

其他回答

还有一本书还没有推荐,但非常重要,那就是约翰·莱文的《链接器和加载器》。如果您不使用外部汇编程序,则需要一种方法来输出可以链接到最终程序的目标文件。即使您正在使用外部汇编程序,也可能需要了解重定位以及整个程序加载过程如何工作,以制作一个工作工具。这本书收集了很多关于各种系统的这个过程的随机知识,包括Win32和Linux。

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.

列表中缺少:Jones和Lins的《垃圾收集:自动动态内存管理的算法》。

(假设您正在编写编译器和运行时系统,并且正在实现一种垃圾收集语言。

我发现Dragon的书太难读了,因为它太专注于语言理论,而实际上编写编译器并不需要这些理论。

我将添加Oberon书籍,其中包含了一个惊人的快速和简单的Oberon编译器项目Oberon的完整源代码。

我正在研究同样的概念,发现了Joel Pobar写的一篇很有前景的文章,

为.NET框架创建一个语言编译器-不知道它去了哪里

为.NET框架创建一个语言编译器-原始文档的pdf副本

他讨论了编译器的高级概念,并为. net框架发明了自己的语言。尽管它的目标是。net框架,但其中的许多概念应该能够被复制。文章内容包括:

语言的定义 扫描仪 解析器(位我主要感兴趣) 针对。net框架的 代码生成器

还有其他的话题,但你得到的只是。

它面向初学者,用c#(不完全是Java)编写。

HTH

骨头