Antlr中的Semantic Predicates

有些时候由于Antlr不是真正的LL(K)算法,会产生二义性的代码,这时需要我们手工来编写LookAhead的判断语句。这就用到了Semantic Predicates

 

语法结构为{XXXX}?,示意如下

create_stmt
options{
//generateAmbigWarnings=false;
}
:
{((LA(2) == SQL2RW_temp || LA(2)==SQL2RW_temporary) && LA(3)==SQL2RW_table) || (LA(2)==SQL2RW_table)}? create_table_stmt
|{((LA(2) == SQL2RW_temp || LA(2)==SQL2RW_temporary) && LA(3)==SQL2RW_view) || (LA(2)==SQL2RW_view)}? create_view_stmt
|{((LA(2) == SQL2RW_temp || LA(2)==SQL2RW_temporary) && LA(3)==SQL2RW_trigger) || (LA(2)==SQL2RW_trigger)}? create_trigger_stmt
|create_index_stmt
;