The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


GraphViz2::Parse::Yapp - Visualize a yapp grammar as a graph


    use GraphViz2::Parse::Yapp;
    # no objects - quicker
    my $gd = GraphViz2::Parse::Yapp::to_graph('t/calc.output');

    # populate a GraphViz2 object with a Graph::Directed of a parser
    my $gv = GraphViz2->from_graph(GraphViz2::Parse::Yapp::graphvizify($gd));

    # OO interface, using lazy-built attributes
    my $gvp = GraphViz2::Parse::Yapp->new(file_name => $file_name);
    my $gd = $gvp->as_graph; # Graph::Directed object
    # or supply a suitable Graph::Directed object
    my $gvp = GraphViz2::Parse::Yapp->new(as_graph => $gd);
    # then get the GraphViz2 object
    my $gv = $gvp->graph;

    # DEPRECATED ways to get $gvp with populated $gv
    my $gvp = GraphViz2::Parse::Yapp->new;
    $gvp->create(file_name => 't/calc.output');
    my $gv = $gvp->graph;
    # or give it a pre-set-up GraphViz2 object
    my $gv = GraphViz2->new(...);
    my $gvp = GraphViz2::Parse::Yapp->new(graph => $gv);
    # call ->create as above

    # produce a visualisation
    my $format = shift || 'svg';
    my $output_file = shift || "output.$format";
    $gv->run(format => $format, output_file => $output_file);

See t/


Takes a yapp grammar and converts it into a Graph::Directed object, or directly into a GraphViz2 object.


This is the recommended interface.


    my $gd = GraphViz2::Parse::Yapp::to_graph('t/calc.output');

Given a yapp grammar, returns a Graph::Directed object describing the finite state machine for it.


    my $gv = GraphViz2->from_graph(GraphViz2::Parse::Yapp::graphvizify($gd));

Mutates the given graph object to add to it the graphviz attributes visualisation "hints" that will make the "from_graph" in GraphViz2 method visualise this regular expression in the most meaningful way, including labels and groupings.

It is idempotent as it simply sets the graphviz attribute of the relevant graph entities.

Returns the graph object for convenience.


This is a Moo class, but with a recommended functional interface.

Constructor attributes


The name of a yapp output file. See t/calc.output.

This key is optional. You need to provide it by the time you access either the "as_graph" or "graph".


The Graph::Directed object to use. If not given, will be lazily built on access, from the "regexp".


The GraphViz2 object to use. This allows you to configure it as desired.

This key is optional. If provided, the create method will populate it. If not, it will have these defaults, lazy-built and populated from the "as_graph".

    my $gv = GraphViz2->new(
            edge   => {color => 'grey'},
            global => {directed => 1},
            graph  => {rankdir => 'TB'},
            node   => {color => 'blue', shape => 'oval'},

create(regexp => $regexp)

DEPRECATED. Mutates the object to set the file_name attribute, then accesses the as_graph attribute (possibly lazy-building it), then graphvizifys its as_graph attribute with that information, then from_graphs its graph.

Returns $self for method chaining.


Many thanks are due to the people who chose to make Graphviz Open Source.

And thanks to Leon Brocard, who wrote GraphViz, and kindly gave me co-maint of the module.


GraphViz2 was written by Ron Savage <> in 2011.

Home page:


Australian copyright (c) 2011, Ron Savage.

All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Perl License, a copy of which is available at: