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

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


当前回答

资源清单:

A Nanopass Framework for Compiler Education ¶ Advanced Compiler Design and Implementation $ An Incremental Approach to Compiler Construction ¶ ANTLR 3.x Video Tutorial Basics of Compiler Design Building a Parrot Compiler Compiler Basics Compiler Construction $ Compiler Design and Construction $ Crafting a Compiler with C $ Crafting Interpreters [Compiler Design in C] 12 ¶ Compilers: Principles, Techniques, and Tools $ — aka "The Dragon Book"; widely considered "the book" for compiler writing. Engineering a Compiler $ Essentials of Programming Languages Flipcode Article Archive (look for "Implementing A Scripting Engine by Jan Niestadt") Game Scripting Mastery $ How to build a virtual machine from scratch in C# ¶ Implementing Functional Languages Implementing Programming Languages (with BNFC) Implementing Programming Languages using C# 4.0 Interpreter pattern (described in Design Patterns $) specifies a way to evaluate sentences in a language Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages $ Let's Build a Compiler by Jack Crenshaw — The PDF ¶ version (examples are in Pascal, but the information is generally applicable) Linkers and Loaders $ (Google Books) Lisp in Small Pieces (LiSP) $ LLVM Tutorial Modern Compiler Implementation in ML $ — There is a Java $ and C $ version as well - widely considered a very good book Object-Oriented Compiler Construction $ Parsing Techniques - A Practical Guide Project Oberon ¶ - Look at chapter 13 Programming a Personal Computer $ Programing Languages: Application and Interpretation Rabbit: A Compiler for Scheme¶ Reflections on Trusting Trust — A quick guide Roll Your Own Compiler for the .NET framework — A quick tutorial from MSDN Structure and Interpretation of Computer Programs Types and Programming Languages Want to Write a Compiler? - a quick guide Writing a Compiler in Ruby Bottom Up Compiling a Lisp — compile directly to x86-64

传说:

链接到PDF文件 $印刷书籍的链接

其他回答

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

阅读它。

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

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

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.

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

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

一般来说,编译器没有五分钟的教程,因为这是一个复杂的主题,编写一个编译器可能需要几个月的时间。你得自己去找。

Python和Ruby通常是解释型的。也许你也想从一个口译员开始。这通常比较简单。

The first step is to write a formal language description, the grammar of your programming language. Then you have to transform the source code that you want to compile or interpret according to the grammar into an abstract syntax tree, an internal form of the source code that the computer understands and can operate on. This step is usually called parsing and the software that parses the source code is called a parser. Often the parser is generated by a parser generator which transform a formal grammar into source oder machine code. For a good, non-mathematical explanation of parsing I recommend Parsing Techniques - A Practical Guide. Wikipedia has a comparison of parser generators from which you can choose that one that is suitable for you. Depending on the parser generator you chose, you will find tutorials on the Internet and for really popular parser generators (like GNU bison) there are also books.

为您的语言编写解析器可能非常困难,但这取决于您的语法。所以我建议保持你的语法简单(不像c++);LISP就是一个很好的例子。

在第二步中,抽象语法树从树形结构转换为线性中间表示。作为一个很好的例子,Lua的字节码经常被引用。但是中间表示实际上取决于你的语言。

如果您正在构建一个解释器,则只需解释中间表示即可。您还可以及时编译它。我推荐LLVM和libjit进行即时编译。为了使语言可用,你还必须包含一些输入和输出函数,也许还有一个小的标准库。

如果您要编译该语言,它将更加复杂。你必须为不同的计算机架构编写后端,并从这些后端的中间表示生成机器代码。对于这个任务,我推荐LLVM。

有一些关于这个主题的书,但我不能推荐他们一般使用。他们中的大多数要么太学术,要么太实际。没有“21天自学编译器写作”,因此,你必须买几本书才能很好地理解整个主题。如果你上网搜索,你会发现一些在线书籍和课堂笔记。也许你附近有一个大学图书馆,在那里你可以借到关于编译器的书籍。

我还建议你在理论计算机科学和图论方面有良好的背景知识,如果你想让你的项目认真起来的话。计算机科学学位也会很有帮助。