C的子集的抽象语法树

时间:2023-01-25 09:29:12

For teaching purpose we are building a javascript step by step interpreter for (a subset of) C code.

为了教学的目的,我们正在为C代码(一个子集)一步一步地构建一个javascript解释器。

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...

基本上我们有:int,float…,数组,函数,for, while…没有指针。javascript解释器已经完成,允许我们解释布尔表达式是如何计算的,它将显示变量堆栈……

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.

目前,我们正在手动将C示例转换为一些javascript,这些javascript将运行并构建一个操作栈(affectation, function call…),稍后可以使用这些操作来完成步骤。因为我们把自己限制在C的一个子集,所以这很容易做到。

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.

现在,我们希望将C代码编译成javascript表示。我们所需要的是C代码的抽象语法树,javascript生成非常简单。

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.

你知道一个好的c解析器可以生成这样的树吗?不需要使用javascript(但这是完美的),任何语言都可以,因为这可以离线完成。

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.

我已经看过了Emscripten (https://github.com/kripken/emscripten),但是它更多的是一个C=>javascript编译器,这不是我们想要的。

2 个解决方案

#1


3  

I've recently used Eli Bendersky's pycparser to mess with ASTs of C code. I think it'd work well for your purposes.

我最近使用了Eli Bendersky的pycparser来处理C代码的ast。我认为这对你的目的很有效。

#2


0  

I think that ANTLR has a full C parser.

我认为ANTLR有一个完整的C解析器。

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.

为了完成您的翻译任务,我怀疑您将需要完整的符号表支持;你必须知道符号是什么意思。在这里,大多数“解析器”都会让你失望;它们不构建完整的符号表。我认为ANTLR没有,但我可能是错的。

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.

我们的DMS软件Reengineering Toolkit及其C前端提供完整的C arser,并构建完整的符号表。(您的应用程序可能不需要它,但它也包含完整的C预处理器)。它还提供控制流、数据流、点分析和调用图构造,所有这些都可以用于将C转换为目标虚拟机。

#1


3  

I've recently used Eli Bendersky's pycparser to mess with ASTs of C code. I think it'd work well for your purposes.

我最近使用了Eli Bendersky的pycparser来处理C代码的ast。我认为这对你的目的很有效。

#2


0  

I think that ANTLR has a full C parser.

我认为ANTLR有一个完整的C解析器。

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.

为了完成您的翻译任务,我怀疑您将需要完整的符号表支持;你必须知道符号是什么意思。在这里,大多数“解析器”都会让你失望;它们不构建完整的符号表。我认为ANTLR没有,但我可能是错的。

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.

我们的DMS软件Reengineering Toolkit及其C前端提供完整的C arser,并构建完整的符号表。(您的应用程序可能不需要它,但它也包含完整的C预处理器)。它还提供控制流、数据流、点分析和调用图构造,所有这些都可以用于将C转换为目标虚拟机。