首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
当前回答
如果你对为函数式语言(而不是过程式语言)编写编译器感兴趣,Simon Peyton-Jones和David Lester的《实现函数式语言:教程》是一个很好的指南。
函数式计算如何工作的基本概念是通过简单但功能强大的函数式语言“Core”中的示例来指导的。此外,Core语言编译器的每个部分都用Miranda(一种与Haskell非常相似的纯函数式语言)中的代码示例进行了解释。
书中描述了几种不同类型的编译器,但即使你只遵循Core的模板编译器,你也会对函数式编程有一个很好的理解。
其他回答
最快的方法是读两本书:
JP Bennett的1990版《编译技术介绍,使用ANSI C, LeX和YaCC的第一课程》-示例代码,解析理论和设计的完美平衡-它包含了一个用C, LeX和YaCC编写的简单语法的完整编译器
龙书(旧版本)-主要是一个详细的参考功能没有涵盖在前一本书
如果你像我一样,没有受过正规的计算机科学教育,并且对 构建/想知道编译器是如何工作的:
我推荐“Java编程语言处理器:编译器和解释器”, 对于自学成才的计算机程序员来说,这是一本令人惊叹的书。
在我看来,理解那些基本的语言理论、自动化机器和集合理论并不是什么大问题。问题是如何把这些东西转换成代码。上面的书告诉您如何编写解析器、分析上下文和生成代码。如果你不能理解这本书,那么我不得不说,放弃构建编译器吧。这本书是我读过的最好的编程书。
还有一本书,也很好,《c语言编译器设计》,里面有很多代码,告诉你如何构建编译器和词法分析器工具。
构建编译器是一种有趣的编程实践,可以教会您许多编程技能。
不要买龙的书。这是浪费金钱和时间,不适合从业者。
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.
“…让我们构建一个编译器……”
我认为@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
资源清单:
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文件 $印刷书籍的链接