如何在Python中解析C格式字符串?

时间:2015-05-03 07:35:33

标签: python c regex clang pycparser

我在C文件中有这个代码:

printf("Worker name is %s and id is %d", worker.name, worker.id);

我希望,使用Python,能够解析格式字符串并找到"%s""%d"

所以我想要一个功能:

>>> my_function("Worker name is %s and id is %d")
[Out1]: ((15, "%s"), (28, "%d))

我尝试使用libclang的Python绑定和pycparser来实现这一目标,但我没有看到如何使用这些工具完成此操作。

我也尝试使用正则表达式来解决这个问题,但这并不简单 - 考虑printf"%%s"时的用例以及类似的内容。

gcc和clang显然都是编译的一部分 - 没有人将这个逻辑导出到Python?

2 个答案:

答案 0 :(得分:3)

您当然可以使用正则表达式找到格式正确的候选人。

看一下C Format Specification的定义。 (使用微软,但使用你想要的。)

是:

%[flags] [width] [.precision] [{h | l | ll | w | I | I32 | I64}] type

您还有%%的特殊情况,它在printf中变为%

您可以将该模式转换为正则表达式:

(                                 # start of capture group 1
%                                 # literal "%"
(?:                               # first option
(?:[-+0 #]{0,5})                  # optional flags
(?:\d+|\*)?                       # width
(?:\.(?:\d+|\*))?                 # precision
(?:h|l|ll|w|I|I32|I64)?           # size
[cCdiouxXeEfgGaAnpsSZ]            # type
) |                               # OR
%%)                               # literal "%%"

Demo

然后进入Python正则表达式:

import re

lines='''\
Worker name is %s and id is %d
That is %i%%
%c
Decimal: %d  Justified: %.6d
%10c%5hc%5C%5lc
The temp is %.*f
%ss%lii
%*.*s | %.3d | %lC | %s%%%02d'''

cfmt='''\
(                                  # start of capture group 1
%                                  # literal "%"
(?:                                # first option
(?:[-+0 #]{0,5})                   # optional flags
(?:\d+|\*)?                        # width
(?:\.(?:\d+|\*))?                  # precision
(?:h|l|ll|w|I|I32|I64)?            # size
[cCdiouxXeEfgGaAnpsSZ]             # type
) |                                # OR
%%)                                # literal "%%"
'''

for line in lines.splitlines():
    print '"{}"\n\t{}\n'.format(line, 
           tuple((m.start(1), m.group(1)) for m in re.finditer(cfmt, line, flags=re.X))) 

打印:

"Worker name is %s and id is %d"
    ((15, '%s'), (28, '%d'))

"That is %i%%"
    ((8, '%i'), (10, '%%'))

"%c"
    ((0, '%c'),)

"Decimal: %d  Justified: %.6d"
    ((9, '%d'), (24, '%.6d'))

"%10c%5hc%5C%5lc"
    ((0, '%10c'), (4, '%5hc'), (8, '%5C'), (11, '%5lc'))

"The temp is %.*f"
    ((12, '%.*f'),)

"%ss%lii"
    ((0, '%s'), (3, '%li'))

"%*.*s | %.3d | %lC | %s%%%02d"
    ((0, '%*.*s'), (8, '%.3d'), (15, '%lC'), (21, '%s'), (23, '%%'), (25, '%02d'))

答案 1 :(得分:1)

一个简单的实现可能是以下生成器:

def find_format_specifiers(s):
    last_percent = False
    for i in range(len(s)):
        if s[i] == "%" and not last_percent:
            if s[i+1] != "%":
                yield (i, s[i:i+2])
            last_percent = True
        else:
            last_percent = False

>>> list(find_format_specifiers("Worker name is %s and id is %d but %%q"))
[(15, '%s'), (28, '%d')]

如果需要,可以相当容易地扩展它以处理其他格式说明符信息,如宽度和精度。