1

我正在尝试解析一个可以包含表创建脚本或索引创建脚本的文件。
下面是语法。当我使用一些垃圾输入运行脚本规则时,create xyz table 我得到一个错误line 1:0 no viable alternative at input 'create' 但是当我运行 table_script 或 index_script 我得到特定的错误消息line 1:8 missing 'table' at 'xxxtab'

即使我将脚本作为丢失的表或索引运行......是否有可能得到相同的错误消息?

grammar DBScript;
options { output=AST; }
tokens {
    CREATE;
    TABLE;
    INDEX;

}

scripts
:
index_script | table_script
;

index_script
: create index index_name;


table_script
    : create table table_name ;

create
    : 'create';

table
    : 'table';

index
    : 'index';

table_name 
  :
   IDENT;

index_name 
  :
   IDENT;

fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INTEGER : DIGIT+ ;
IDENT : LETTER (LETTER | DIGIT)*;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};
4

1 回答 1

0

我相信从 ANTLR 3.5 开始,错误消息可能 具体。但是,没有完美的方法来报告所有类型的错误。我能给出的最好的提示是告诉你 ANTLR 最擅长报告(并从中恢复)出现在 LL(1) 决策中的错误,你可以通过具有更长前瞻要求的左因子规则来编写这些错误。

对于上面的示例,您可以将引用保留create如下。请注意,在左分解之后既不index_script也不table_script直接引用。create

scripts : create_script;
create_script : create (index_script | table_script);

index_script : index index_name;
table_script : table table_name;
于 2013-03-26T21:35:59.153 回答