C 子集的抽象语法树
出于教学目的,我们正在为 C 代码(子集)构建一个 javascript 逐步解释器。
基本上我们有:int,float...,数组,函数,for,while...没有指针。 javascript 解释器已完成,允许我们解释如何计算布尔表达式,将显示变量堆栈...
现在,我们正在手动将 C 示例转换为一些 javascript,这些 javascript 将运行并构建一系列操作(affectation、函数调用...),稍后可以用来执行逐步的操作。由于我们将自己限制在 C 的子集内,因此很容易做到。
现在我们想将 C 代码编译为我们的 javascript 表示形式。我们所需要的只是 C 代码的抽象语法树,并且 javascript 的生成很简单。
你知道有一个好的 C 语言解析器可以生成这样的树吗?不需要使用 javascript(但这将是完美的),任何语言都可以,因为这可以离线完成。
我看过 Emscripten ( https://github.com/kripken/emscripten ),但它更多的是C=>javascript 编译器,这不是我们想要的。
For teaching purpose we are building a javascript step by step interpreter for (a subset of) C code.
Basically we have : int,float..., arrays, functions, for, while... no pointers.
The javascript interpreter is done and allow us to explain how a boolean expression is evaluated, will show the variables stack...
For now, we are manually converting our C examples to some javascript that will run and build a stack of actions (affectation, function call...) that can later on be used to do the step by step stuff. Since we are limiting ourselves to a subset of C it's quite easy to do.
Now we would like to compile the C code to our javascript representation. All we need is a Abstract-syntax tree of the C code and the javascript generation is straightforward.
Do you know a good C-parser that could generate a such tree ? No need to be in javascript (but that would be perfect), any language is alright as this can be done offline.
I've looked at Emscripten ( https://github.com/kripken/emscripten ) but it's more a C=>javascript compiler and that's not what we want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近使用 Eli Bendersky 的 pycparser 来处理 C 代码的 AST。我认为它很适合您的目的。
I've recently used Eli Bendersky's pycparser to mess with ASTs of C code. I think it'd work well for your purposes.
我认为 ANTLR 有一个完整的 C 解析器。
为了完成您的翻译任务,我怀疑您将需要完整的符号表支持;你必须知道这些符号的含义。在这里,大多数“解析器”都会让你失败;他们没有建立完整的符号表。我认为 ANTLR 不会,但我可能是错的。
我们的 DMS 软件重组工具包及其 C Front End 提供完整的C arser,并构建完整的符号表。 (您的应用程序可能不需要它,但它也包含完整的 C 预处理器)。它还提供控制流、数据流、分析点和调用图构造,所有这些都有助于将 C 语言转换为任何目标虚拟机。
I think that ANTLR has a full C parser.
To do your translation task, I suspect you will need full symbol table support; you have to know what the symbols mean. Here most "parsers" will fail you; they don't build a full symbol table. I think ANTLR does not, but I could be wrong.
Our DMS Software Reengineering Toolkit with its C Front End provides a full C arser, and builds complete symbol tables. (You may not need it for your application, but it includes a full C preprocessor, too). It also provide control flow, data flow, points-to-analysis and call graph construction, all of which can be useful in translating C to whatever your target virtual machine is.