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

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


当前回答

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.

其他回答

还有一本书还没有推荐,但非常重要,那就是约翰·莱文的《链接器和加载器》。如果您不使用外部汇编程序,则需要一种方法来输出可以链接到最终程序的目标文件。即使您正在使用外部汇编程序,也可能需要了解重定位以及整个程序加载过程如何工作,以制作一个工作工具。这本书收集了很多关于各种系统的这个过程的随机知识,包括Win32和Linux。

我同意龙书的参考;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》是个不错的选择。如果你想先玩硬核游戏,那么你应该选择《龙之书》。

如果你没有时间,我推荐Niklaus Wirth的“编译器构造”(Addison-Wesley)。1996年),一本你一天就能读完的小册子,但它解释了基础知识(包括如何实现词法分析器、递归下降解析器和你自己的基于堆栈的虚拟机)。在那之后,如果你想深入了解,就像其他评论者建议的那样,没有办法绕过龙的书。

您可以使用Apache软件基金会的BCEL。使用这个工具,您可以生成类似汇编程序的代码,但它是带有BCEL API的Java。您可以学习如何生成中间语言代码(在本例中是字节代码)。

简单的例子

用这个函数创建一个Java类: maxAsString(int a, int b) { If (a > b) { 返回Integer.valueOf(一).toString (); } if (a < b) { 返回Integer.valueOf (b) .toString (); }其他{ 返回“=”; } }

现在用这个类运行BCELifier

BCELifier bcelifier = new BCELifier("MyClass", System.out);
bcelifier.start();

您可以在控制台上看到整个类的结果(如何构建字节代码MyClass.java)。该函数的代码如下:

private void createMethod_1() {
  InstructionList il = new InstructionList();
  MethodGen method = new MethodGen(ACC_PUBLIC, Type.STRING, new Type[] { Type.INT, Type.INT }, new String[] { "arg0", "arg1" }, "maxAsString", "MyClass", il, _cp);

  il.append(InstructionFactory.createLoad(Type.INT, 1)); // Load first parameter to address 1
  il.append(InstructionFactory.createLoad(Type.INT, 2)); // Load second parameter to adress 2
    BranchInstruction if_icmple_2 = InstructionFactory.createBranchInstruction(Constants.IF_ICMPLE, null); // Do if condition (compare a > b)
  il.append(if_icmple_2);
  il.append(InstructionFactory.createLoad(Type.INT, 1)); // Load value from address 1 into the stack
  il.append(_factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Constants.INVOKESTATIC));
  il.append(_factory.createInvoke("java.lang.Integer", "toString", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
  il.append(InstructionFactory.createReturn(Type.OBJECT));
  InstructionHandle ih_13 = il.append(InstructionFactory.createLoad(Type.INT, 1));
  il.append(InstructionFactory.createLoad(Type.INT, 2));
    BranchInstruction if_icmpge_15 = InstructionFactory.createBranchInstruction(Constants.IF_ICMPGE, null); // Do if condition (compare a < b)
  il.append(if_icmpge_15);
  il.append(InstructionFactory.createLoad(Type.INT, 2));
  il.append(_factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Constants.INVOKESTATIC));
  il.append(_factory.createInvoke("java.lang.Integer", "toString", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
  il.append(InstructionFactory.createReturn(Type.OBJECT));
  InstructionHandle ih_26 = il.append(new PUSH(_cp, "equals")); // Return "equals" string
  il.append(InstructionFactory.createReturn(Type.OBJECT));
  if_icmple_2.setTarget(ih_13);
  if_icmpge_15.setTarget(ih_26);
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}

我发现Dragon的书太难读了,因为它太专注于语言理论,而实际上编写编译器并不需要这些理论。

我将添加Oberon书籍,其中包含了一个惊人的快速和简单的Oberon编译器项目Oberon的完整源代码。