首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
首选语言:C/ c++、Java、Ruby。
我正在寻找一些关于如何编写自己的编译器的有用书籍/教程,只是为了教育目的。我最熟悉C/ c++、Java和Ruby,所以我更喜欢包含这三者之一的资源,但任何好的资源都是可以接受的。
当前回答
如果你想使用功能强大的高级工具,而不是自己构建一切,那么阅读本课程的项目和阅读材料是一个很好的选择。这是一门语言课程,由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非常擅长从中生成优化的程序集。
其他回答
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
龙之书绝对是关于“构建编译器”的书,但是如果您的语言不像当前这一代语言那么复杂,那么您可能想看看设计模式中的解释器模式。
书中的示例设计了一种类似正则表达式的语言,并且经过了深思熟虑,但正如书中所述,它有助于思考整个过程,但实际上只在小型语言上有效。然而,用这种模式为一种小型语言编写解释器要快得多,而不必学习所有不同类型的解析器,yacc和lex等等……
如果您不仅仅是在寻找书籍,而且还对有关于该主题的文章的网站感兴趣,我已经在博客中讨论了创建编程语言的各个方面。大部分文章都可以在我博客的“语言设计”类别中找到。
特别是,我介绍了手动生成Intel机器代码、自动生成机器码或字节码、创建字节码解释器、编写面向对象的运行时、创建简单的加载器以及编写简单的标记/清除垃圾收集器。所有这些都是非常实用和务实的方式,而不是用大量的理论让你感到无聊。
将感激反馈这些。
资源清单:
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文件 $印刷书籍的链接
你可能想看看Lex/Yacc(或Flex/Bison,随便你怎么称呼它们)。Flex是一个词法分析器,它将解析和识别语言的语义组件(“标记”),而Bison将用于定义解析每个标记时发生的情况。这可以是,但绝对不限于,打印出C代码,供编译器编译成C,或者动态运行指令。
这个常见问题应该对你有帮助,这个教程看起来很有用。