Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ n-ary tree implementation for use in recursive descent parsing

I'm still a little new to C++ so bear with me. I'm implementing an interpreter for a hypothetical language called Core thats described by a BNF grammar. So far I've implemented a tokenizer that gives me a nice queue of tokens representing a Core program. I'm now in the process of writing the Parser/Executer which takes the output from the tokenizer and uses it to populate an object of the ParseTree class (which I have to design) using recursive descent parsing. I understand the fundamentals of how to do this but am having trouble implementing the ParseTree class. Productions described by the Core BNF usually have 2-5 terminal/nonterminal symbols but some may have up to 20 so I need an n-ary tree where each node can have a different number of children.

I suppose the ParseTree class doesn't necessarily need to use a tree for its implementation but that seems to make the most sense (Is there a different data structure that might be better/easier?). Im not aware of any container in STL that fits the bill for what I need. I've looked at the Boost property tree but from what I can tell that won't work either. I'd prefer not to reinvent the wheel and implement a tree from scratch if at all possible. Also, I'm limited by not being able to use any external libraries aside from Boost. What is the best way to implement my ParseTree? Are there any good pre-made tree implementations I could use?

like image 637
user1776041 Avatar asked Oct 26 '12 04:10

user1776041


1 Answers

I suggest using 'left child, right sibling' binary tree for representing the parse tree. It is a replacement of an n-ary tree. Any n-ary tree can be represented using the 'first child, next sibling' BINARY tree.

The concept is as follows: if A has three children: B, C and D and C has 2 children E and F as follows

              A
            / | \
           B  C  D
              /\
             E  F

this can be represented as

              A
             /
             B
              \
               C
              / \
             E   D
              \
               F

i.e. the children always go to the left node and the siblings to the right node. It is easy to build too and the pre-order traversal of this tree is same as the pre-order traversal of an n-ary tree.

n-ary tree pre-order traversal:

display (node, level) {
    if (!node) return;
    print node;
    display (node->left, level+1);
    display (node->right, level+1);
}

child sibling binary tree pre-order travesal

display (node, level) {
    if (!node) return;
    print node;
    display (node->left, level+1);
    display (node->right, level);
}

How to build this tree:

1. Throw your terminals and non-terminals in a Stack.
2. When you want to combine n nodes under parent node 'p', pop 'n' elements from stack, making the last pop as the right child of the current pop.
3. Finally make the nth pop the left child of node 'p'.
like image 106
aakash Avatar answered Nov 09 '22 09:11

aakash