下面的代碼將走AST和打印所有AST節點的標準錯誤。 同樣的樹步行者是可以替換樹節點的樹變換器的基礎。
分配新樹節點與:(pANTLR3_BASE_TREE)(PSR->的接頭> nilNode(PSR->適配器));
刪除AST節點與:parentASTnode-> DeleteChild,則(parentASTnode,nodeIndex); [deleteChild不釋放已刪除的節點]
將節點替換爲:parentASTnode-> replaceChildren(parentASTnode,nStartChildIndex,nStopChildIndex,newASTnode); [你不能在一個AST樹級別的中間插入節點,你只能更換節點或添加到父節點的孩子列表的末尾]
void printTree(pANTLR3_BASE_TREE t, int indent)
{
pANTLR3_BASE_TREE child = NULL;
int children = 0;
char * tokenText = NULL;
string ind = "";
int i = 0;
if (t != NULL)
{
children = t->getChildCount(t);
for (i = 0; i < indent; i++)
ind += " ";
for (i = 0; i < children; i++)
{
child = (pANTLR3_BASE_TREE)(t->getChild(t, i));
tokenText = (char *)child->toString(child)->chars;
fprintf(stderr, "%s%s\n", ind.c_str(), tokenText);
if (tokenText == "<EOF>")
break;
printTree(child, indent+1);
}
}
}
// Run the parser
pANTLR3_BASE_TREE langAST = (psr->start_rule(psr)).tree;
// Print the AST
printTree(langAST, 0);
// Get the Parser Errors
int nErrors = psr->pParser->rec->state->errorCount;
「我生成與ANTLR C中的AST ... 「?這與PLSQL樹有什麼關係,或者你只是告訴你設法做了兩次? – 2012-08-01 04:02:49
@IraBaxter:沒有說清楚,我使用ANTLR從grammer文件生成C目標,因爲我想在另一個C程序中使用這個解析器和重寫工具 – Ivan 2012-08-01 05:13:42