Ignore lexer rule after initial match

122 views Asked by At

thanks for taking a look at my question.

So I have the grammar and lexer rules that I use to parse user input on a grocery list. The grammar matches sentences such as '10 Pound beef' which has the tokens 'amount unit ware'. The ware token matches any valid unicode string but I cannot enter strings matched by the unit token as they are caught by the unit rule. So my question is, can i instruct my lexer to ignore the unit rule after the first match such that I can input '10 Pound Pound' with the tokens 'amount unit ware' without errors?

Grammar:

grammar Shopping;
import lexerrules;

parse   : item EOF ;
item    : (amount (SPACE* unit)? SPACE+)? ware | (unit (SPACE* amount)? SPACE+)? ware ;
ware    : STRING (SPACE+ STRING)* ;
amount  : NUM ;
unit    : UNIT ;

Lexer rules:

lexer grammar lexerrules;

NUM     : [0-9]+(('.'|',')[0-9]+)? ;
UNIT    : WEIGHT | LENGTH | VOLUME | MISC ;
STRING  : CHAR+ 
SPACE   : ' ' ;

WS      : [\u000C\f\t\r\n]+ -> skip ;

CHAR    : '\u0041' .. '\uFFFF' ;

WEIGHT  : [Kk]'g' | [Kk]'ilo' | [Kk]'ilogram' | [Gg] | [Gg]'ram' | 
          [Dd]'ecigram' |  [Oo]'unce' | [Oo]'z' | [Pp]'ound' | [Ll]'b' ;

LENGTH  : [Mm] | [Mm]'eter' | [Cc]'m' | [Cc]'entimeter' |
          [Ii]'nch' | [Ii][Nn] ;

VOLUME  : [Ll] | [Ll]'iter' | [Dd]'l' | [Dd]'eciliter' | [Cc][Ll] |
          [Cc]'entiliter' ;
0

There are 0 answers