ANTLR4解析不适用于重复文本

时间:2017-11-27 10:40:32

标签: java parsing antlr antlr4

我已经成功解析了文件信息,直到使用给定语法的“要最终确定...”行。但是一旦我们在“待定稿......”之后收到更多数据,解析器就会跳过1-3条记录,并且在输出中给出第一个标题和最后2条记录(4-5)。

问题:如何更改语法,以便能够解析所有数据? 非常感谢您对以下问题的了解:

解析文字:(这里的每个RECORD都是Java中的地图)

+++    AV_AW_GER_1111_SpringShop        2017-10-09 12:00:00
O&M    #58513
%%/*123456*/FUN TEST:REC=ALL,SD=2017&10&02,ST=03&01&01,CLRSD=2017&10&09,CLRST=08&10&00;%%
RETCODE = 0  Operation succeeded

1RECORD  11111       Creat     Warning         KRAN      14758    Signaling
     Sync serial No.  =  121212
          Record name  =  Cell PCI Conflict
   Record raised time  =  2017-11-14 12:15:41
       Place info  =  Local Cell ID=1, Cell Name=AO_5655_56551_L1_B, eNodeB ID=15655, Cell ID=1, NB-IoT Cell Flag=FALSE

2RECORD  22222       Creat     Major           KRAN      52896    Environment
     Sync serial No.  =  231123
          Record name  =  Intruder Record
   Record raised time  =  2017-11-14 13:00:09
       Place info  =  Cabinet No.=0, Subrack No.=0, Slot No.=19, Port No.=2, Board Type=UPEU

3RECORD  33333       Creat     Major           KRAN      36369    Environment
     Sync serial No.  =  340434
          Record name  =  Intruder Record
   Record raised time  =  2017-11-14 12:58:40
       Place info  =  Cabinet No.=0, Subrack No.=0, Slot No.=19, Port No.=2, Board Type=UPEU
(Number of results = 3)

To be finalized...

---    END

+++    QW_ER_TY_2222_SpringPool       2017-11-20 13:29:34
O&M    #89874
%%/*012567*/FUN TEST:REC=ALL,SD=2017&10&02,ST=03&01&01,CLRSD=2017&10&09,CLRST=08&10&00;%%
RETCODE = 0  Operation succeeded

4RECORD  55555       Creat     Warning         KRAN      14736    Signaling
     Sync serial No.  =  45585
          Record name  =  Cell PCI Conflict
   Record raised time  =  2017-11-14 12:15:41
       Place info  =  Local Cell ID=1, PCI Value=408, Conflict Type=Confusion, NB-IoT Cell Flag=FALSE 
       Result type  =  Normally cleared

5RECORD  44444       Creat     Major           KRAN      65137    Environment
     Sync serial No.  =  99999
          Record name  =  Intruder Record
   Record raised time  =  2017-11-14 12:58:40
       Place info  =  Cabinet No.=0, Subrack No.=0, Slot No.=19, Port No.=2, Board Type=UPEU
        Result type  =  Normally cleared
(Number of results = 2)

2 reports in total

---    END

语法:

rspAlarm:
      alarmResponse alarmResponse1 EOF;
alarmResponse:
   header recordAlarm+ (COMPLEX_FLAG EOL)? TERMINATOR EOL;
alarmResponse1:
      header recordAlarm+ TERMINATOR EOL;
recordAlarm:
     completionStatus|(title SPACE* EOL)|(SPACE* keyValue '=' SPACE* value EOL)|TOTAL|EOL ;
header:
   connectInfo server_code FUNCTION EOL responseIdentification;
connectInfo:
     '+++' SPACE+ STRING (SPACE STRING|NUMBER)? SPACE+ timeStamp EOL*;
timeStamp:
    DATE EOL;
server_code:
    'O&M' SPACE+ HUSH NUMBER+ EOL;
responseIdentification:
        'RETCODE' SPACE '=' SPACE codeStatus SPACE+ completionStatus;
codeStatus:
    NUMBER;
completionStatus:
    COMPLETION_CODE EOL;
title:
    (STRING|NUMBER) (SPACE+ (STRING|NUMBER))*;
keyValue:
    ((STRING|NUMBER) SPACE*)+;
value:
     ( (STRING|NUMBER|DATE|HUSH) SPACE*)+;

NUMBER:('-')?[0-9]+;
DATE:NUMBER+'-'NUMBER+'-'NUMBER+SPACE+NUMBER+':'NUMBER+':'NUMBER+;
STRING:[a-zA-Z0-9\-\'\&\_\@\=\.\:\,\(\)\/]+;
FUNCTION:[\%][\%](.*)[\%][\%];
COMPLETION_CODE:('Success'|'Operation succeeded.'|'No record exists'|'Invalid command,it is inexecutable.'|) EOL;
TOTAL:'(Number of results = ' NUMBER ')';
COMPLEX_FLAG:('To be finalized...');
TERMINATOR :('---    END');
DASH:[\-]+;
PLUS:[\+][\+][\+];
EOL:'\r'?'\n';
SPACE:' ';
HUSH:[\#];
WS:('\t'|'\n'|'\r')->skip;

输出:

+++    AV_AW_GER_1111_SpringShop        2017-10-09 12:00:00
O&M    #58513
%%/*123456*/FUN TEST:REC=ALL,SD=2017&10&02,ST=03&01&01,CLRSD=2017&10&09,CLRST=08&10&00;%%
RETCODE = 0  Operation succeeded

And Record 4 and 5 + details after. 

解析器是skippinig Record 1-3,但所有文件都已被读取。

1 个答案:

答案 0 :(得分:0)

我认为令牌"功能"消耗所有记录,在您的情况下,您应该使用非贪婪的规则,尝试以下:

FUNCTION:[\%][\%](.*?)[\%][\%];

记下问号