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

James Ladd james_ladd at hotmail.com
Wed Jul 6 14:41:41 PDT 2011


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: ('~'|'!'|'@'|'%'|'&'|'*'|'-'|'+'|'='|'\\'|'|'|'?'|'/'|'>'|'<'|',') ('~'|'!'|'@'|'%'|'&'|'*'|'-'|'+'|'='|'\\'|'|'|'?'|'/'|'>'|'<'|',')*;

 		 	   		  


More information about the antlr-interest mailing list