如何创建将解析日期的 antlr4 语法 [英] How to create a antlr4 grammar which will parse date

查看:24
本文介绍了如何创建将解析日期的 antlr4 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下 ANTLR4 语法解析一些日期格式.

I want to parse few date format using following ANTLR4 grammar.

grammar Variables;
//varTable : tableNameFormat dateFormat? ;
//tableNameFormat: (ID SEPERATOR);
dateFormat : YEAR UNDERSCORE MONTH UNDERSCORE TODAY
       | YEAR
       ;
YEAR : DIGIT DIGIT DIGIT DIGIT;                         // 4-digits YYYY
MONTH : DIGIT DIGIT;                                    // 2-digits MM
TODAY : DIGIT DIGIT ;                                     // 2-digits DD
UNDERSCORE: ('_' | '-' );
fragment
DIGIT : [0-9] ;
ID : [a-zA-Z][a-zA-Z0-9]? ;
WS  : [ \t\r\n]+ -> skip ;

这个语法应该很容易解析2016-01-01",但它给出了输入不匹配.请帮忙

This grammar should parse "2016-01-01" easily but it's giving input mismatch. Please help

推荐答案

对于这样的任务,正则表达式是更好的解决方案.但是如果你把它作为一个学习项目,这里是......

For such a task regex is much better solution. But if you have it as a study project, here it is...

重要的是要意识到词法分析器规则的顺序至关重要.输入将通过这些规则进行测试,并使用第一个适用的规则.应该从最具体的角度编写规则以避免冲突.例如,如果你有带有变量名和一些关键字的语法,那么关键字应该放在第一位,否则它们将被标记为变量.

It is important to realize that order of lexer rules is crucial. Input will be tested by these rules and the first applicable will be used. The rules should be written from the most specific to avoid conflicts. For example, if you have grammar with variable names and some keywords, keywords should be first otherwise they will be marked as variables.

有很多方法可以解决这个问题,但最好的方法是一个名为 DATE 的词法分析器规则:NUM NUM NUM NUM '-' NUM NUM '-' NUM NUM;您拥有的月和日规则将不起作用,因为它们是模棱两可的.词法分析器如何判断输入的两个数字是月还是日?

There are many ways you can solve this, but the best would be one lexer rule named DATE : NUM NUM NUM NUM '-' NUM NUM '-' NUM NUM; Month and Day rules as you have them wont work, as they are ambigous. How can lexer tell if two numbers input is month or day?

这篇关于如何创建将解析日期的 antlr4 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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