在c头文件中查找所有宏定义 [英] Find all macro definition in c header file

查看:70
本文介绍了在c头文件中查找所有宏定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pycparser 获取 c 头文件中所有宏定义的列表.

I am trying to get list of all macro definitions in a c header file by using pycparser.

如果可能的话,你能帮我举个例子吗?

Can you please help me on this issue with an example if possible?

谢谢.

推荐答案

尝试使用 pyparsing 代替...

Try using pyparsing instead...

from pyparsing import *

# define the structure of a macro definition (the empty term is used 
# to advance to the next non-whitespace character)
macroDef = "#define" + Word(alphas+"_",alphanums+"_").setResultsName("macro") + \
            empty + restOfLine.setResultsName("value")
with open('myheader.h', 'r') as f:
    res = macroDef.scanString(f.read())
    print [tokens.macro for tokens, startPos, EndPos in res]

myheader.h 看起来像这样:

myheader.h looks like this:

#define MACRO1 42
#define lang_init ()  c_init()
#define min(X, Y)  ((X) < (Y) ? (X) : (Y))

输出:

['MACRO1', 'lang_init', 'min']

setResultsName 允许您调用您想要作为成员的部分.因此,对于您的回答,我们做了 tokes.macro,但我们也可以轻松访问该值.我从 Paul McGuire 的例子

The setResultsName allows you to call the portion you want as a member. So for your answer we did tokes.macro, but we could have just as easily accessed the value as well. I took part of this example from Paul McGuire's example here

您可以在此处详细了解pyparsing

希望能帮到你

这篇关于在c头文件中查找所有宏定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆