[antlr-interest] NoSuchElementException

Mike Lischke mike at lischke-online.de
Thu Sep 6 07:10:31 PDT 2012


Justin,

your grammar came over in an ugly format...


Try something like this for lines instead:

SOURCE_LINE_: .* (NEWLINE_ | EOF);

Then your preprocess rule could go like this:

preprocess:
	line* EOF
;


ANTLR is clever enough to exclude the token after the Kleene operator from what the dot matches, which is very convenient.



> grammar Test;
> 
> 
> 
> options
> 
> {
> 
>                language=C;
> 
> }
> 
> 
> 
> 
> 
> // Parser rules
> 
> preprocess
> 
>                :               (line? NEWLINE_)* line? EOF
> 
>                ;
> 
> 
> 
> line
> 
>                :               PP_directive_
> 
>                |              SOURCE_LINE_
> 
>                ;
> 
> 
> 
> // Lexer rules
> 
> 
> 
> PP_directive_
> 
>                :               '#define'
> 
>                ;
> 
> 
> 
> NEWLINE_
> 
>                :               '\u000D'? '\u000A'
> 
>                |              '\u0085'
> 
>                |              '\u2028'
> 
>                |              '\u2029'
> 
>                ;
> 
> 
> 
> SOURCE_LINE_
> 
>                :               ~NEWLINE_+
> 
>                ;
> 
> 
> 
> 
> 
> So I have two questions. It seems to me that the tool should never
> crash, so is this an ANTLR bug? Secondly, there is clearly a problem
> with what I am trying to do here. Is it not possible to capture
> everything on a line (that is not a newline) as a token? Does anyone
> have a workaround?
> 
> 
> 
> Thanks,
> 
> 
> 
> - Justin
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Mike
-- 
www.soft-gems.net




More information about the antlr-interest mailing list