0

我正在使用 pycparser 解析 C 源代码的项目。

因此,当我运行预处理器时,使用https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html我在我的预处理翻译单元中有 #linenum filename flags linemarkers。

但是,当我解析gcc -E使用 pycparser 的输出时,标记嵌入了坐标(文件、行、列),但它们似乎不包含来自 linemarkers 标志的任何对我非常有用的信息。

在我的 AST 中包含线标记或将信息嵌入 AST 令牌中的任何解决方案或建议?


更新 我需要的是通过我的令牌并了解它们所属的文件(这已经在 pycparser 中)以及如何包含该文件。

此信息位于预处理器引入的留置权标记的标志字段中。确实,如果我有:

<tokens of file1.h>
<tokens of file2.h>
<tokens of main.c>

file2.h 的包含可能在 file1.h 或 main.c 中。我需要使用 pycparser 提取此信息。我知道我可以使用 gcc -H 并进行大量分析和处理等来摆脱它。但是,线标记的标志元素会报告我是打开文件还是从文件返回,因此它包含有关嵌套包含的信息。这个信息在pycparser的某个地方吗?可以以某种方式简单地添加吗?

4

1 回答 1

0

pycparser理解#line指令并将它们合并到它为所有令牌跟踪的坐标中。例如,考虑这个文件:

int a;

int b;

我们可以转储它的 AST:

$ python examples/dump_ast.py --coord /tmp/file.c
FileAST:  (at None)
  Decl: a, [], [], [] (at /tmp/file.c:1:5)
    TypeDecl: a, [] (at /tmp/file.c:1:5)
      IdentifierType: [u'int'] (at /tmp/file.c:1:1)
  Decl: b, [], [], [] (at /tmp/file.c:3:5)
    TypeDecl: b, [] (at /tmp/file.c:3:5)
      IdentifierType: [u'int'] (at /tmp/file.c:3:1)

请注意,声明b具有 location /tmp/file.c:3:5,表示文件的第 3 行(和第 5 列)。

现在稍微修改文件为:

int a;

#line 90
int b;

并再次转储 AST:

$ python examples/dump_ast.py --coord /tmp/file.c
FileAST:  (at None)
  Decl: a, [], [], [] (at /tmp/file.c:1:5)
    TypeDecl: a, [] (at /tmp/file.c:1:5)
      IdentifierType: [u'int'] (at /tmp/file.c:1:1)
  Decl: b, [], [], [] (at /tmp/file.c:90:5)
    TypeDecl: b, [] (at /tmp/file.c:90:5)
      IdentifierType: [u'int'] (at /tmp/file.c:90:1)

看看 的位置发生了b什么?

于 2021-05-18T00:45:46.950 回答