SLX
SLX

Reputation: 33

Exclude tokens from Identifier lexical rule

I have Identifier lexical rule:

Identifier
:   ( 'a'..'z' | 'A'..'Z' | '_' ) ( 'a'..'z' | 'A'..'Z' | '_' | '0'..'9' )*
;

LogicalOr and LogicalAnd rules:

LogicalOr       :   '| ' | '||' | OR;
LogicalAnd      :   '&' | '&&' | AND;
fragment Or : '[Oo][Rr]';
fragment And : '[Aa][Nn][Dd]';

strings "and" and "or" are recognized as identifiers, instead of logicalAnd and logicalOr. Could someone help me to solve this problem please?

Upvotes: 0

Views: 339

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99999

There are two potential issues at play. First and foremost, ANTLR 3 does not support the character class syntax introduced by ANTLR 4. Your Or fragment literally matches the input [Oo][Rr]; it does not match OR, or, or oR. The same applies to your And fragment. You need to write the rule like this instead:

fragment
Or
  : ('O' | 'o') ('R' | 'r')
  ;

If this does not resolve your issue, then you need to make sure your LogicalOr and LogicalAnd rules are positioned before the Identifier rule in the grammar. The rule which appears first will determine what token type is assigned for this input sequence.

Upvotes: 1

Related Questions