I created a sample grammer and generated java source codes and compile them and executed my sample grammer but I got an error like the below.
我创建了一个示例语法并生成了java源代码并对它们进行编译并执行了示例语法,但是我得到了如下所示的错误。
C:\devEnvironments\antlr>java org.antlr.v4.runtime.misc.TestRig mytest.RuleTest
column_spec -tree
aaa.bbb.ccc
^Z
line 1:0 no viable alternative at input 'aaa.bbb.ccc\r\n'
(column_spec aaa.bbb.ccc\r\n)
The following is my rule source code.
下面是我的规则源代码。
grammar RuleTest;
@header {
package mytest;
}
column_spec:
( ( schema_name DOT )? table_name DOT )? column_name ;
ID:
( 'A'..'Z' | 'a'..'z' | '_' | '$') ( 'A'..'Z' | 'a'..'z' | '_' | '$' | '0'..'9' )*
;
// identifiers --- http://dev.mysql.com/doc/refman/5.6/en/identifiers.html --------------
schema_name : ID;
table_name : ID;
engine_name : ID;
column_name : ID;
view_name : ID;
parser_name : ID;
index_name : ID;
partition_name : ID;
partition_logical_name : ID;
constraint_symbol_name : ID;
foreign_key_symbol_name : ID;
collation_name : ID;
event_name : ID;
user_name : ID;
function_name : ID;
procedure_name : ID;
server_name : ID;
wrapper_name : ID;
alias : ( AS_SYM )? ANY_STRING;
ANY_STRING:
~(' ')+;
DOT : '.' ;
AS_SYM : 'as';
WS : [ \t\r\n]+ -> skip ;
I don't know why '\r', '\n' characters are not skipped and why this error happens? I used antlr 4.4 version.
我不知道为什么不跳过'\r'和'\n'字符,为什么会发生这种错误?我使用了antlr 4.4版本。
1 个解决方案
#1
1
The ANY_STRING
lexer rule matches your entire input. Since that's long than the 3 characters matched by the ID
rule, it will always have precedence. You need to either remove that rule, or change it so it only matches one character (thus never the longest).
ANY_STRING lexer规则匹配您的整个输入。因为这比ID规则匹配的3个字符长,所以它总是具有优先级。您需要删除该规则,或者更改它,以便它只匹配一个字符(因此永远不会是最长的)。
ANY_STRING_CHAR : ~' ';
#1
1
The ANY_STRING
lexer rule matches your entire input. Since that's long than the 3 characters matched by the ID
rule, it will always have precedence. You need to either remove that rule, or change it so it only matches one character (thus never the longest).
ANY_STRING lexer规则匹配您的整个输入。因为这比ID规则匹配的3个字符长,所以它总是具有优先级。您需要删除该规则,或者更改它,以便它只匹配一个字符(因此永远不会是最长的)。
ANY_STRING_CHAR : ~' ';