I want to extra all preprocess statement in C source file, and ignore all other statement. I'v tried add a last rule like Unknown : . -> skip ; // or -> channel(HIDDEN) ;in the lexer, or in the parser, add a last rule like:ignored : . ;, but it does not work.
Here is my grammar :
grammar PreProcessStatement;
pre_if_statement
: pre_if pre_elif* pre_else? pre_endif
;
pre_if : PreProcessBegin 'if' statement;
pre_endif : PreProcessBegin 'endif' ;
pre_else : PreProcessBegin 'else' ;
pre_elif : PreProcessBegin 'elif'statement ;
pre_define : PreProcessBegin 'define' statement;
pre_undef : PreProcessBegin 'undef'statement ;
pre_pragma : PreProcessBegin 'pragma'statement;
statement
: IDENTIFIER
| statement Condition statement
| '(' statement (Condition | Logic_or | Logic_and) statement ')'
| statement (Logic_or | Logic_and) statement
;
Logic_or
: '||'
;
Logic_and
: '&&'
;
PreProcessBegin : '#' ;
Condition : '==' | '>' | '>='| '<' | '<=' ;
NUM : INT | HEX ;
STRID : '"'ID'"' ;
IDENTIFIER : [a-zA-Z_0-9]+ ;
ID : [a-zA-Z_]+ ;
INT : [0-9]+ ;
HEX : '0x'INT;
WS : [ \t\n\r]+ -> skip ;
NewLine : ('\n' | '\r' | '\n\r');
MulLine : '\\' NewLine -> skip ;
Unknown : .*? -> skip ; // or -> channel(HIDDEN) ;
Input:
#if (test == ttt)
#elif rrrr
#else
aaa
#endif
Error:
line 4:0 extraneous input 'aaa' expecting '#'
I'v read the link below, does not work. Skipping unmatched input in Antlr
What's wrong with my grammar?