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

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


当前回答

如果你想了解更多关于编译器(和元编译器)的知识,这不是一本书,而是一篇技术论文和一个非常有趣的学习经历……本网站将引导您构建一个完全自包含的编译器系统,可以编译自己和其他语言:

教程:metaccompilers第1部分

这一切都是基于一份惊人的10页技术论文:

META II:一种面向语法的编译器编写语言

从1964年开始。我从1970年开始学习如何构建编译器。当您最终明白编译器如何自我再生....时,会有一个令人兴奋的时刻

我从大学时代就认识这个网站的作者,但我和这个网站没有任何关系。

其他回答

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

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

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

创建编译器的一个简单方法是使用bison和flex(或类似),构建一个树(AST)并用C生成代码,生成C代码是最重要的步骤。通过生成C代码,您的语言将自动在所有具有C编译器的平台上运行。

生成C代码就像生成HTML一样简单(只需使用打印或等效工具),这又比编写C解析器或HTML解析器容易得多。

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

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

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

将感激反馈这些。