dlang-community / Pegged

A Parsing Expression Grammar (PEG) module, using the D programming language.
534 stars 66 forks source link

Generate GraphViz compatible data file #250

Closed ghost closed 5 years ago

ghost commented 6 years ago

Possible (?) enhancement

veelo commented 6 years ago

Can you elaborate? You want to generate a graph? Of what exactly?

rikkimax commented 6 years ago

My example of XML (using dot from Graphviz):

digraph ComplexGraph {
    nodesep=.05;
    rankdir=LR;
    node[shape=record,width=.1,height=.1];
    subgraph g_Document {
        n_Document[fontsize=18, shape=ellipse, label="Document"];
        n_Document -> n_Document_opts
        n_Document_opts [shape=record, label="Option sets | <optset0> 0"];
        node [width=1.5];
        n_Document_optset0[shape=record, label="{<opt0> Prolog | <opt1> emit Element | <opt2> emit Misc*}"];
        n_Document_opts:optset0 -> n_Document_optset0;
        n_Document_optset0:opt0 -> n_Prolog;
        n_Document_optset0:opt1 -> n_Element;
        n_Document_optset0:opt2 -> n_Misc;
    }
}

xmlrulegraph

ghost commented 6 years ago

Of the grammar. The picture above is what inspires the request.

Aphexus commented 5 years ago

Maybe you can use something like

version (tracer) { bool cond(string ruleName, const ref ParseTree p) { static startTrace = false; if (ruleName.startsWith("MyGrammar.RuleOfInterest")) startTrace = true; return startTrace && ruleName.startsWith("MyGrammar"); } setTraceConditionFunction(&cond); }

and just output the specific graphviz node information require(from just graphing the node names to also including the expressions and maybe sub-graphs of them)?

It would be pretty cool to be able visualize a grammar. Probably help a lot with design and bugs.

Also, I didn't know D could output files at compile time, but

"This will create the arithmetic.d file in the current directory, with the necessary infrastructure."

(maybe this is runtime?)

So, could it create a png at compile time using graphviz? Sort of automate the process for quick grammar visualization.

ghost commented 5 years ago

At least just generate the script file with edges and nodes.

veelo commented 5 years ago

Maybe you can use something like

version (tracer)

Unless I am misunderstanding, no. A trace produces a log of the parse (rules tried), not of the grammar. It is specific for a particular input. Logs also quickly grow into the GBs for any non-trivial case, much too big to represent in a graph.

But the problem is not all that hard. Producing a dotfile generator to represent any Pegged grammar as a graph should be pretty easy for any Pegged user that is familiar with the dot format and knows how he/she wants a grammar to be represented graphically. The grammar of all Pegged grammars is right here, just use Pegged for what it is meant to be used for :-)

Aphexus commented 5 years ago

Yeah, I didn't realize it would only trace out to a failure.

It could be used though to display the trace in a more visual format to help debug. Would probably be very similar code.

ghost commented 5 years ago

Not at all. The typical usage is to have a graphical representation of a grammar, e.g the documentation of a programming language. For example in Object Pascal doc:

ghost commented 5 years ago

I'll rather do my own tool. I think that processing the input file will work, no need to put this in the library.