使用ANTLR4导致无关的输入错误

时间:2013-10-20 10:45:51

标签: antlr

我是一名ANTLR新手,并且正在努力解决我遇到的一些错误。下面我已经包含了我正在使用的语法,输入文件和我得到的错误。

我的Antlr Grammar文件如下:

grammar Simple;

@header
{
package simple;
}

PARSER

program :
        anylinebefore+
        processline
        anylineafter+
        'MSEND' NEWLINE
         '.' EOF
        ;

anylinebefore:  CH* NEWLINE | commentline;

anylineafter:  statement | commentline;

statement: movestatement ; 

movestatement : 'MOVE' arg ('to' | 'TO')  ID '.' NEWLINE ;

arg : ID|STRING;

processline:  PROCESSLITERAL  NEWLINE; 

commentline:  '!' CH* NEWLINE;

LEXER

WS     : [ \t]+ -> skip ;
STRING  :  '\'' (~['])* '\'';
ID     : ('a'..'z'|'A'..'Z')+;
INT    : '0'..'9'+;
TO     : ('to' | 'TO');
CH      :       [\u0000-\uFFFE];
PROCESSLITERAL  :  'PROCESS SOURCE FOLLOWS';
NEWLINE   : '\r'? '\n'  ;

我的输入文件如下:

MODIFY   
         PROCESS SOURCE FOLLOWS  
MOVE 'WSFRED' TO AGRPASSEDTWO.   
         MSEND    
         .

我得到的错误是:

showtree:
     [java] line 1:0 extraneous input 'MODIFY' expecting {'!', CH, NEWLINE}

我不明白为什么这与语法中的 anylinebefore 不匹配 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

“MODIFY”是一个ID,与+之前的任何行都不匹配