ANTLR 4.5:行 1:22 不匹配的输入 'randomly' 期望方向
ANTLR 4.5: line 1:22 mismatched input 'randomly' expecting DIRECTION
以下语法无法与 Antlr4.5 和 Java 1.8.45(IDE:IntelliJ Ultimate 14.1.4)一起正常工作:
grammar PlayerAIShots;
file : row row EOF ;
row : START (randomshot)? SPACE direction Dot (LineBreak | EOF);
randomshot: RANDOM ;
direction : DIRECTION ;
RANDOM : 'randomly' ;
DIRECTION : ('to the left'|'to the right'|'central') ;
START : 'The opponent shoots' ;
SPACE : ' ' ;
Dot : '.' ;
// line break
LineBreak : '\r'?'\n' | '\r';
WS : [\t\r\n]+ -> skip ; // skip tabs, newlines
让生成的词法分析器和解析器被评估结果为:
line 1:22 mismatched input 'randomly' expecting DIRECTION
在使用的数据(文本文件)中,第二行已正确处理,但如上述错误消息所示,不是第一行。这是正在使用的文本文件:
The opponent shoots randomly to the left.
The opponent shoots to the right.
删除那些 SPACE 在 "row" 定义中的错误不会发生。为什么?
您输入的第一行有误。您没有像这样
指定在 START
和 randomshot
之间需要 space
row : START (SPACE randomshot)? SPACE direction Dot (LineBreak | EOF);
当您从 'row' 定义中删除 'SPACE' 时,您会得到更多错误
line 1:19 extraneous input ' ' expecting {'randomly', DIRECTION}
line 1:28 extraneous input ' ' expecting DIRECTION
line 2:19 extraneous input ' ' expecting {'randomly', DIRECTION}
经过多次尝试,这才是真正符合预期的行为:
row : START SPACE (RANDOM)? (SPACE)? direction Dot (LineBreak | EOF);
以下语法无法与 Antlr4.5 和 Java 1.8.45(IDE:IntelliJ Ultimate 14.1.4)一起正常工作:
grammar PlayerAIShots;
file : row row EOF ;
row : START (randomshot)? SPACE direction Dot (LineBreak | EOF);
randomshot: RANDOM ;
direction : DIRECTION ;
RANDOM : 'randomly' ;
DIRECTION : ('to the left'|'to the right'|'central') ;
START : 'The opponent shoots' ;
SPACE : ' ' ;
Dot : '.' ;
// line break
LineBreak : '\r'?'\n' | '\r';
WS : [\t\r\n]+ -> skip ; // skip tabs, newlines
让生成的词法分析器和解析器被评估结果为:
line 1:22 mismatched input 'randomly' expecting DIRECTION
在使用的数据(文本文件)中,第二行已正确处理,但如上述错误消息所示,不是第一行。这是正在使用的文本文件:
The opponent shoots randomly to the left.
The opponent shoots to the right.
删除那些 SPACE 在 "row" 定义中的错误不会发生。为什么?
您输入的第一行有误。您没有像这样
指定在START
和 randomshot
之间需要 space
row : START (SPACE randomshot)? SPACE direction Dot (LineBreak | EOF);
当您从 'row' 定义中删除 'SPACE' 时,您会得到更多错误
line 1:19 extraneous input ' ' expecting {'randomly', DIRECTION}
line 1:28 extraneous input ' ' expecting DIRECTION
line 2:19 extraneous input ' ' expecting {'randomly', DIRECTION}
经过多次尝试,这才是真正符合预期的行为:
row : START SPACE (RANDOM)? (SPACE)? direction Dot (LineBreak | EOF);