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

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


当前回答

看看下面这本书。作者是ANTLR的创建者。

语言实现模式:创建您自己的领域特定语言和通用编程语言。

其他回答

每当我想尝试一种新的语言想法时,我就写一个简单的解析器,让它生成一些容易获得良好编译器的语言,比如C。

你认为c++是如何完成的?

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.

如果你对为函数式语言(而不是过程式语言)编写编译器感兴趣,Simon Peyton-Jones和David Lester的《实现函数式语言:教程》是一个很好的指南。

函数式计算如何工作的基本概念是通过简单但功能强大的函数式语言“Core”中的示例来指导的。此外,Core语言编译器的每个部分都用Miranda(一种与Haskell非常相似的纯函数式语言)中的代码示例进行了解释。

书中描述了几种不同类型的编译器,但即使你只遵循Core的模板编译器,你也会对函数式编程有一个很好的理解。

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.

这是一个庞大的课题。不要低估这一点。不要低估我的观点。 我听说龙之书是一个(?)开始的地方,连同搜索。(收集学会寻找,最终它将成为你的人生。 构建自己的编程语言绝对是一个很好的练习!但你要知道,它最终永远不会被用于任何实际目的。这种情况很少有例外。