issue with pointer to user defiled types in C grammar

44 views
Skip to first unread message

sreeb...@gmail.com

unread,
Jan 5, 2025, 11:59:02 PMJan 5
to antlr-discussion
Hi Experts,

I see an issue in C grammar. We can define pointer to user defined typedef structures. Antlr considering it as multiplicative operation. It works fine for pointer to default data types. Could you suggest me on how to correct this?

Non working case:
(translator) ubuntu@ip-172-31-94-97:~/antlrc$ antlr4-parse C.g4 translationUnit -tree
int main() {
nginx *d;
}
(translationUnit:1 (externalDeclaration:1 (functionDefinition:1 (declarationSpecifiers:1 (declarationSpecifier:2 (typeSpecifier:4 int))) (declarator:2 (directDeclarator:8 (directDeclarator:1 main) ( ))) (compoundStatement:1 { (blockItemList:1 (blockItem:1 (statement:3 (expressionStatement:1 (expression:1 (assignmentExpression:1 (conditionalExpression:1 (logicalOrExpression:1 (logicalAndExpression:1 (inclusiveOrExpression:1 (exclusiveOrExpression:1 (andExpression:1 (equalityExpression:1 (relationalExpression:1 (shiftExpression:1 (additiveExpression:1 (multiplicativeExpression:1 (castExpression:2 (unaryExpression:1 (postfixExpression:1 (primaryExpression:1 nginx)))) * (castExpression:2 (unaryExpression:1 (postfixExpression:1 (primaryExpression:1 d))))))))))))))))) ;)))) }))))

working case:
(translator) ubuntu@ip-172-31-94-97:~/antlrc$ antlr4-parse C.g4 translationUnit -tree
int main() {
int *b;
}
(translationUnit:1 (externalDeclaration:1 (functionDefinition:1 (declarationSpecifiers:1 (declarationSpecifier:2 (typeSpecifier:4 int))) (declarator:2 (directDeclarator:8 (directDeclarator:1 main) ( ))) (compoundStatement:1 { (blockItemList:1 (blockItem:2 (declaration:1 (declarationSpecifiers:1 (declarationSpecifier:2 (typeSpecifier:4 int))) (initDeclaratorList:1 (initDeclarator:1 (declarator:1 (pointer:1 *) (directDeclarator:1 b)))) ;))) }))))
(translator) ubuntu@ip-172-31-94-97:~/antlrc$

Thanks
Bharat


Ken Domino

unread,
Jan 11, 2025, 10:28:17 AMJan 11
to antlr-discussion
The cause of the problem is because the grammar is ambiguous between `statement` and `declaration`. The "good" example avoids the ambiguity because basic types are hardwired into the grammar. Really, the grammar should have a basic symbol table. There are other possible hacks, but they will have varying unsavory assumptions.

$ trparse --ambig bad.c | trtree -a
CSharp 0 bad.c success 0.0608332
bad.c.d=116.a=1: (compilationUnit (translationUnit (externalDeclaration (functionDefinition (declarationSpecifiers (declarationSpecifier (typeSpecifier (Int "int")))) (declarator (directDeclarator (directDeclarator (Identifier "bad")) (LeftParen "(") (RightParen ")"))) (compoundStatement (LeftBrace "{") (blockItemList (blockItem (statement (expressionStatement (expression (assignmentExpression (conditionalExpression (logicalOrExpression (logicalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (castExpression (unaryExpression (postfixExpression (primaryExpression (Identifier "nginx"))))) (Star "*") (castExpression (unaryExpression (postfixExpression (primaryExpression (Identifier "d")))))))))))))))))) (Semi ";"))))) (RightBrace "}"))))) (EOF ""))
bad.c.d=116.a=2: (compilationUnit (translationUnit (externalDeclaration (functionDefinition (declarationSpecifiers (declarationSpecifier (typeSpecifier (Int "int")))) (declarator (directDeclarator (directDeclarator (Identifier "bad")) (LeftParen "(") (RightParen ")"))) (compoundStatement (LeftBrace "{") (blockItemList (blockItem (declaration (declarationSpecifiers (declarationSpecifier (typeSpecifier (typedefName (Identifier "nginx"))))) (initDeclaratorList (initDeclarator (declarator (pointer (Star "*")) (directDeclarator (Identifier "d"))))) (Semi ";")))) (RightBrace "}"))))) (EOF ""))

sreeb...@gmail.com

unread,
Jan 13, 2025, 5:49:46 AMJan 13
to antlr-discussion
Thanks for details. I will find a workaround.
Reply all
Reply to author
Forward
0 new messages