Browse
 
Tools
Rss Categories

LVParseTree_CreateIteratorBegin and LVParseTree_CreateIteratorEnd

Reference Number: AA-00959 Views: 1470 0 Rating/ Voters

LVParseTree_CreateIteratorBegin and LVParseTree_CreateIteratorEnd provide iterators for visiting every node in the tree in a top-to-bottom, left-to-right descent. It is also the basis for the Tag and Terminal iterators.

Functions

  • H_PARSE_TREE_ITR LVParseTree_CreateIteratorBegin(H_PARSE_TREE Tree)

    H_PARSE_TREE_ITR LVParseTree_CreateIteratorEnd(H_PARSE_TREE Tree)

Parameter

Tree

Handle to a parse tree.

Example

The following code prints out every node in a parse tree.

C code

H_PARSE_TREE_ITR Itr;
H_PARSE_TREE_ITR End;
H_PARSE_TREE_NODE Node;

Itr = LVParseTree_CreateIteratorBegin(Tree);
End = LVParseTree_CreateIteratorEnd(Tree);

while (!LVParseTree_Iterator_AreEqual(Itr,End))
{
H_PARSE_TREE_NODE Node = LVParseTree_Iterator_GetNode(Itr);
for (int i = 0; i < LVParseTree_Node_GetLevel(Node); ++i) printf("\t");
if (LVParseTree_Node_IsRule(Node))
printf("$%s:\n",LVParseTree_Node_GetRuleName(Node));
if (LVParseTree_Node_IsTag(Node))
printf("{%s}\n",LVParseTree_Node_GetText(Node));
if (LVParseTree_Node_IsTerminal(Node))
printf("\"%s\"\n",LVParseTree_Node_GetText(Node));
LVParseTree_Iterator_Advance(Itr);
}

LVParseTree_Iterator_Release(Itr);
LVParseTree_Iterator_Release(End);

// Note: Node handles don't get released; They are part of the tree,
// and the tree releases them when it gets released

If the grammar was the top level navigation example grammar, and the engine recognized "go back", the the above code would print out:

  • $directive:
    • "go"
    • "back"
    • {$ = "APPLICATION_BACK"}

See Also