LVParseTree_CreateIteratorBegin and

LVParseTree_CreateIteratorEnd

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

Parameter

Tree

Handle to a parse tree.

Example

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

  1. H_PARSE_TREE_ITR Itr;
  2. H_PARSE_TREE_ITR End;
  3. H_PARSE_TREE_NODE Node;
  4.  
  5. Itr = LVParseTree_CreateIteratorBegin(Tree);
  6. End = LVParseTree_CreateIteratorEnd(Tree);
  7.  
  8. while (!LVParseTree_Iterator_AreEqual(Itr,End))
  9. {
  10. H_PARSE_TREE_NODE Node = LVParseTree_Iterator_GetNode(Itr);
  11. for (int i = 0; i < LVParseTree_Node_GetLevel(Node); ++i) printf("\t");
  12. if (LVParseTree_Node_IsRule(Node))
  13. printf("$%s:\n",LV_ParseTree_Node_GetRuleName(Node));
  14. if (LVParseTree_Node_IsTag(Node))
  15. printf("{%s}\n",LVParseTree_Node_GetText(Node));
  16. if (LVParseTree_Node_IsTerminal(Node))
  17. printf("\"%s\"\n",LVParseTree_Node_GetText(Node));
  18. LVParseTree_Iterator_Advance(Itr);
  19. }
  20.  
  21. LVParseTree_Iterator_Release(Itr);
  22. LVParseTree_Iterator_Release(End);
  23.  
  24. // Note: Node handles don't get released; They are part of the tree,
  25. // 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:

See Also

© 2012 LumenVox LLC. All rights reserved.