how to define a rule of a pattern repeated by a fixed number of times using antlr grammar -
i know '+', '?' , '*'. if want repeats for, say, 5 times? example, if identifier must string of hexdecimal numbers of length 5?
to more specific, i'm thinking define general lexer rule of unlimited length, , then, @ parsing time count how many time repeated, if equals 5, rename type of token, how can this? or there easy way?
at parsing time count how many time repeated, if equals 5, rename type of token, how can this? or there easy way?
yes, can disambiguating semantic predicate (explanation):
grammar t; parse : (short_num | long_num)+ eof ; short_num : {input.lt(1).gettext().length() == 5}? num ; long_num : {input.lt(1).gettext().length() == 8}? num ; num : '0'..'9'+ ; sp : ' ' {skip();} ;
which parse input 12345 12345678
follows:
but can change type of token in lexer based on property of matched text, this:
grammar t; parse : (short | long)+ eof ; num : '0'..'9'+ { if(gettext().length() == 5) $type = short; if(gettext().length() == 8) $type = long; // when length other 5 or 8, type of token stay num } ; sp : ' ' {skip();} ; fragment short : ; fragment long : ;
which cause same input parsed this:
Comments
Post a Comment