[antlr-interest] Lines that don't match a rule ...

Jim Idle jimi at temporal-wave.com
Wed Jul 6 16:30:52 PDT 2011


Try:

lines
  : (messagePattern LINE_BREAK)* EOF
  ;

messagePattern
  : ('+' | '-') (unarySelector | keywordSelector | binarySelector)
  | { while (input.LA(1) != LINE_BREAK && input.LA(1) != EOF)
input.consume(); }
  ;

This will just eat the lines that don't


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of James Ladd
> Sent: Wednesday, July 06, 2011 2:42 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Lines that don't match a rule ...
>
>
> Hi All,
>
> I hope I can get a little help on the grammar I have below.
> It works to a degree but I'm having trouble with some edge cases and
> that is stressing me a little.
>
> When I write something like the following I get the right response -
> sequence of tokens.
>
> Im putting '\n' where I would have new lines.
>
> \n
> + foo\n
> \n
>
> What I can't seem to do is put in a rule that matches a line that
> doesn't start with '+' or '-'
> I get a no viable Alt Exception with all that I try.
>
>
> My goal is to be able to detect lines starting with a '+' or a '-'
> followed by a messagePattern.
> And lines NOT starting with a '+' or a '-'.  The grammar below does not
> contain any rules for this, because I can't get them to work.
>
> Please help
>
> Rgs, James.
>
>
> grammar Preprocessor;
>
> options {
>   language = Java;
> }
>
> @header {
>   package compiler;
>
> }
>
> @lexer::header {
>   package compiler;
> }
>
> lines
>   : (lineBreak | messagePattern)* EOF
>   ;
>
> messagePattern
>   : ('+' | '-') (unarySelector | keywordSelector | binarySelector)
>
>   ;
>
> unarySelector
>   : NAME
>   ;
>
> keywordSelector
>   : (KEYWORD NAME)+
>   ;
>
> binarySelector
>   : BINARY_SYMBOL NAME
>   ;
>
> lineBreak
>   : LINE_BREAK  ;
>
> LINE_BREAK: ('\r'? '\n' | '\r');
>
> NAME: ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9')*;
> KEYWORD: NAME':';
> BINARY_SYMBOL: ('~'|'!'|'@'|'%'|'&'|'*'|'-
> '|'+'|'='|'\\'|'|'|'?'|'/'|'>'|'<'|',') ('~'|'!'|'@'|'%'|'&'|'*'|'-
> '|'+'|'='|'\\'|'|'|'?'|'/'|'>'|'<'|',')*;
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list