我有一个ANTLR固定节点,而不是错误的节点产生,使用C靶Java解析器和它工作得很好。问题是我也希望它解析错误的代码并产生一个有意义的AST。如果我为它提供一个带有一个导入的最小Java类,之后缺少一个分号,它会生成两个“树错误节点”对象,其中导入类的令牌和令牌应该是。AST在ANTLR
但由于它正确地解析下面的代码,并产生正确的节点为这个编码它必须从错误通过将分号或通过重新同步恢复。有没有办法让antlr反映它在AST内部产生的固定输入?或者我可以至少得到产生“树节点错误”的令牌/文本?
在C目标 antlr3commontreeadaptor.c围绕线200下面的代码片段显示,C靶只创建虚拟错误节点至今:
static pANTLR3_BASE_TREE
errorNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_TOKEN_STREAM ctnstream, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken, pANTLR3_EXCEPTION e)
{
// Use the supplied common tree node stream to get another tree from the factory
// TODO: Look at creating the erronode as in Java, but this is complicated by the
// need to track and free the memory allocated to it, so for now, we just
// want something in the tree that isn't a NULL pointer.
//
return adaptor->createTypeText(adaptor, ANTLR3_TOKEN_INVALID, (pANTLR3_UINT8)"Tree Error Node");
}
难道我的运气在这里,只有错误的节点在Java目标产生将允许我检索错误节点的文本?
我认为你应该删除C标签,因为这似乎不是使用或理解C的问题。你应该添加解析器或解析标签。 – nategoose 2010-05-14 17:36:15