如何解析代码以构建 Java 编译器?

发布于 2024-07-15 18:53:35 字数 231 浏览 6 评论 0 原文

我需要写一个编译器。 这是大学的家庭作业。 老师告诉我们,我们可以使用任何我们想要的API来进行代码的解析,只要它是好的。 这样我们就可以更加关注我们将生成的 JVM。

所以是的,我将用 Java 编写一个编译器来生成 Java。

你知道有什么好的API吗? 我应该使用正则表达式吗? 我通常手动编写自己的解析器,尽管在这种情况下不建议这样做。

任何帮助,将不胜感激。

I need to write a compiler. It's homework at the univ. The teacher told us that we can use any API we want to do the parsing of the code, as long as it is a good one. That way we can focus more on the JVM we will generate.

So yes, I'll write a compiler in Java to generate Java.

Do you know any good API for this? Should I use regex? I normally write my own parsers by hand, though it is not advisable in this scenario.

Any help would be appreciated.

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

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

发布评论

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

评论(12

七颜 2024-07-22 18:53:35

正则表达式很适合在编译器中使用,但仅用于识别标记(即没有递归结构)。

编写编译器的经典方法是使用一个用于识别标记的词法分析器、一个用于识别结构的语法分析器、一个用于识别含义的语义分析器 ,一个中间代码生成器,一个优化器,最后一个目标代码生成器。 如果使编译器更易于编写,则可以合并或完全跳过这些步骤中的任何一个。

已经开发了许多工具来帮助完成此过程。 对于 Java,您可以查看

Regex is good to use in a compiler, but only for recognizing tokens (i.e. no recursive structures).

The classic way of writing a compiler is having a lexical analyzer for recognizing tokens, a syntax analyzer for recognizing structure, a semantic analyzer for recognizing meaning, an intermediate code generator, an optimizer, and last a target code generator. Any of those steps can be merged, or skipped entirely, if makes the compiler easier to write.

There have been many tools developed to help with this process. For Java, you can look at

近箐 2024-07-22 18:53:35

我推荐 ANTLR,主要是因为它通过 StringTemplate 生成输出的功能。

更好的是 Terence Parr 的书 迄今为止是其中之一更好的书籍面向使用解析器生成器编写编译器。

然后您就拥有了 ANTLRWorks,它使您能够即时学习和调试语法。

最重要的是,ANTLR wiki + 文档,(虽然不够全面,不符合我的喜好),但对于任何初学者来说都是一个很好的起点。 它帮助我在一周内刷新了编译器编写的知识。

I would recommend ANTLR, primarily because of its output generation capabilities via StringTemplate.

What is better is that Terence Parr's book on the same is by far one of the better books oriented towards writing compilers with a parser generator.

Then you have ANTLRWorks which enables you to study and debug your grammar on the fly.

To top it all, the ANTLR wiki + documentation, (although not comprehensive enough to my liking), is a good place to start off for any beginner. It helped me refresh knowledge on compiler writing in a week.

浅语花开 2024-07-22 18:53:35

看一下 JavaCC,Java 的语言解析器。 它非常容易使用并掌握窍门

Have a look at JavaCC, a language parser for Java. It's very easy to use and get the hang of

旧人哭 2024-07-22 18:53:35

走向经典 - Lex + Yacc。 在 Java 中,它拼写为 JAXjavacc。 Javacc 甚至还有一些 Java 语法准备接受检查。

Go classic - Lex + Yacc. In Java it spells JAX and javacc. Javacc even has some Java grammars ready for inspection.

笑着哭最痛 2024-07-22 18:53:35

我建议使用像 ANTLR 这样的元编译器,或者简单的 解析器组合器 库。 函数式 Java 有一个 解析器组合器 API。 还有JParsec。 这两个都基于Haskell 的 Parsec 库

I'd recommend using either a metacompiler like ANTLR, or a simple parser combinator library. Functional Java has a parser combinator API. There's also JParsec. Both of these are based on the Parsec library for Haskell.

你怎么这么可爱啊 2024-07-22 18:53:35

JFlex 是一个扫描仪生成器,根据 手册,旨在与解析器生成器一起使用银联

JFlex 的主要设计目标之一是使与免费 Java 解析器生成器 CUP 的接口尽可能简单 [原文如此]。

它还支持 支持 BYACC/J,顾名思义,它是 Berkeley YACC 的一个端口,用于生成 Java 代码。

我已经使用过 JFlex 本身并且很喜欢它。 然而,我正在做的项目很简单,我手工编写了解析器,所以我不知道 CUP 或 BYACC/J 有多好。

JFlex is a scanner generator which, according to the manual, is designed to work with the parser generator CUP.

One of the main design goals of JFlex was to make interfacing with the free Java parser generator CUP as easy as possibly [sic].

It also has support for BYACC/J, which, as its name suggests, is a port of Berkeley YACC to generate Java code.

I have used JFlex itself and liked it. Howeveer, the project I was doing was simple enough that I wrote the parser by hand, so I don't know how good either CUP or BYACC/J is.

别挽留 2024-07-22 18:53:35

我在编译器课程中使用了 SableCC,尽管不是我的选择。

我记得发现它非常庞大和重量级,更注重清洁而不是方便(没有运算符优先级或任何东西;你必须在语法中说明这一点)。

如果可以选择的话,我可能会想使用其他东西。 我使用 yacc(针对 C)和 happy(针对 Haskell)的经历都很愉快。

I've used SableCC in my compiler course, though not by choice.

I remember finding it very bulky and heavyweight, with more emphasis on cleanliness than convenience (no operator precedence or anything; you have to state that in the grammar).

I'd probably want to use something else if I had the choice. My experiences with yacc (for C) and happy (for Haskell) have both been pleasant.

一腔孤↑勇 2024-07-22 18:53:35

解析器组合器是一个不错的选择。 流行的 Java 实现是 JParsec。

Parser combinators is a good choice. Popular Java implementation is JParsec.

蓝咒 2024-07-22 18:53:35

如果你想成为硬核,请加入一些 http://llvm.org :)

If you're going to go hardcore, throw in a bit of http://llvm.org in the mix :)

狂之美人 2024-07-22 18:53:35

我建议您查看 BeanShell 的源代码。 它有一个 Java 编译器,并且相当容易阅读。

I suggest you look at at the source for BeanShell. It has a compiler for Java and is fairly simple to read.

咋地 2024-07-22 18:53:35

使用解析器组合器,例如 JParsec。 有有关如何使用它的精彩视频教程

Use a parser combinator, like JParsec. There's a good video tutorial on how to use it.

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