是否有更现代的面向对象版本的“让我们构建一个编译器”?

发布于 2024-09-19 12:41:00 字数 1539 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

oО清风挽发oО 2024-09-26 12:41:01

听起来您完全错过了克伦肖教程的要点。 LBC 并不是要编写漂亮、干净或高效的代码。这一切都是为了将​​深入于形式理论的东西降低到一个水平,使临时编码员可以轻松快速地破解一个基本的(但可以工作!)编译器。

几年前,当我通读 LBC 时,我用 C# 重写了示例。我确信班级布局不是最好的,或者任务分离得很好,但它可以与他的 Pascal 相媲美。如果您愿意,我很乐意与您分享代码 - 请告诉我,我可以将其发布到网上并分享链接。

在我的业余时间里,我一直在写一些文章,目的是将 LBC 的哲学和编译器设计的基础知识统一在一起——在每个单元/章节的末尾提供实用的、可工作的代码,同时还讨论一些理论在探索这些想法之后,读者可以理解为什么事情是这样的。但 Crenshaw 花了好几年的时间才写出了他不完整的系列,所以我的系列只是一个白日梦……而且我使用 C(正是因为它不是 C++ 或 Java)。

It sounds like you completely missed the point of Crenshaw's tutorials. LBC isn't about writing pretty, clean, or efficient code. It's all about bringing something that's steeped in formal theory down to a level where the casual coder can easily and rapidly hack out a rudimentary (but working!) compiler.

When I read through LBC years back, I rewrote the examples in C#. I'm sure the class layout isn't the best, or tasks segregated properly, but it's comparable to his Pascal. I'd be happy to share the code with you if you like-- let me know and I can post it online and share the link.

In my spare time I've been hacking out some writing with the aim of unifying the philosophies of LBC and Basics of Compiler Design together-- walkling away with practical, working code at the end of each unit/chapter, with also discuss some theoretical stuff after exploring the ideas so the reader understands why things are the way they are. But it took Crenshaw years to write his incomplete series, so mine my be a pipe dream... and I use C (exactly because it's not C++ or Java).

夜声 2024-09-26 12:41:01

看看 Terence Parr 的“语言实现模式”。他编写了 ANTLR——Java 的解析器生成器——所以了解他的东西。它很好地解释了编译器设计的原理,并且逐步构建。

Martin Fowler 的“领域特定语言” 也不错。它的议程与纯编译器课程略有不同,但对于语言设计的关键概念来说是一个很好的参考。

Take a look at Terence Parr's "Language Implementation Patterns". He wrote ANTLR - a parser generator for Java - so knows his stuff. It explains the principles of compiler design really well and builds up gradually.

Martin Fowler's "Domain Specific Languages" is also good. It has a slightly different agenda than being a pure compilers course, but is a good reference on the key concepts of language design.

葬﹪忆之殇 2024-09-26 12:41:01

我是 “MiniJava” 以及基于“Modern Java 中的编译器实现》系列书籍。这并不能完全满足您提到的所有要求,因为 MiniJava 实现通常会生成本机代码 - 但可以轻松更改后端以发出 MSIL 或其他内容。

I'm a fan of "MiniJava" and associated work based on the "Modern Compiler Implementation in Java" family of books. This doesn't quite meet all the requirements you mention as a MiniJava implementation will, generally, generate native code - but the backend can easily be changed to emit MSIL or whatever.

奢望 2024-09-26 12:41:01

我最近在我的公司使用 BNFC 构建了一个编译器,起初我被指示使用 Flex 和 Bison (C /C++),但我发现它们很麻烦,所以我使用 BNFC 来生成 Flex 和 Bison 文件。

不能说我喜欢这些代码,我的语法相当大,生成的访问者也很大,但没有什么是我无法处理的,我从一开始就进行了 TDD,所以我总是有足够的测试来重构,但我也保留了一个 UML 图来帮助我思考我编写的额外课程。

实际上有一本名为实现编程语言的书,自称是“一本自学书,并且在某种程度上,这是一本 BNFC 工具手册”,如果我读了它,我可能会在实施决策方面遇到更少的困难,但总的来说,我发现 BNFC 足够直观,只需阅读 手册教程

最后但并非最不重要的一点是,它还可以与其他语言一起使用,包括 Java(使用 Cup 和 JLex)

I have recently built a compiler at my company using BNFC, at first I was instructed to use Flex and Bison (C/C++) but I found them to be a pain so I used BNFC to generate the Flex and Bison files.

Can't say I liked the code, my grammar was pretty big and so was the generated visitor but nothing I couldn't handle, I TDDed from the beginning so I always had enough tests to refactor and but I also kept a UML diagram to help me think about the additional classes I wrote.

There actually is a book called Implementing Programming Languages self described as "a self-study book, and to some extent, a manual to the BNFC tool" had I read it I would probably have struggled less with implementation decisions but overall I found BNFC to be intuitive enough to be able to use it by only reading the manual and the tutorial

Last but not least, it can also be used with other languages including Java (with Cup and JLex)

黑白记忆 2024-09-26 12:41:01

您是否看过 PyPy 项目?它是 Python 语言的 Python 实现。或许它能为你自举Java的目标提供一些启发?

Have you taken a look at the PyPy project? It is a Python implementation of the Python language. Maybe it can provide some inspiration for your goal of self-bootstrapping Java?

风柔一江水 2024-09-26 12:41:01

当考虑学习这些东西时,你应该看看书 language-implementation-patterns< /a> 和 antlr-reference

When thinking of learning this stuff, you should have a look at book language-implementation-patterns and antlr-reference

多情癖 2024-09-26 12:41:01

如果您喜欢通过示例来学习,Finch(我的一种小型编程语言)的代码:

  1. 是用以下语言编写的面向对象的C++。
  2. 很干净。
  3. 包括字节码编译器。

If you like to learn by example, the code for Finch, a little programming language of mine:

  1. Is written in object-oriented C++.
  2. Is very clean.
  3. Includes a bytecode compiler.
东风软 2024-09-26 12:41:01

瓦特和瓦特怎么样? Brown 的 Java 编程语言处理器。它演示了在(简单)编译器设计中使用哪些 OO 模式。我成功地将它与 C# 一起使用。

How about Watt & Brown's Programming Language Processors in Java. It demonstrates what OO patterns to use in (simple) compiler design. I used it with C# successfully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文