c++ - Pretty printing a parse tree to std out? -
i have written simple recursive descent parser in c++.
i need way print std out cannot figure out how this.
i have class node , has function printsymbol()
print symbol.
it has std::list <node*> m_children
children.
given this, how can pretty print parse tree std out?
thanks
add overload printsymbol
takes indent-level, or default value, either works:
void printsymbol(unsigned indent = 0) const { std::cout << std::string(indent,' ') << m_symbol << '\n'; (auto child : m_children) child->printsymbol(indent+2); }
given single node direct call printsymbol()
should output symbol, newline, , children if has any, indented. given root pointer should dump entire parse hierarchy stdout. can extraordinarily creative regarding ascii art, console-dependent line chars if you're set on it, can tedious quickly, warn you.
regardless, should @ least give picture can print. either or utterly misunderstood question.
best of luck
Comments
Post a Comment