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

查看:18
本文介绍了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天全站免登陆