【问题标题】:pycparser.plyparser.ParseError on complex struct复杂结构上的 pycparser.plyparser.ParseError
【发布时间】:2016-08-18 23:55:39
【问题描述】:

我正在尝试使用pycparser 来解析这个 C 代码:

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

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

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

【问题讨论】:

  • 我认为你应该首先调试你的python软件。这里没有 gcc 特定的问题。你应该添加python标签,让python社区帮助你。
  • 谢谢,我刚刚做了。
  • 你的 c 代码可以在没有 python 的情况下编译和工作吗?你为什么不使用cffi?您是否在 ply/pycparser 跟踪器中报告了此问题? github.com/eliben/pycparser/issues/newgithub.com/dabeaz/ply/issues/new
  • @denfromufa 是的,代码使用 GCC 编译。我没有使用cffi,因为我只想解析 C 代码并做一些静态分析。是的,我刚刚在这里提交了一个错误报告:github.com/eliben/pycparser/issues/143
  • 是我遗漏了什么,还是我们应该删除标签python-cffi

标签: python c ply pycparser


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多