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

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


当前回答

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.

其他回答

资源清单:

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

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

我记得大约七年前问过这个问题,当时我对编程还是个新手。

我问的时候非常小心,令人惊讶的是,我没有像你们在这里受到那么多批评。然而,他们确实给我指出了“龙书”的方向,在我看来,这是一本真正伟大的书,解释了编写编译器所需知道的一切(当然,你必须掌握一两种语言)。你懂的语言越多越好。

是的,很多人说读那本书是疯狂的,你不会从中学到任何东西,但我完全不同意这种说法。

许多人还说编写编译器是愚蠢和毫无意义的。编译器开发之所以有用,原因有很多:

因为它很有趣。 它是有教育意义的,当你学习如何编写编译器时,你会学到很多计算机科学和其他技术,这些技术在编写其他应用程序时很有用。 如果没有人编写编译器,现有的语言就不会变得更好。

我没有立即编写自己的编译器,但在询问之后,我知道从哪里开始。现在,在学习了许多不同的语言和阅读了龙书之后,写作并不是什么大问题。(我也在学习计算机工程,但我对编程的大部分知识都是自学的。)

总之,《龙之书》是一个很棒的“教程”。但是在尝试编写编译器之前,先花些时间掌握一两种语言。不过,不要指望在未来十年左右成为编译器大师。

如果你想学习如何编写解析器/解释器,这本书也是不错的选择。

“让我们构建一个编译器”是很棒的,但它有点过时了。(我并不是说这让它变得不那么有效。)

或者看看俚语。这类似于“让我们构建一个编译器”,但对于初学者来说是一个更好的资源。这是一个pdf教程,需要7步的方法来教你一个编译器。添加quora链接,因为它有各种各样的俚语端口的链接,在c++, Java和JS中,也有python和Java中的解释器,最初是用c#和。net平台编写的。

最快的方法是读两本书:

JP Bennett的1990版《编译技术介绍,使用ANSI C, LeX和YaCC的第一课程》-示例代码,解析理论和设计的完美平衡-它包含了一个用C, LeX和YaCC编写的简单语法的完整编译器

龙书(旧版本)-主要是一个详细的参考功能没有涵盖在前一本书