ANTR3设置令牌可接受的字符数 [英] ANTR3 set the number of accepted characters for a token

查看:49
本文介绍了ANTR3设置令牌可接受的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个词法分析器,该词法分析器仅在最大8位数字的情况下才接受整数.是这里的替代方法,而不仅仅是这样写吗?

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 | ...)

推荐答案

这可以使用门控语义谓词完成,例如:

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

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

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

有关此类谓词的详细信息,请参见:什么是语义谓词"在ANTLR中?

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

这篇关于ANTR3设置令牌可接受的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆