我需要一些指导来编写语法来解析游戏 Aion 的日志文件。我决定使用 Antlr3(因为它似乎是一个可以完成这项工作的工具,而且我认为学习使用它对我有好处)。但是,我遇到了问题,因为日志文件的结构不完全。
我需要解析的日志文件如下所示:
2010.04.27 22:32:22 : You changed the connection status to Online.
2010.04.27 22:32:22 : You changed the group to the Solo state.
2010.04.27 22:32:22 : You changed the group to the Solo state.
2010.04.27 22:32:28 : Legion Message: www.xxxxxxxx.com (forum)
ventrillo: 19x.xxx.xxx.xxx
Port: 3712
Pass: xxxx (blabla)
4/27/2010 7:47 PM
2010.04.27 22:32:28 : You have item(s) left to settle in the sales agency window.
如您所见,大多数行都以时间戳开头,但也有例外。我想在 Antlr3 中做的是编写一个解析器,它只使用以时间戳开头的行,同时默默地丢弃其他行。
这是我到目前为止所写的(我是这些东西的初学者,所以请不要笑:D)
grammar Antlr;
options {
language = Java;
}
logfile: line* EOF;
line : dataline | textline;
dataline: timestamp WS ':' WS text NL ;
textline: ~DIG text NL;
timestamp: four_dig '.' two_dig '.' two_dig WS two_dig ':' two_dig ':' two_dig ;
four_dig: DIG DIG DIG DIG;
two_dig: DIG DIG;
text: ~NL+;
/* Whitespace */
WS: (' ' | '\t')+;
/* New line goes to \r\n or EOF */
NL: '\r'? '\n' ;
/* Digits */
DIG : '0'..'9';
因此,我需要一个示例,说明如何在不为没有时间戳的行生成错误的情况下对其进行解析。
谢谢!