2
votes

I am using ANTLR4 and the canonical grammar for C as found here:
https://github.com/antlr/grammars-v4/tree/master/c

I am not modifying the grammar in any way. I am finding that the parsing is failing on typedef statements that occur outside of functions. The simplest example I can give:

PASSES:

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

FAILS:

typedef unsigned int UINT;

main()
{
    return 0;
}

The error message is:

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

I am not aware of any rule in C that says typedefs must appear inside functions. Is this a bug in the ANTLR C grammar, or am I doing something wrong?

1

1 Answers

1
votes

Works for me:

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 version issue? classpath diff between javac and antlr4?