当规则失败时,动作仍然执行,为什么?
有我的测试:
语法测试;
选项{
language = C;
}
@parser ::包括
{
#include< iostream>
}
@lexer ::包括
{
#include< iostream>
}
RSCRIPT
:INT LR RR {std :: cout<< "行动奔跑...... \ n&#34 ;;};
INT:
(' 0' ..' 9&#39)+;
LR:' {&#39 ;;
RR:'}&#39 ;;
主要乐趣:
int main()
{
char teststr1 [] =" 111dd}&#34 ;;
pANTLR3_INPUT_STREAM输入;
input = antlr3StringStreamNew((pANTLR3_UINT8)teststr1,ANTLR3_ENC_8BIT,sizeof(teststr1) - 1,(pANTLR3_UINT8)" -memory - ");
pTestLexer Lex = TestLexerNew(输入);
pANTLR3_COMMON_TOKEN_STREAM tstream = antlr3CommonTokenStreamSourceNew(
ANTLR3_SIZE_HINT,TOKENSOURCE(Lex));
pTestParser psr = TestParserNew(tstream);
PSR-> RSCRIPT(PSR);
int ParserErrorCount = psr-> pParser-> rec-> state-> errorCount;
int LexerErrorCount = Lex-> pLexer-> rec-> state-> errorCount;
std :: cout<< "解析器错误计数:" << ParserErrorCount<< " \ n&#34 ;;
std :: cout<< " Lexer错误计数:" << LexerErrorCount<< " \ n&#34 ;;
的getchar();
返回0;
}
输出:
-memory-(1):lexer错误3:
在偏移4处,靠近' d' :
DD}
-memory-(1):lexer错误3:
在偏移5处,靠近' d' :
d}
-memory-(1):错误10:org.antlr.runtime.MissingTokenException,偏移量为5
接近[索引:0(开始:0-停止:0)='<缺失LR>',键入< 5>线路:1 LinePos:5]
:缺少LR
行动跑......
分析器错误计数:1
Lexer错误数:2
我的问题有两个:
我无法理解为什么规则匹配失败,但操作仍在运行?
如何在第一次错误捕获(词法分析器或解析器错误)时退出解析器,我不知道这个,请帮忙。
提前致谢。