首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
当前回答
Python附带了一个用Python编写的Python编译器。您可以看到源代码,它包括从解析、抽象语法树、发出代码等所有阶段。 破解它。
其他回答
I think Modern Compiler Implementation in ML is the best introductory compiler writing text. There's a Java version and a C version too, either of which might be more accessible given your languages background. The book packs a lot of useful basic material (scanning and parsing, semantic analysis, activation records, instruction selection, RISC and x86 native code generation) and various "advanced" topics (compiling OO and functional languages, polymorphism, garbage collection, optimization and single static assignment form) into relatively little space (~500 pages).
我更喜欢《现代编译器实现》而不是《Dragon》,因为《现代编译器实现》对该领域的调查较少——相反,它确实涵盖了编写一个严肃、体面的编译器所需的所有主题。在你读完这本书之后,如果你需要的话,你就可以直接深入研究论文了。
I must confess I have a serious soft spot for Niklaus Wirth's Compiler Construction. It is available online as a PDF. I find Wirth's programming aesthetic simply beautiful, however some people find his style too minimal (for example Wirth favors recursive descent parsers, but most CS courses focus on parser generator tools; Wirth's language designs are fairly conservative.) Compiler Construction is a very succinct distillation of Wirth's basic ideas, so whether you like his style or not or not, I highly recommend reading this book.
如果您愿意使用LLVM,请查看这个:http://llvm.org/docs/tutorial/。它教你如何使用LLVM的框架从头编写编译器,并且不假设你有任何关于这个主题的知识。
本教程建议您编写自己的解析器和lexer等,但我建议您了解bison和flex后再进行研究。它们让生活变得轻松多了。
如果你没有时间,我推荐Niklaus Wirth的“编译器构造”(Addison-Wesley)。1996年),一本你一天就能读完的小册子,但它解释了基础知识(包括如何实现词法分析器、递归下降解析器和你自己的基于堆栈的虚拟机)。在那之后,如果你想深入了解,就像其他评论者建议的那样,没有办法绕过龙的书。
抱歉,这是西班牙文,但这是阿根廷一门名为“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.
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.