0
votes

I am new to ANTLR4 and would like to know the best way to push info from a lexer action to the parser.

I have the following:

LINE_DIRECTIVE: '#line' [ \t]+ INTEGER [ \t]+ STRING_LITERAL { System.out.println(getText()); skip(); }
          ;

Basically I would like to push the getText() to the parser.

Putting a parser-rule for the line-directive is not an option as such a line directive can happen everywhere...

Thanks for your help!

1

1 Answers

0
votes

Communication from lexer to parser is not possible in a way you want (if I understand you correctly).

What you could do is put your LINE_DIRECTIVE on another channel (and not skip the token!). There are the DEFAULT (or DEFAULT_CHANNEL) and HIDDEN channels, and you can introduce more of them 1. The parser only reads tokens from the DEFAULT channel, but you can change that at runtime 2.

1https://theantlrguy.atlassian.net/wiki/display/ANTLR4/Lexer+Rules#LexerRules-channel()

2Access Channels in ANTLR 4 and Parse them separately