[antlr-interest] Adding a Space Leads to Mismatch

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


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