无关输入 '<EOF>' 期待 {' ', '} - Antlr4

Extraneous input '<EOF>' expecting {' ', '} - Antlr4

我有一个文本文件正在通过 antlr4 解析。

文本格式

                                                                        TEST OFFICE
                                                                        98 KINGS STREET
       DATE     18/05/22                                                FORT,ABC
       ABC HOTEL PLC
       CFA 843                                                         PAGE                1
       NO 40 DUTCH BUILDING
       PARK ROAD HALL
       KINGS STREET 01                                                ACCOUNT NO          5XX75YY200
       2128 100                                                         CURRENCY            USD
           BALANCE AS OF  17MAY22                                                                     1,115.75
   18MAY22 CLOSING BALANCE                                                                            1,115.75
           TOTAL DEPOSITS       0 ITEMS                                             .00
           TOTAL WITHDRAWALS    0 ITEMS                     .00

语法规则

// Define a grammar called CustomValidation
grammar CustomValidation;  

@header {
    package grammar ;
}

init : statement+ ;

statement : detail+ ;

detail : content+ ;

content : id ;

id : (WORD | NUMBER | SIGNS)* ('\r'|'\n')+ ;

WORD : LETTER+ ;

NUMBER : DIGIT+ ;

SIGNS : SIGN+ ;

WHITESPACE : ( '\t' | ' ' )+ -> skip ;

fragment LETTER : ('a'..'z' | 'A'..'Z') ;

fragment SIGN : ('.'|'+'|'('|')'|'/'|','|'-'|'&'|'\''|':'|'#'|'_'|'*'|';'|'%'|'@'|'"'|'`') ;

fragment DIGIT : ('0'..'9') ;

我明白了

Error -> line 36127:63 extraneous input '<EOF>' expecting {'
', '
', WORD, NUMBER, SIGNS}

我哪里做错了,我怎样才能使这些规则变得更好?

如果您从 init 规则开始,您的示例输入将得到正确解析:

String source = "                                                                        TEST OFFICE\n" +
    "                                                                        98 KINGS STREET\n" +
    "       DATE     18/05/22                                                FORT,ABC\n" +
    "       ABC HOTEL PLC\n" +
    "       CFA 843                                                         PAGE                1\n" +
    "       NO 40 DUTCH BUILDING\n" +
    "       PARK ROAD HALL\n" +
    "       KINGS STREET 01                                                ACCOUNT NO          5XX75YY200\n" +
    "       2128 100                                                         CURRENCY            USD\n" +
    "           BALANCE AS OF  17MAY22                                                                     1,115.75\n" +
    "   18MAY22 CLOSING BALANCE                                                                            1,115.75\n" +
    "           TOTAL DEPOSITS       0 ITEMS                                             .00\n" +
    "           TOTAL WITHDRAWALS    0 ITEMS                     .00\n";

CustomValidationLexer lexer = new CustomValidationLexer(CharStreams.fromString(source));
CustomValidationParser parser = new CustomValidationParser(new CommonTokenStream(lexer));
ParseTree root = parser.init();
System.out.println(root.toStringTree(parser));

打印:

(init (statement (detail (content (id TEST OFFICE \n)) (content (id 98 KINGS STREET \n)) (content (id DATE 18 / 05 / 22 FORT , ABC \n)) (content (id ABC HOTEL PLC \n)) (content (id CFA 843 PAGE 1 \n)) (content (id NO 40 DUTCH BUILDING \n)) (content (id PARK ROAD HALL \n)) (content (id KINGS STREET 01 ACCOUNT NO 5 XX 75 YY 200 \n)) (content (id 2128 100 CURRENCY USD \n)) (content (id BALANCE AS OF 17 MAY 22 1 , 115 . 75 \n)) (content (id 18 MAY 22 CLOSING BALANCE 1 , 115 . 75 \n)) (content (id TOTAL DEPOSITS 0 ITEMS . 00 \n)) (content (id TOTAL WITHDRAWALS 0 ITEMS . 00 \n)))))

看起来像这样缩进:

(init 
  (statement 
    (detail 
      (content 
        (id TEST OFFICE \n)) 
      (content 
        (id 98 KINGS STREET \n))
      (content 
        (id DATE 18 / 05 / 22 FORT , ABC \n))
      (content 
        (id ABC HOTEL PLC \n))
      (content 
        (id CFA 843 PAGE 1 \n)) 
      (content 
        (id NO 40 DUTCH BUILDING \n))
      (content 
        (id PARK ROAD HALL \n))
      (content 
        (id KINGS STREET 01 ACCOUNT NO 5 XX 75 YY 200 \n))
      (content 
        (id 2128 100 CURRENCY USD \n))
      (content 
        (id BALANCE AS OF 17 MAY 22 1 , 115 . 75 \n))
      (content 
        (id 18 MAY 22 CLOSING BALANCE 1 , 115 . 75 \n))
      (content 
        (id TOTAL DEPOSITS 0 ITEMS . 00 \n))
      (content 
        (id TOTAL WITHDRAWALS 0 ITEMS . 00 \n)))))

我猜你遇到的错误是由解析器生成的,该解析器生成的语法看起来与你现在发布的语法不同。每次对语法进行更改时,都需要让 ANTLR 生成新的词法分析器和解析器 类.