如何使用 C++ 构建 C 解释器或其他一些PL

发布于 2024-10-12 20:52:13 字数 302 浏览 8 评论 0原文

我正在考虑为 C 语言创建一个解释器。

我已经掌握了这个链接,但我想知道是否有更好的方法。现在,我正在考虑使用 C++ 来实现它。但如果有任何其他语言更适合这项任务,那么我愿意为此目的而更改为该语言。

我正在构建用于静态分析目的的解释器,这需要诸如语句标记、地址存储、堆地址存储和此分析通常所需的其他操作等操作。

任何可以帮助我入门的链接都会很棒。请分享您的想法,我将非常感激,因为这对我来说是一个全新的领域。谢谢大家

I am looking at creating an interpreter for the C language.

I have got hold of this link but I would like to know if there is any better way to do it. Right now, I am looking at implementing it using C++. But if there is any other language which is better at this task then I am willing to change to that for this purpose .

I am building the interpreter for static analysis purpose, which would require operations like statement labeling, address storing, Heap address storing and other operations typically required for this analysis.

Any links which would help me in getting started would be great. Please share your thoughts and I would really appreciate it since it is a completely new area for me. Thank you all

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

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

发布评论

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

评论(3

梦开始←不甜 2024-10-19 20:52:13

静态分析某些代码的工具不是解释器。

不管怎样,编写这样的工具并不是一件小事,它需要很好地理解编译器的工作原理。通常这样的工作分为许多任务:首先对代码进行标记、解析并构建抽象语法树

一旦声明了您的语言(在本例中为 C)规则,这些操作就很容易完成,并且有许多工具可以自动化的过程。最著名的工具可能是 bison(针对 C);还有许多非常好的且易于使用的 C++ 库(例如 boost.spirit),甚至还有一些专门为执行这些操作而设计的语言。

因此,我的建议是提高您对编译器的了解,并尝试为更简单的语言编写一些解析器。你不能从无到有地开始分析 C。

A tool to statically analyze some code is not an interpreter.

Anyway, writing such tool is not a trivial thing, it requires a good understanding of how compilers work. Usually such job is split in many tasks: tokenizing the code, parsing it and building an abstract syntax tree first of all

These operations are simple to do once you declare your language (in this case C) rules, and there are many tools to automatize the process. Most famous tool is probably bison (which is for C); there are also many really good and easy-to-use C++ libraries (like boost.spirit), and even some languages which have been designed in order to do these stuff.

My suggestion is thus to improve your knowledge about compilers, and to try and write some parsers for simpler languages. You cannot start analyzing C from nowhere.

走野 2024-10-19 20:52:13

我可以建议使用库来进行解析吗?

Clang 是一个模块化项目,提供(除其他外)一个 C 解析器,它生成一个您可以自由探索的 AST(抽象语法树)。

它还有一个由 Ted Kremenek 领导的静态分析器工作正在进行中,您也许应该检查一下......也许您能够为它做出贡献:)

May I suggest using a library to do the parsing ?

Clang is a modular project providing (amongst other things) a C parser that yields a AST (Abstract Syntax Tree) that you can freely explore.

It also has a Static Analyzer work in progress, led by Ted Kremenek, that you should perhaps check out... and maybe you'll be able to contribute to it :)

梦言归人 2024-10-19 20:52:13

已经提到了 Clang,因此对于问题的“其他 PL”部分,我建议查看 http:// /cil.sourceforge.net/

Clang was already mentioned, so for the "other PL" part of the question I'd suggest to take a look at http://cil.sourceforge.net/

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