gpt4 book ai didi

c - ANTLR4 不解析 typedef

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:49 25 4
gpt4 key购买 nike

我正在使用 ANTLR4 和 C 的规范语法,如下所示:
https://github.com/antlr/grammars-v4/tree/master/c

我不会以任何方式修改语法。我发现对发生在函数外部的 typedef 语句的解析失败。我能给出的最简单的例子:

通行证:

main()
{
typedef unsigned int UINT;
return 0;
}

失败:

typedef unsigned int UINT;

main()
{
return 0;
}

错误信息是:

line 1:0 no viable alternative at input 'typedef'

我不知道 C 中有任何规则规定 typedef 必须出现在函数内部。这是 ANTLR C 语法中的错误,还是我做错了什么?

最佳答案

对我有用:


antlr4 C.g4
javac *.java
grun C compilationUnit -tree t.c
(compilationUnit (translationUnit (translationUnit (externalDeclaration (declaration (declarationSpecifiers (declarationSpecifier (storageClassSpecifier typedef)) (declarationSpecifier (typeSpecifier unsigned)) (declarationSpecifier (typeSpecifier int)) (declarationSpecifier (typeSpecifier (typedefName UINT)))) ;))) (externalDeclaration (functionDefinition (declarator (directDeclarator (directDeclarator main) ( ))) (compoundStatement { (blockItemList (blockItem (statement (jumpStatement return (expression (assignmentExpression (conditionalExpression (logicalOrExpression (logicalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (castExpression (unaryExpression (postfixExpression (primaryExpression 0))))))))))))))))) ;)))) })))) <EOF>)

ANTLR 版本问题? javac 和 antlr4 之间的类路径差异?

关于c - ANTLR4 不解析 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23039747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com