简体   繁体   中英

Reluctant matching in ANTLR 4.4

Just as the reluctant quantifiers work in Regular expressions I'm trying to parse two different tokens from my input ie, for operand1 and operator. And my operator token should be reluctantly matched instead of greedily matching input tokens for operand1.

Example, Input:

Active Indicator in ("A", "D", "S")

(To simplify I have removed the code relevant for operand2)

Expected operand1:

Active Indicator

Expected operator:

in

Actual output for operand1:

Active indicator in

and none for the operator rule. Below is my grammar code:

grammar Test;

condition: leftOperand WHITESPACE* operator;

leftOperand:  ALPHA_NUMERIC_WS ;
operator: EQUALS | NOT_EQUALS | IN | NOT_IN;

EQUALS  : '=';
NOT_EQUALS  : '!=';
IN  : 'in';
NOT_IN  : 'not' WHITESPACE 'in';

WORD: (LOWERCASE | UPPERCASE )+ ;
ALPHA_NUMERIC_WS:    WORD  ( WORD| DIGIT | WHITESPACE )* ( WORD | DIGIT)+ ;
WHITESPACE  : (' ' | '\t')+;

fragment DIGIT: '0'..'9' ;

LOWERCASE   : [a-z] ;
UPPERCASE   : [A-Z] ;

One solution to this would be to not produce one token for several words but one token per word instead.
Your grammar would then look like this:

grammar Test;

condition: leftOperand operator;

leftOperand:  ALPHA_NUMERIC+ ;
operator: EQUALS | NOT_EQUALS | IN | NOT_IN;

EQUALS  : '=';
NOT_EQUALS  : '!=';
IN  : 'in';
NOT_IN  : 'not' WHITESPACE 'in';

WORD: (LOWERCASE | UPPERCASE )+ ;
ALPHA_NUMERIC:    WORD  ( WORD| DIGIT)* ;
WHITESPACE  : (' ' | '\t')+ -> skip; // ignoring WS completely

fragment DIGIT: '0'..'9' ;

LOWERCASE   : [a-z] ;
UPPERCASE   : [A-Z] ;

Like this the lexer will not match the whole input as ALPHA_NUMERIC_WS once the corresponding lexer rule has been entered because any occuring WS forces the lexer to leave the ALPHA_NUMERIC rule. Therefore any following input will be given a chance to be matched by other lexer-rules (in the order they are defined in the grammar).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM