[antlr-interest] Adding a Space Leads to Mismatch

Quintin Beukes quintin.beukes at signio.co.za
Thu Feb 9 09:52:06 PST 2012


I've narrowed it down to:
ALPHANUMERICSPACE
  : ('a'..'z' | 'A'..'Z' | '0'..'9')+ (' '+ ('a'..'z' | 'A'..'Z' | '0'..'9')+)*
  ;

It seems when I remove the second grouping, it works with the space. As in:
ALPHANUMERICSPACE
  : ('a'..'z' | 'A'..'Z' | '0'..'9')+
  ;

Though I originally ended up with it as above because I had other
conflicts when I also have and use them as follows (no matter the
order they're defined):
ALPHANUMERIC
  : ('a'..'z' | 'A'..'Z' | '0'..'9')+
  ;

ALPHANUMERICSPACE
  : ('a'..'z' | 'A'..'Z' | '0'..'9' | ' ')+
  ;

Any suggestions as to how to solve this would be appreciated. I keep
running into such conflicts and must be ignorant of some concept.

Quintin Beukes

On Thu, Feb 9, 2012 at 7:10 PM, Quintin Beukes
<quintin.beukes at signio.co.za> wrote:
> Hi,
>
> Can someone please explain the following behavior to me. I've narrowed
> the problem down to a single space match, as described below.
>
> // Begin Grammar //
> grammar DebugA;
>
> fieldRules
>   : EVENTNAME ':' NEWLINE
>     rules
>     EOF
>   ;
>
> rules
>   : (rule)+
>   ;
>
> rule
>   : WS ALPHANUMERICSPACE NEWLINE
>     WS '(' ruleExpression '):' NEWLINE
>   ;
>
> ruleExpression
>   : 'If'
>   ;
>
> EVENTNAME
>   : 'OnChange'
>   | 'OnClick'
>   ;
>
> WS
>   : ('\t' | ' ')+
>   ;
>
> ALPHANUMERICSPACE
>   : ('a'..'z' | 'A'..'Z' | '0'..'9')+ (' '+ ('a'..'z' | 'A'..'Z' | '0'..'9')+)*
>   ;
>
> NEWLINE
>   : '\r'? '\n'
>   ;
> // End Grammar //
>
> // Begin Input //
> OnChange:
>  Rule Name
>  (If):
> // End Input //
>
> The above grammar and input matches perfectly.
>
> Though, if I change the last line of the input to:
>  (If ):
>
> And change the ruleExpression in the grammar to:
> ruleExpression
>   : 'If' WS
>   ;
>
> Then it doesn't match. Matching 'If ' works, and 'If' ' ' doesn't.
> I've tried numerous things and can't seem to get past this oddity.
>
> Quintin Beukes


More information about the antlr-interest mailing list