我在我的ANTLR v3语法文件中定义了这个词法分析器规则 - 它用双引号计算文本 . 我需要将其转换为ANTLR v4 . ANTLR编译器抛出错误'语法错误:匹配词法分析器规则'(在@init行中)期望COLON不匹配输入'@' . 词法分析器规则是否包含@init块?这应该怎么改写?
DOUBLE_QUOTED_CHARACTERS
@init
{
int doubleQuoteMark = input.mark();
int semiColonPos = -1;
}
: ('"' WS* '"') => '"' WS* '"' { $channel = HIDDEN; }
{
RecognitionException re = new RecognitionException("Illegal empty quotes\"\"!", input);
reportError(re);
}
| '"' (options {greedy=false;}: ~('"'))+
('"'|';' { semiColonPos = input.index(); } ('\u0020'|'\t')* ('\n'|'\r'))
{
if (semiColonPos >= 0)
{
input.rewind(doubleQuoteMark);
RecognitionException re = new RecognitionException("Missing closing double quote!", input);
reportError(re);
input.consume();
}
else
{
setText(getText().substring(1, getText().length()-1));
}
}
;
样本数据:
-
" " - >抛出错误"Illegal empty quotes!";
-
"asd -> throws error "缺少收盘双重报价!“
-
"text" - >返回文本(有效输入,"..."的内容)
1 回答
当我的.g4语法导入lexer文件时,我反击了这个问题 . 导入语法文件似乎在ANTLR4中引发了许多未记录的缺陷 . 所以最终我不得不停止使用导入 . 在我的例子中,一旦我将LEXER语法合并到解析器语法(一个.g4文件)中,我的@input和@after解析错误就消失了 . 我应该提交一个测试用例错误,至少要记录下来 . 一旦我这样做,我会在这里更新 . 我含糊地回忆起关于将词法分析器语法导入我的解析器中引发无证行为的2-3个问题 . stackoverflow上有很多内容 .