AST from C code [closed]

2019-01-31 12:57发布

I want to perform some transformations on C source code. I need a tool on linux that generates a complete AST from the source code so that I can apply my transformations on this AST and then convert it back to the C source code. I tried ELSA but it is not getting compiled. (I am using Ubuntu 8.4). Can anyone suggest a better tool/application?

11条回答
对你真心纯属浪费
2楼-- · 2019-01-31 13:24

www.antlr.org

查看更多
来,给爷笑一个
3楼-- · 2019-01-31 13:24

You can try generate AST (Abstract Syntax Tree) using Lexx and Yacc on Linux:

lex and yacc

from lex and yacc to ast

查看更多
神经病院院长
4楼-- · 2019-01-31 13:25

"I tried ELSA but it is not getting compiled. (I am using Ubuntu 8.4)"

The Elkhound and Elsa source code, version 2005.08.22b from scottmcpeak.com/elkhound/ is outdated (old C++ style .h header files).

Elsa is working and part of Oink: http://www.cubewano.org/oink/#Gettingthecode I have just got it working now under Ubuntu 9.10.

查看更多
姐就是有狂的资本
5楼-- · 2019-01-31 13:27

To get AST XML output you can try to use cscan from MarpaX::Languages::C::AST. The output will look like:

xml <cscan> <typedef_hash> <typedef id="GLenum" before="unsigned int" after="" file="/usr/include/GL/gl.h"/> ...

查看更多
Fickle 薄情
6楼-- · 2019-01-31 13:28

There are two projects that I'm aware of and that you could find useful:

They both parse a standard C source code to allow further analisys and transformation. I've not used them so you have to check for yourself if they fit your needs.

The suggestion of using GCC is also valid, of course. I know there's not much documentation on this aspect of gcc, though.

查看更多
SAY GOODBYE
7楼-- · 2019-01-31 13:32

See pycparser - a pure-Python AST generator for C.

查看更多
登录 后发表回答