質問
私は 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;
}
エラーメッセージは次のとおりです。
行 1:0 入力 'typedef' に実行可能な代替手段がありません
typedef が関数内に存在しなければならないという C の規則を私は知りません。これは 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の間のクラスパスの違いは何ですか?