antlr4 grammar for splitting the number

≯℡__Kan透↙ 提交于 2019-12-24 18:17:22

问题


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')+ ;

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

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,

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

anyone, please help

thanks in advance


回答1:


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 ;


来源:https://stackoverflow.com/questions/47349729/antlr4-grammar-for-splitting-the-number

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