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

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


当前回答

“…让我们构建一个编译器……”

我认为@sasb是http://compilers.iecc.com/crenshaw/的第二名。暂时忘掉买更多的书吧。

为什么?工具和语言。

所需的语言是Pascal,如果我没记错的话,是基于Turbo-Pascal的。如果你去http://www.freepascal.org/并下载Pascal编译器,所有的例子都可以直接从页面上运行~ http://www.freepascal.org/download.var Free Pascal的好处是,你可以使用它几乎任何你喜欢的处理器或操作系统。

一旦你掌握了课程,然后尝试更高级的“龙书”~ http://en.wikipedia.org/wiki/Dragon_book

其他回答

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

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

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

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

资源清单:

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

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.

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

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