0

Im currently building a Javascript compiler in ANTLR and JAVA. I use ANTLR's tree-grammar for generating ASTs. (Still in doubt whether this is smarter than a heterogeneous approach with a manually defined Abstract class for generating nodes, but that's another topic).

My problem is that when i have parsed some input, lets say, var x = 5; this is internally represented as; VARDECL as root and x as left child and 5 as right child. I now have the option to print this tree, using the toStringTree() command, which outputs (VARDECL x 5) - this representations gets quite hard to comprehend in larger programs, so i was wondering if there exists a third party tool that takes this textual tree-representation as input and can output a nice graphically model of the tree? (Or do i have to implement that as well)

Regards Sune.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Sune1987
  • 155
  • 2
  • 10

1 Answers1

0

Checkout this previous Q&A how to create a graphical tree of your AST using Graphviz' DOT language.

Just in case you're writing your own JavaScript grammar, have a look at the list of grammars on ANTLR wiki: there are many ECMA/JS grammars available that you can use.

Lastly, you may want to have a look at this previous Q&A where I posted an answer that shows how to evaluate a language (expressions, in this case) with a tree grammar using custom tree nodes. Of course, you'll have much more different nodes because the language is more complex (assignments, functions, scopes, etc.), but you could started with that example.

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288