0

I am coding a parser of expression and visualization of it, which means every step of the recursive descent parsing or construction of AST will be visualized like a tiny version of VisuAlgo

// Expression grammer
Goal -> Expr
Expr -> Term + Term
      | Expr - Term
      | Term
Term -> Term * Factor
      | Term / Factor
      | Factor
Factor -> (Expr)
      | num
      | name

So I am wondering what data structure can be easily used for storing every step of constructing AST and how to implement the visualization of every step of constructing AST. Well, I have searched some similar questions and implement a recursive descent parser before, but just can't get a way to figure this out. I will be appreciated if anyone can help me.

Bowen Peng
  • 1,635
  • 4
  • 21
  • 39

1 Answers1

0

This SO answer shows how to parse and build a tree as you parse

You could easily just the print the tree or tree fragments at each point where they are created.

To print the tree, just walk it recursively and print the nodes with indentation equal to depth of recursion.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341