How to get the the grammar from the parsetree in Rascal?

143 Views Asked by At

I am pretty new to this, but I have started coding Rascal and I have been stuck for a long time. I am trying to get the grammar of a parse tree, but I can't seem to manage to retrieve it.

You know the yellow boxes with grammars when you hover your mouse above the parse tree here? http://tutor.rascal-mpl.org/Recipes/Visualization/ParseTree/ParseTree.html#/Recipes/Visualization/ParseTree/ParseTree.html

I want to somehow get that. I am trying to visualize the parsetree on html. And I somehow think it is best to get the grammar and then draw the tree into html myself, or is there a better way of doing it?

Any help is appreciated!!

1

There are 1 best solutions below

0
On BEST ANSWER

Every parse tree node carries a representation of every grammar rule used. In the library module Parsetree you can find the definition of the Tree datatype. Each parse tree is of this type. Each appl term has a Production and a list of children of type list[Tree].

Using pattern matching or field projection you can get at this data and draw your tree.

You can find example code in vis::ParseTree.

Also handy is the module lang::rascal::format::Grammar which has functions to pretty print grammar rule back in Rascal notation.