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

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


当前回答

Not included in the list so far is this book: Basics of Compiler Design (Torben Mogensen) (from the dept. of Computer Science, University of Copenhagen) I'm also interested in learning about compilers and plan to enter that industry in the next couple of years. This book is the ideal theory book to begin learning compilers as far as I can see. It's FREE to copy and reproduce, cleanly and carefully written and gives it to you in plain English without any code but still presents the mechanics by way of instructions and diagrams etc. Worth a look imo.

其他回答

As an starting point, it will be good to create a recursive descent parser (RDP) (let's say you want to create your own flavour of BASIC and build a BASIC interpreter) to understand how to write a compiler. I found the best information in Herbert Schild's C Power Users, chapter 7. This chapter refers to another book of H. Schildt "C The complete Reference" where he explains how to create a calculator (a simple expression parser). I found both books on eBay very cheap. You can check the code for the book if you go to www.osborne.com or check in www.HerbSchildt.com I found the same code but for C# in his latest book

The Dragon Book is too complicated. So ignore it as a starting point. It is good and makes you think a lot once you already have a starting point, but for starters, perhaps you should simply try to write an math/logical expression evaluator using RD, LL or LR parsing techniques with everything (lexing/parsing) written by hand in perhaps C/Java. This is interesting in itself and gives you an idea of the problems involved in a compiler. Then you can jump in to your own DSL using some scripting language (since processing text is usually easier in these) and like someone said, generate code in either the scripting language itself or C. You should probably use flex/bison/antlr etc to do the lexing/parsing if you are going to do it in c/java.

这里有很多很好的答案,所以我想在列表中再添加一个:

十多年前,我有一本叫做Project Oberon的书,里面有一些关于编译器的非常好的文字。这本书的真正突出之处在于,它的来源和解释都是非常实用和易读的。全文(2005年版)已以pdf格式提供,因此您可以立即下载。编译器将在第12章讨论:

http://www.ethoberon.ethz.ch/WirthPubl/ProjectOberon.pdf

尼克劳斯·沃斯,于尔格·古特克内西

(这本书的内容不像他关于编译器的书那么广泛)

我读过几本关于编译器的书,我可以第二龙书,花时间在这本书上是非常值得的。

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

如果您不仅仅是在寻找书籍,而且还对有关于该主题的文章的网站感兴趣,我已经在博客中讨论了创建编程语言的各个方面。大部分文章都可以在我博客的“语言设计”类别中找到。

特别是,我介绍了手动生成Intel机器代码、自动生成机器码或字节码、创建字节码解释器、编写面向对象的运行时、创建简单的加载器以及编写简单的标记/清除垃圾收集器。所有这些都是非常实用和务实的方式,而不是用大量的理论让你感到无聊。

将感激反馈这些。