复杂结构上的pycparser.plyparser.ParseError

时间:2016-08-18 23:55:39

标签: python c ply pycparser

我尝试使用pycparser来解析此C代码:

https://github.com/nbeaver/mx-trunk/blob/0b80678773582babcd56fe959d5cfbb776cc0004/libMx/d_adsc_two_theta.c

带有最小示例的repo和Makefile在这里:

https://github.com/nbeaver/pycparser-problem

在Debian Jessie上使用pycparser v2.14(来自pip)和gcc 4.9.2。

我尝试过的事情:

  • -nostdinc标记传递给gcc并包含fake_libc_include文件夹。
  • 使用-D'__attribute__(x)='取出GCC扩展程序
  • 使用假标头例如<sys/param.h>
  • 如果代码不兼容C99,请使用-std=c99
  • 如果我的机器有些奇怪的话,请重现redis example

这就是回溯的样子:

Traceback (most recent call last):
  File "just_parse.py", line 21, in <module>
    parse(path)
  File "just_parse.py", line 9, in parse
    ast = pycparser.parse_file(filename)
  File "/home/nathaniel/.local/lib/python2.7/site-packages/pycparser/__init__.py", line 93, in parse_file
    return parser.parse(text, filename)
  File "/home/nathaniel/.local/lib/python2.7/site-packages/pycparser/c_parser.py", line 146, in parse
    debug=debuglevel)
  File "/home/nathaniel/.local/lib/python2.7/site-packages/pycparser/ply/yacc.py", line 265, in parse
    return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
  File "/home/nathaniel/.local/lib/python2.7/site-packages/pycparser/ply/yacc.py", line 1047, in parseopt_notrack
    tok = self.errorfunc(errtoken)
  File "/home/nathaniel/.local/lib/python2.7/site-packages/pycparser/c_parser.py", line 1680, in p_error
    column=self.clex.find_tok_column(p)))
  File "/home/nathaniel/.local/lib/python2.7/site-packages/pycparser/plyparser.py", line 55, in _parse_error
    raise ParseError("%s: %s" % (coord, msg))
pycparser.plyparser.ParseError: in/d_adsc_two_theta.c:63:82: before: .

追溯指向此行:

https://github.com/nbeaver/mx-trunk/blob/0b80678773582babcd56fe959d5cfbb776cc0004/libMx/d_adsc_two_theta.c#L63

反过来指向此#define宏:

https://github.com/nbeaver/mx-trunk/blob/0b80678773582babcd56fe959d5cfbb776cc0004/libMx/mx_motor.h#L484

1 个答案:

答案 0 :(得分:0)

原因似乎是offsetof()功能。最小的工作示例由最近的提交修复,但是:

https://github.com/eliben/pycparser/issues/87