Re: [antlr-discussion] how to handle `macro call` for C/C++/Objective-C language?

34 views
Skip to first unread message

Mike Lischke

unread,
Dec 14, 2016, 3:25:11 AM12/14/16
to antlr-di...@googlegroups.com

Some macros are defined in header files which I cannot get them. So if antlr scans and parses the source code, there are always 'NullPointerException' because macro cannot match any rules.

int print() {
   
char* label = "hello, the world";
   LOG
(label)          // `LOG` is macro which I can't find its definition, antlr will crash because there isn't ';' at end of line.
   return 0;
}

how to tell antlr to skip `LOG(label)`. 
I don't care the parsing result is right or wrong. I only want antlr to continue scanning the other parts of the file.

That's purely a matter of the grammar. If the grammar supports macro calls there will no problems to parse such input. However, usually macros are handled in a preprocessor step as "simple" text replacement actions.


Ivan Kochurkin

unread,
Feb 17, 2017, 8:15:20 AM2/17/17
to antlr-discussion
Actually, preprocessor directives processing is a hard challenge especially for token-based parser. I highlighted two approaches of directives processing in my article: Parsing Preprocessor Directives in Objective-C
In your case use can simply ignore LOG macro by the following way (see "Ignored Macros" section in my article):

LOG_MACRO: 'LOG(' ~')'* ')' -> channel(IGNORED_MACROS);


среда, 14 декабря 2016 г., 11:25:11 UTC+3 пользователь Mike Lischke написал:
Reply all
Reply to author
Forward
0 new messages