C 预处理器的 ANTLR 语法

发布于 2025-01-03 10:03:53 字数 443 浏览 1 评论 0原文

我在antlr网站上下载了ac预处理器语法。但是它有一个错误,我不知道如何纠正它。

     macroExpansion  
: id=IDENTIFIER WS? LPAREN WS?   RPAREN -> ^(EXPAND $id)
| id=IDENTIFIER WS? LPAREN WS? macArgs  WS? RPAREN -> ^(EXPAND $id macArgs?)

规则宏扩展对于下面的代码会出错:

      typedef VOID (WINAPI *PFIBER_START_ROUTINE)( LPVOID lpFiberParameter );

因为VOID后面的标记将被视为参数,但实际上VOID只是一个宏而不是函数宏。

我怎样才能改变语法?希望有人能帮助我。谢谢!

I download a c preprocessor grammar on the antlr website.But it has an error and I have no idea about how to correct it.

     macroExpansion  
: id=IDENTIFIER WS? LPAREN WS?   RPAREN -> ^(EXPAND $id)
| id=IDENTIFIER WS? LPAREN WS? macArgs  WS? RPAREN -> ^(EXPAND $id macArgs?)

rule macroExpansion will go wrong for the code below:

      typedef VOID (WINAPI *PFIBER_START_ROUTINE)( LPVOID lpFiberParameter );

Because tokens following VOID would be considered as arguments,but in fact VOID is just a macro not a function marco.

How can I change the grammar?Hope anyone can help me.Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2025-01-10 10:03:53

由于规则必须以 IDENTIFIER 开头,后跟 LPAREN,因此我看不到它与 typedef VOID ( 等输入匹配,因为 >typedef 没有被考虑在内。

仅通过查看 macroExpansion

macroExpansion  
 : id=IDENTIFIER WS? LPAREN WS?   RPAREN -> ^(EXPAND $id)
 | id=IDENTIFIER WS? LPAREN WS? macArgs  WS? RPAREN -> ^(EXPAND $id macArgs?)
 ;                            //   ^                                   ^
                              //   |                                   |
                              //  not optional                        optional

我会对语法的其余部分有点怀疑:macArgs? 是可选的在重写规则中,但这不正确:左侧不是可选的。它可以像这样重写:

macroExpansion  
 : id=IDENTIFIER WS? LPAREN WS? (macArgs WS?)? RPAREN -> ^(EXPAND $id macArgs?)
 ;

在这种情况下 macArgs? 是正确的。

Since the rule must start with IDENTIFIER followed by a LPAREN, I can't see it ever match input like typedef VOID ( since the typedef isn't accounted for.

By only looking at the macroExpansion:

macroExpansion  
 : id=IDENTIFIER WS? LPAREN WS?   RPAREN -> ^(EXPAND $id)
 | id=IDENTIFIER WS? LPAREN WS? macArgs  WS? RPAREN -> ^(EXPAND $id macArgs?)
 ;                            //   ^                                   ^
                              //   |                                   |
                              //  not optional                        optional

I'd be a bit skeptical about the rest of the grammar though: the macArgs? is made optional in the rewrite rule, but that is not correct: the left hand side isn't optional. It could be rewritten like this:

macroExpansion  
 : id=IDENTIFIER WS? LPAREN WS? (macArgs WS?)? RPAREN -> ^(EXPAND $id macArgs?)
 ;

in which case macArgs? is correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文