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

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


当前回答

我也喜欢Crenshaw的教程,因为它非常清楚地说明了编译器只是另一个程序,它读取一些输入并写出一些输出。

阅读它。

如果您愿意,可以使用它,但随后查看另一篇关于如何编写更大、更完整的编译器的参考文献。

阅读《关于信任的信任》,了解一下在这个领域可以做的不明显的事情。

其他回答

如果你想使用功能强大的高级工具,而不是自己构建一切,那么阅读本课程的项目和阅读材料是一个很好的选择。这是一门语言课程,由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非常擅长从中生成优化的程序集。

Donald Knuth的《计算机编程的艺术》最初是作为编译器编写的教程而写的,这让我很惊讶。

当然,Knuth博士对主题深入研究的倾向导致编译器写作教程被扩展到大约9卷,其中只有3卷真正出版了。这是一个关于编程主题的相当完整的阐述,涵盖了关于编写编译器需要了解的所有细节。

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.

“让我们构建一个编译器”是很棒的,但它有点过时了。(我并不是说这让它变得不那么有效。)

或者看看俚语。这类似于“让我们构建一个编译器”,但对于初学者来说是一个更好的资源。这是一个pdf教程,需要7步的方法来教你一个编译器。添加quora链接,因为它有各种各样的俚语端口的链接,在c++, Java和JS中,也有python和Java中的解释器,最初是用c#和。net平台编写的。

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.