Skip to content
Olivier Duhart edited this page May 17, 2019 · 2 revisions

Viewing syntax tree

CSLY allow to get and visualize the concrete syntax tree after a parse. The ParseResult<IN,OUT> class expose the root of the syntax tree as a ISyntaxNode<IN>.

Compiling syntax tree to graphviz

ISyntaxNode<IN> can be compiled to a graphviz dot file using GraphVizEBNFSyntaxTreeVisitor<IN> (courtesy to DotNetGraph for .dot file generation)

var result = parser.Result.Parse("2 + 2 * 3");
var tree = result.SyntaxTree;
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ExpressionToken>();
var root = graphviz.VisitTree(tree);
string graph = graphviz.Graph.Compile();
File.Delete("c:\\temp\\tree.dot");
File.AppendAllText("c:\\temp\\tree.dot", graph);

the graph for this example is :

Syntax tree