用于拆分数字的antlr4语法 [英] antlr4 grammar for splitting the number

查看:75
本文介绍了用于拆分数字的antlr4语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

grammar Te;

/*
 * Parser Rules
 */

test : (example+ EOF);
example: digit COMMA digit2 NEWLINE; 
digit: (INT)+? ; 
digit2: (INT INT INT INT)+?; 

/*
 * Lexer Rules
 */ 

INT :[0-9]; 
COMMA: ','; 
NEWLINE : ('\r'? '\n' | '\r')+ ;

这是我写的语法,用于将数字序列考虑为一个数字,直到检测到COMMA,然后将数字序列考虑为4个数字

This is the grammar I have written for considering the number sequence into single digit until a COMMA is detected and afterwards consider the number sequence into 4 digits

例如,让我的输入为00000,12345678912345678912现在应该考虑00000并将其拆分为个位数,例如

for example, let my input be 00000,12345678912345678912 now it should consider 00000 and split it into single digits like

token 1 =0, 
token 2 =0,
token 3 =0,
token 4 =0, 
token 5 =0,

在COMMA之后,应考虑12345678912345678912并像

and after COMMA it should consider 12345678912345678912 and split like

token 1 =1234, 
token 2 =5678,
token 3 =9123,
token 4 =4567, 
token 5 =8912,

但是它并没有采用第二条规则并在COMMA之后将其打印为一位数字

but it's not taking the 2nd rule and printing it as single digits after COMMA

任何人,请帮助

预先感谢

推荐答案

您几乎完全正确.您犯的唯一错误是您要循环解析器规则而不是词法分析器规则.因此,您的语法应如下所示:

You almost got it right. The only error you made is that you want to loop the parser rule and not the lexer rule. Therefore your grammar should look like this:

example: digit+ COMMA digit2+ NEWLINE ;
digit: INT ;
digit2: INT INT INT INT ;

这篇关于用于拆分数字的antlr4语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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