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

查看:22
本文介绍了用于拆分数字的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')+ ;

这是我写的语法,用于将数列考虑为一位数,直到检测到逗号,然后将数列考虑为 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,

在逗号之后应该考虑 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,

但它没有采用第二条规则并将其打印为逗号后的个位数

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天全站免登陆