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

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


当前回答

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

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天自学编译器写作”,因此,你必须买几本书才能很好地理解整个主题。如果你上网搜索,你会发现一些在线书籍和课堂笔记。也许你附近有一个大学图书馆,在那里你可以借到关于编译器的书籍。

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

其他回答

如果你像我一样,没有受过正规的计算机科学教育,并且对 构建/想知道编译器是如何工作的:

我推荐“Java编程语言处理器:编译器和解释器”, 对于自学成才的计算机程序员来说,这是一本令人惊叹的书。

在我看来,理解那些基本的语言理论、自动化机器和集合理论并不是什么大问题。问题是如何把这些东西转换成代码。上面的书告诉您如何编写解析器、分析上下文和生成代码。如果你不能理解这本书,那么我不得不说,放弃构建编译器吧。这本书是我读过的最好的编程书。

还有一本书,也很好,《c语言编译器设计》,里面有很多代码,告诉你如何构建编译器和词法分析器工具。

构建编译器是一种有趣的编程实践,可以教会您许多编程技能。

不要买龙的书。这是浪费金钱和时间,不适合从业者。

资源清单:

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文件 $印刷书籍的链接

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.

Python附带了一个用Python编写的Python编译器。您可以看到源代码,它包括从解析、抽象语法树、发出代码等所有阶段。 破解它。

来自comp.compiler常见问题解答:

《个人电脑编程》作者:Per Brinch Hansen Prentice-Hall 1982 ISBN 0-13-730283-5

这本书名很不幸的书 解释单用户编程环境的设计和创建 用于微电脑,使用一种类似pascal的语言,叫做Edison。作者介绍 一个的逐步实现的所有源代码和解释 爱迪生编译器和简单的支持操作系统,全部写在 爱迪生本身(除了一个小的支持内核写在符号 PDP 11/23汇编程序;完整的源代码也可以为IBM订购 个人电脑)。

这本书最有趣的地方是:1)它的能力 演示如何创建一个完整的,独立的,自我维护的, 有用的编译器和操作系统,以及2)有趣的讨论 语言设计和规范的问题和权衡。

“Brinch Hansen on Pascal Compilers” by Per Brinch Hansen 普伦蒂斯-霍尔 1985 ISBN 0-13-083098-4

Another light-on-theory heavy-on-pragmatics here's-how-to-code-it book. The author presents the design, implementation, and complete source code for a compiler and p-code interpreter for Pascal- (Pascal "minus"), a Pascal subset with boolean and integer types (but no characters, reals, subranged or enumerated types), constant and variable definitions and array and record types (but no packed, variant, set, pointer, nameless, renamed, or file types), expressions, assignment statements, nested procedure definitions with value and variable parameters, if statements, while statements, and begin-end blocks (but no function definitions, procedural parameters, goto statements and labels, case statements, repeat statements, for statements, and with statements).

编译器和解释器都是用Pascal* (Pascal " * ")编写的 Pascal子集扩展了一些爱迪生风格的创建功能 软件开发系统。用于IBM个人电脑的Pascal编译器由 但是很容易将这本书的Pascal编译器移植到任何作者 方便的Pascal平台。

本书使编译器的设计和实现看起来很简单。我 尤其是作者对质量的关注, 可靠性和测试。编译器和解释器可以很容易地使用 作为一个更复杂的语言或编译器项目的基础,特别是 如果你被要求快速启动并运行一些东西。