Antlr C解析器能否从无效令牌中恢复
我开发了一个Antlr3.4,语法生成了一个AST,供以后解析。生成的解析器使用Antlr的C接口。当解析器遇到意外的标记时,它会添加 “树错误节点”连接到AST令牌流,并继续处理输入。(内部“树错误节点”表示ANTLR3_令牌_无效。) 当我将解析器的输出传递给AST解析器时,它会在Antlr C解析器能否从无效令牌中恢复,c,antlr,C,Antlr,我开发了一个Antlr3.4,语法生成了一个AST,供以后解析。生成的解析器使用Antlr的C接口。当解析器遇到意外的标记时,它会添加 “树错误节点”连接到AST令牌流,并继续处理输入。(内部“树错误节点”表示ANTLR3_令牌_无效。) 当我将解析器的输出传递给AST解析器时,它会在“树错误节点”上停止。在AST流中是否存在处理无效令牌的方法 我正在使用: libantlr3c-3.4 antlr3.4 我发现您可以重写树适配器方法“errorNode”来发出用户指定的令牌。然后可以在AS
“树错误节点”
上停止。在AST流中是否存在处理无效令牌的方法
我正在使用:
- libantlr3c-3.4
- antlr3.4
我发现您可以重写树适配器方法“errorNode”来发出用户指定的令牌。然后可以在AST解析器中处理该令牌。您需要使用上述方法覆盖Match(),并执行解析器的恢复(这是c#伪代码): 此外,您还需要从不匹配的令牌恢复:
protected override object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow)
{
if (needs recover)
{
if (unwanted token(input, ttype))
{
.. go to the next valid terminal
.. consume as if ok
.. return next valid token
}
if (missing token(input, follow))
{
.. go to the next valid terminal
.. insert missing symbol and return
}
.. othwerwise throw
}
.. call base recovery(input, ttype, follow);
}
如果还有其他问题,请告诉我
protected override object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow)
{
if (needs recover)
{
if (unwanted token(input, ttype))
{
.. go to the next valid terminal
.. consume as if ok
.. return next valid token
}
if (missing token(input, follow))
{
.. go to the next valid terminal
.. insert missing symbol and return
}
.. othwerwise throw
}
.. call base recovery(input, ttype, follow);
}