解析源码

发布于 2024-11-10 15:37:19 字数 293 浏览 6 评论 0原文

我需要解析不同文件的源代码,每个文件都用不同的语言编写,我想使用 C 来完成此操作。

为此,我正在考虑使用 yacc / lex,但我发现它们很难理解,可能是因为完全缺乏像样的文档(要么是这样,要么它们真的很神秘)。

所以我的问题是:在哪里可以找到一些关于 yacc / lex 的好文档,最好是教程风格的介绍?或者,有没有更好的方法在 C 中做到这一点?也许我可以使用其他东西来代替 yacc / lex,甚至可能用不同的语言编写?

I need to parse the source code of different files, each written in a different language, and I would like to do this using C.

To do that, I was thinking of using yacc / lex, but I find them very hard to understand, maybe due to the complete lack of decent documentation (either that, or they really are cryptic).

So my questions are: where can I find some good documentation for yacc / lex, preferably a tutorial style introduction? Or, is there any better way to do this in C? Maybe there's something else I could use instead of yacc / lex, perhaps even written in a different language?

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

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

发布评论

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

评论(3

甜宝宝 2024-11-17 15:37:20

如果您喜欢数学(这个答案中最重要的子句),那么编写您自己的编译器,然后用它编写您的编译器。我这样做过一次,因为我厌倦了为编译器的所有产品编写所有函数,而我最初是作为递归下降编译器开始的,因为 2004 年的可用选择并不让我满意,而且因为我有空闲时间在找工作的同时。我只在一个项目上使用了编译器编译器,并且不一定经过彻底测试,所以它不在github上。我对我设计的语法文件语法非常满意。

如果我今天有这样的需要,我可能会做出不同的决定。较新的尖端 CC 在过去 8 年里似乎发生了很大变化。

If you like math (the most important clause in this answer), then write your own compiler-compiler, and then write your compiler with that. I did this once because I was getting bored of writing all the functions for all the productions of a compiler which I had started as a recursive-descent compiler, because the available choices in 2004 didn't please me, and because I had free time while job-hunting. I only used the compiler compiler on the one project, and it is not necessarily thoroughly tested, so it is not on github. I was very happy with the grammar file syntax that I devised.

If I had such a need today I might make a different decision. The newer cutting-edge CC's seem to have have changed a lot in the last 8 years.

梨涡少年 2024-11-17 15:37:19

yacclex 是非常强大的工具,围绕编译器构造理论构建。为了能够完全理解它们,您可能需要一些形式语言、自动机理论和编译器构造方面的基础知识。

龙书是该主题的经典之作。

yacc and lex are very powerful tools, built around the theories for compiler construction. To be able to fully understand them you probably need some basics in formal languages, automata theory and compiler construction.

The dragon book is a classic on the subject.

忆伤 2024-11-17 15:37:19

Kernighan 和 Pike 的Unix 编程环境的后半部分详细介绍了如何使用 lex 和 yacc 进行解释器编程。 lex 覆盖范围有点小,因为他们大多使用自定义扫描仪。

The second half of Kernighan and Pike's The Unix Programming Environment is an extended introduction to programming an interpreter with lex and yacc. The lex coverage is a little light, as they mostly use a custom scanner.

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