解析C头文件
我有一个使用 gcc -E 标志预编译的 C 头文件,现在尝试使用 Lex 和 Yacc 进行解析;然而,它被 typedef 变量所困扰。
例如:
typedef unsigned long ULONG;
ULONG i = 5;
将在第二行的 ULONG 处抛出语法错误。
我试图重新定义部分语法(在这里找到)http:// www.quut.com/c/ANSI-C-grammar-y.html,特别是在 type_specifer
下,将 TYPE_NAME
替换为 IDENTIFIER
>,但是这会创建多个我无法修复 s/r 和 r/r 错误。
您还有其他推荐的方法吗? 或者一起预编译的不同方法?
I have a C header file that I have precompiled using the gcc -E
flag, and am now trying to parse using Lex and Yacc; however, it is getting hung up on typedef'd variables.
for example:
typedef unsigned long ULONG;
ULONG i = 5;
will throw a syntax error at the second line's ULONG.
I have tried to redefine part of the grammar (found here) http://www.quut.com/c/ANSI-C-grammar-y.html, specifically under type_specifer
by replacing TYPE_NAME
with IDENTIFIER
, however that create multiple s/r and r/r errors that I am unable to fix.
Are there other approaches that you would recommend?
Or a different approach at precompiling all together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您也链接的代码中:
http://www.quut.com/c/ANSI-C-grammar -y.html,
看最下面:
你会看到这个函数
你实际上需要编写识别序列的代码(yytest,yytext + yylength]是一个标识符或一个TYPE_NAME。这意味着在解析器中你需要在解析代码时构建一些结构,以便这个函数可以在结构中查找标识符。
使用默认类型 char/int/short/long/float/double 等预填充结构。在解析输入时需要添加其他类型。
In the code you link too:
http://www.quut.com/c/ANSI-C-grammar-y.html,
look at the bottom:
You will see this function
You actually need to write the code that identifies weather a sequence (yytest, yytext+yylength] is an identifier or a TYPE_NAME. This means in the parser you need to build some structure as you parse the code so that this function can look up the identifier in the structure.
Pre-populate the structure with the default types char/int/short/long/float/double etc. The other types you will need to add as you parse the input.