1

I'm using ANTLR3 (C runtime) to parse a text file: The problem is that I usually want to recognize newline characters, but at some points in the grammar I want to ignore them.

My first approach was to dynamically from the grammar set a boolean value that was used in the lexer rule for NEWLINE to set its channel to HIDDEN or DEFAULT.

That didn't work as ANTLR3 first lexes all the tokens and builds the tokenstream, and then the grammar walks the stream.

Now I'm wondering if it's possible to dynamically (using parser grammar predicates) tell the tokenstream to start/stop listening to a specific token channel in addition to the default one like this:

NEWLINE: ('\r'? '\n')+ {$channel=CH_WSPACE;}

expr
@init {
INPUT.disableChannel(CH_WSPACE);
}
@after {
INPUT.enableChannel(CH_WSPACE);
}  : 
...rhs of the rule....
;

Do I need to write my own TokenStream? Is there a better approach to my problem?

Thanks a lot in advance!

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Mike
  • 71
  • 8
  • 1
    A related question: http://stackoverflow.com/questions/5741829/how-do-i-get-an-antlr-parser-rule-to-read-from-both-default-and-hidden-channel However, the answer uses some specific OO features (probably) not available in C. If this doesn't help, could you explain a bit more about the language you're trying to parse? (not a *dumbed*-down version, but actual input) I, or someone else might have another approach than you have it in mind. – Bart Kiers Apr 12 '12 at 12:04
  • I would suggest joining the ANTLR forum, see reference in ANTLR tag info, and ask Jim Idle personally. He created the C target version and is usually there. – Guy Coder Apr 18 '12 at 22:09

0 Answers0