ANTR3 set the number of accepted characters for a token

五迷三道 提交于 2019-12-14 04:20:51

问题


I have to create a Lexer which will accept for example an integer only if it has a maximum of 8 digits. Is here an alternative to do it rather than just writing it like this?

INTEGER : (DIG | DIG DIG | DIG DIG DIG | ...)

回答1:


This can be done using a Gated Semantic Predicates like this:

INTEGER
@init{int n = 1;}
  :  ({n <= 8}?=> DIGIT {n++;})+
  ;

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

Details about this kind of predicate, see: What is a 'semantic predicate' in ANTLR?



来源:https://stackoverflow.com/questions/10373620/antr3-set-the-number-of-accepted-characters-for-a-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!