首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
当前回答
Donald Knuth的《计算机编程的艺术》最初是作为编译器编写的教程而写的,这让我很惊讶。
当然,Knuth博士对主题深入研究的倾向导致编译器写作教程被扩展到大约9卷,其中只有3卷真正出版了。这是一个关于编程主题的相当完整的阐述,涵盖了关于编写编译器需要了解的所有细节。
其他回答
如果您愿意使用LLVM,请查看这个:http://llvm.org/docs/tutorial/。它教你如何使用LLVM的框架从头编写编译器,并且不假设你有任何关于这个主题的知识。
本教程建议您编写自己的解析器和lexer等,但我建议您了解bison和flex后再进行研究。它们让生活变得轻松多了。
龙之书绝对是关于“构建编译器”的书,但是如果您的语言不像当前这一代语言那么复杂,那么您可能想看看设计模式中的解释器模式。
书中的示例设计了一种类似正则表达式的语言,并且经过了深思熟虑,但正如书中所述,它有助于思考整个过程,但实际上只在小型语言上有效。然而,用这种模式为一种小型语言编写解释器要快得多,而不必学习所有不同类型的解析器,yacc和lex等等……
我同意龙书的参考;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》是个不错的选择。如果你想先玩硬核游戏,那么你应该选择《龙之书》。
资源清单:
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文件 $印刷书籍的链接
Donald Knuth的《计算机编程的艺术》最初是作为编译器编写的教程而写的,这让我很惊讶。
当然,Knuth博士对主题深入研究的倾向导致编译器写作教程被扩展到大约9卷,其中只有3卷真正出版了。这是一个关于编程主题的相当完整的阐述,涵盖了关于编写编译器需要了解的所有细节。