首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
首选语言: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.
如果你想使用功能强大的高级工具,而不是自己构建一切,那么阅读本课程的项目和阅读材料是一个很好的选择。这是一门语言课程,由Java解析器引擎ANTLR的作者编写。你可以从Pragmatic Programmers网站上获得这门课程的PDF版本。
The course goes over the standard compiler compiler stuff that you'd see elsewhere: parsing, types and type checking, polymorphism, symbol tables, and code generation. Pretty much the only thing that isn't covered is optimizations. The final project is a program that compiles a subset of C. Because you use tools like ANTLR and LLVM, it's feasible to write the entire compiler in a single day (I have an existence proof of this, though I do mean ~24 hours). It's heavy on practical engineering using modern tools, a bit lighter on theory.
顺便说一下,LLVM非常棒。在许多情况下,你可能会编译到汇编,你最好编译到LLVM的中间表示。它是更高级别的、跨平台的,LLVM非常擅长从中生成优化的程序集。
抱歉,这是西班牙文,但这是阿根廷一门名为“Compiladores e Intérpretes”(编译器和口译员)的课程的参考书目。
这门课程从形式化语言理论到编译器构造,这些是你至少构建一个简单的编译器所需要的主题:
Compilers Design in C. Allen I. Holub Prentice-Hall. 1990. Compiladores. Teoría y Construcción. Sanchís Llorca, F.J. , Galán Pascual, C. Editorial Paraninfo. 1988. Compiler Construction. Niklaus Wirth Addison-Wesley. 1996. Lenguajes, Gramáticas y Autómatas. Un enfoque práctico. Pedro Isasi Viñuela, Paloma Martínez Fernández, Daniel Borrajo Millán. Addison-Wesley Iberoamericana (España). 1997. The art of compiler design. Theory and practice. Thomas Pittman, James Peters. Prentice-Hall. 1992. Object-Oriented Compiler Construction. Jim Holmes. Prentice Hall, Englewood Cliffs, N.J. 1995 Compiladores. Conceptos Fundamentales. B. Teufel, S. Schmidt, T. Teufel. Addison-Wesley Iberoamericana. 1995. Introduction to Automata Theory, Languages, and Computation. John E. Hopcroft. Jeffref D. Ullman. Addison-Wesley. 1979. Introduction to formal languages. György E. Révész. Mc Graw Hill. 1983. Parsing Techniques. A Practical Guide. Dick Grune, Ceriel Jacobs. Impreso por los autores. 1995 http://www.cs.vu.nl/~dick/PTAPG.html Yacc: Yet Another Compiler-Compiler. Stephen C. Johnson Computing Science Technical Report Nº 32, 1975. Bell Laboratories. Murray Hill, New Jersey. Lex: A Lexical Analyzer Generator. M. E. Lesk, E. Schmidt. Computing Science Technical Report Nº 39, 1975. Bell Laboratories. Murray Hill, New Jersey. lex & yacc. John R. Levine, Tony Mason, Doug Brown. O’Reilly & Associates. 1995. Elements of the theory of computation. Harry R. Lewis, Christos H. Papadimitriou. Segunda Edición. Prentice Hall. 1998. Un Algoritmo Eficiente para la Construcción del Grafo de Dependencia de Control. Salvador V. Cavadini. Trabajo Final de Grado para obtener el Título de Ingeniero en Computación. Facultad de Matemática Aplicada. U.C.S.E. 2001.
我同意龙书的参考;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》是个不错的选择。如果你想先玩硬核游戏,那么你应该选择《龙之书》。
这里有很多很好的答案,所以我想在列表中再添加一个:
十多年前,我有一本叫做Project Oberon的书,里面有一些关于编译器的非常好的文字。这本书的真正突出之处在于,它的来源和解释都是非常实用和易读的。全文(2005年版)已以pdf格式提供,因此您可以立即下载。编译器将在第12章讨论:
http://www.ethoberon.ethz.ch/WirthPubl/ProjectOberon.pdf
尼克劳斯·沃斯,于尔格·古特克内西
(这本书的内容不像他关于编译器的书那么广泛)
我读过几本关于编译器的书,我可以第二龙书,花时间在这本书上是非常值得的。