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

查看:370
本文介绍了如何创建将解析日期的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;月份和日期规则不起作用,因为它们含糊不清. lexer如何分辨输入的两个数字是月还是日?

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