[antlr-interest] antlr should throw NoViableAltException

David Holroyd dave at badgers-in-foil.co.uk
Sun Apr 15 13:53:16 PDT 2007


On Sun, Apr 15, 2007 at 10:51:50PM +0800, femto gary wrote:
> I want to openLineBreakChannel in if_expression,
> {lexer.openLineBreakChannel();}, so after if boolean_expression
> the line_break or 'then'|':' are mandatory, not skip() or channel HIDDEN,
> but I've tried this, found nothing happened.
> it seems Lexer all parse out token stream then handle it to parser, so
> parser can't affect lexer thru call to
> lexer.openLineBreakChannel();

Just in case you've not already read the warnings: controlling the lexer
from the parser is hard, in general.  First, there's the TokenStream
implementation sitting between the parser and lexer.  Second, the parser
may have already see several tokens past the 'current' position in the
token stream, due to lookahead.

As you've discovered, the ANTLR CommonTokenStream tokenizes the entire
input at the start or parsing, so when your method runs, the lexer is
already at EOF.  

You might be able to make your approach work if you poke around in the
guts of the CommonTokenStream implementation; some combination of
input.get(i) and input.rewind(i) to find and modify the LINE_BREAK,
perhaps?


ta,
dave

-- 
http://david.holroyd.me.uk/


More information about the antlr-interest mailing list