首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
当前回答
这是一个庞大的课题。不要低估这一点。不要低估我的观点。 我听说龙之书是一个(?)开始的地方,连同搜索。(收集学会寻找,最终它将成为你的人生。 构建自己的编程语言绝对是一个很好的练习!但你要知道,它最终永远不会被用于任何实际目的。这种情况很少有例外。
其他回答
你应该看看Darius Bacon的“ichbins”,这是一个针对小Lisp方言的编译器,目标是C,只有6页多的代码。与大多数玩具编译器相比,它的优势在于该语言足够完整,可以用它来编写编译器。(tarball还包括一个解释器来引导这个东西。)
在我的Ur-Scheme网页上有更多关于学习编写编译器的有用内容。
资源清单:
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文件 $印刷书籍的链接
我认为这是一个相当模糊的问题;只是因为这个话题涉及的深度。然而,编译器可以被分解成两个独立的部分;上半身和下半身。上半部分通常采用源语言并将其转换为中间表示,下半部分负责特定于平台的代码生成。
尽管如此,解决这个问题的一个简单方法(至少是我们在编译器类中使用的方法)是在上面描述的两部分中构建编译器。具体来说,通过构建上半部分,您将对整个过程有一个很好的了解。
Just doing the top half lets you get the experience of writing the lexical analyzer and the parser and go to generating some "code" (that intermediate representation I mentioned). So it will take your source program and convert it to another representation and do some optimization (if you want), which is the heart of a compiler. The bottom half will then take that intermediate representation and generate the bytes needed to run the program on a specific architecture. For example, the the bottom half will take your intermediate representation and generate a PE executable.
关于这个主题的一些书,我发现特别有用的是编译器原理和技术(或者龙书,因为封面上有可爱的龙)。它有一些很棒的理论,而且肯定以一种非常容易理解的方式涵盖了上下文无关语法。此外,为了构建词法分析器和解析器,您可能会使用*nix工具lex和yacc。而且很无趣的是,这本名为《莱科斯和雅克》的书继承了《龙之书》这部分的内容。
“…让我们构建一个编译器……”
我认为@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
这是一个庞大的课题。不要低估这一点。不要低估我的观点。 我听说龙之书是一个(?)开始的地方,连同搜索。(收集学会寻找,最终它将成为你的人生。 构建自己的编程语言绝对是一个很好的练习!但你要知道,它最终永远不会被用于任何实际目的。这种情况很少有例外。