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

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


当前回答

创建编译器的一个简单方法是使用bison和flex(或类似),构建一个树(AST)并用C生成代码,生成C代码是最重要的步骤。通过生成C代码,您的语言将自动在所有具有C编译器的平台上运行。

生成C代码就像生成HTML一样简单(只需使用打印或等效工具),这又比编写C解析器或HTML解析器容易得多。

其他回答

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.

抱歉,这是西班牙文,但这是阿根廷一门名为“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.

As an starting point, it will be good to create a recursive descent parser (RDP) (let's say you want to create your own flavour of BASIC and build a BASIC interpreter) to understand how to write a compiler. I found the best information in Herbert Schild's C Power Users, chapter 7. This chapter refers to another book of H. Schildt "C The complete Reference" where he explains how to create a calculator (a simple expression parser). I found both books on eBay very cheap. You can check the code for the book if you go to www.osborne.com or check in www.HerbSchildt.com I found the same code but for C# in his latest book

Donald Knuth的《计算机编程的艺术》最初是作为编译器编写的教程而写的,这让我很惊讶。

当然,Knuth博士对主题深入研究的倾向导致编译器写作教程被扩展到大约9卷,其中只有3卷真正出版了。这是一个关于编程主题的相当完整的阐述,涵盖了关于编写编译器需要了解的所有细节。