即使使用预期的输入,Antlr4也会打印“外部输入"错误 [英] Antlr4 is printing 'Extraneous input' error even with expected input
问题描述
我正在尝试使用 OpenSMILES 规范.
语法:
grammar SMILES;
atom: bracket_atom | aliphatic_organic | aromatic_organic | '*';
aliphatic_organic: 'B' | 'C' | 'N' | 'O' | 'S' | 'P' | 'F' | 'Cl' | 'Br' | 'I';
aromatic_organic: 'b' | 'c' | 'n' | 'o' | 's' | 'p';
bracket_atom: '[' isotope? symbol chiral? hcount? charge? atom_class? ']';
symbol: element_symbols | aromatic_symbols | '*';
isotope: NUMBER;
element_symbols: UPPER_CASE_CHAR LOWER_CASE_CHAR?;
aromatic_symbols: 'c' | 'n' | 'o' | 'p' | 's' | 'se' | 'as';
chiral: '@'
| '@@'
| '@TH1' | '@TH2'
| '@AL1' | '@AL2'
| '@SP1' | '@SP2' | '@SP3'
| '@TB1' | '@TB2' | '@TB3' | DOT DOT DOT | '@TB29' | '@TB30'
| '@OH1' | '@OH2' | '@OH3' | DOT DOT DOT | '@OH29' | '@OH30';
hcount: 'H' | 'H' DIGIT;
charge: '-'
| '-' DIGIT
| '+'
| '+' DIGIT
| '--'
| '++';
atom_class:':' NUMBER;
bond: '-' | '=' | '#' | '$' | ':' | '/' | '\\';
ringbond: (bond? DIGIT | bond? '%' DIGIT DIGIT);
branched_atom: atom ringbond* branch*?;
branch: '(' chain ')' | '(' bond chain ')' | '(' dot chain ')';
chain: branched_atom
| chain branched_atom
| chain bond branched_atom
| chain dot branched_atom;
dot: '.';
DOT: .;
DIGIT: [0-9];
NUMBER: DIGIT+;
UPPER_CASE_CHAR: [A-Z];
LOWER_CASE_CHAR: [a-z];
ONE_TO_NINE: [1-9];
smiles: chain;
WS: [ \t\n\r]+ -> skip ;
当尝试使用AntlrWorks2的TestRig解析以下内容时:
When trying to parse the following using AntlrWorks2's TestRig:
CCc(c1)ccc2[n+]1ccc3c2Nc4c3cccc4
打印以下错误(为简便起见,以下简称):
The following error(s) are printed (shortened for brevity):
line 1:5 extraneous input '1' expecting {'*', '[', 'N', 'O', 'I', 'S', '%', ')',..., DIGIT}
...
line 1:31 extraneous input '4' expecting {<EOF>, '*', '[', 'N', 'O',..., DIGIT}
在字符串中遇到的每个数字都会发生这种情况.
This happens for every digit that is encountered in the string.
编辑1
按照@Lucas Trzesniewski的建议修复DOT
规则后,extraneous input
错误已消失.但是,现在在测试其他SMILES字符串时会出现一个新错误.
After fixing the DOT
rule, as suggested by @Lucas Trzesniewski, the extraneous input
error has disappeared. However, a new error is present now when testing a different SMILES string.
例如,测试:
[Cu+2].[O-]S(=O)(=O)[O-]
产生错误:
line 1:1 no viable alternative at input 'C'
编辑2
编辑1 中的问题是由于我的element_symbols
规则引起的.使用文字符号字符串似乎已经解决了该问题.
Problem from EDIT 1 was due to my element_symbols
rule. Using the literal symbol strings seems to have solved it.
element_symbols: 'H' | 'He' | 'Li' | 'Be' | 'B' | 'C' | 'N' | 'O' | 'F' | 'Ne' | //...and so on
推荐答案
您的词法分析器规则不正确.
Your lexer rules are wrong.
第一个错误:
DOT: .;
这是万能的.你真正的意思是:
This is a catch-all. What you really mean is:
DOT: '.';
第二个错误:您对以下规则感到困惑:
Second error: You're getting confused with the following rules:
DIGIT: [0-9];
NUMBER: DIGIT+;
ONE_TO_NINE: [1-9];
ONE_TO_NINE
永远不会匹配任何内容,因为它包含在DIGIT
中,并且DIGIT
首先出现.由于从未使用ONE_TO_NINE
规则,因此您只需删除它即可.
ONE_TO_NINE
will never match anything, because it's included in DIGIT
, and DIGIT
appears first. As the ONE_TO_NINE
rule is never used, so you should simply remove it.
然后,解析器规则中的DIGIT DIGIT
之类的东西将不匹配,如果您期望一个2位数的数字,那么您将在那里获得一个NUMBER
令牌,除非您用空格将数字分开(我不会不知道您在那儿的真正意思是什么,所以也许这不是一个错误.)
Then, things like DIGIT DIGIT
in your parser rules won't match either if you're expecting a 2-digit number, you'll get a NUMBER
token there unless you separate the digits with whitespace (I don't know what you really mean there so perhaps it's not an error).
这篇关于即使使用预期的输入,Antlr4也会打印“外部输入"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!