module languages.tree_graph_listener

Inheritance diagram of pyensae.languages.tree_graph_listener

Short summary

module pyensae.languages.tree_graph_listener

Helpers around language grammar. This module requires antlr4.

source on GitHub

Classes

class

truncated documentation

TreeGraphListener

This class is an attempt to run through the tree and to convert into a graph.

Properties

property

truncated documentation

Edges

return edges

Vertices

return vertices

Methods

method

truncated documentation

__init__

constructor

_get_key_context

_get_key_node

draw

draw the graph with networkx on matplotlib

enterEveryRule

event

exitEveryRule

event

to_dot

export the graph to DOT format

to_networkx

convert the graph into networkx

visitErrorNode

event

visitTerminal

event

Documentation

Helpers around language grammar. This module requires antlr4.

source on GitHub

class pyensae.languages.tree_graph_listener.TreeGraphListener(parser, verbose=False, fLOG=None)

Bases: ParseTreeListener

This class is an attempt to run through the tree and to convert into a graph.

Draw a grammar graph for a small code

from pyensae.languages import get_parser_lexer, parse_code, get_tree_graph
from pyensae.graph_helper import run_dot

code = '''
namespace hello
{
    public static class world
    {
        public static double function(double x, doubly y)
        {
            return x+y ;
        }
    }
}
'''

clparser, cllexer = get_parser_lexer("C#")
parser = parse_code(code, clparser, cllexer)
tree = parser.compilation_unit()
st = get_tree_graph(tree, parser)
dot = st.to_dot()

with open(name, "w") as f:
    f.write(dot)
img = os.path.join(temp, "graph.png")
run_dot(name, img)

source on GitHub

constructor

Parameters:
  • parser – parser used to parse the code

  • verbose – display information along the path

  • fLOG – logging function

source on GitHub

property Edges

return edges

source on GitHub

property Vertices

return vertices

source on GitHub

__init__(parser, verbose=False, fLOG=None)

constructor

Parameters:
  • parser – parser used to parse the code

  • verbose – display information along the path

  • fLOG – logging function

source on GitHub

_get_key_context(ctx)
_get_key_node(node)
draw(ax=None)

draw the graph with networkx on matplotlib

Parameters:

matplotlib – axis

source on GitHub

enterEveryRule(ctx)

event

source on GitHub

exitEveryRule(ctx)

event

source on GitHub

to_dot()

export the graph to DOT format

Returns:

string

source on GitHub

to_networkx()

convert the graph into networkx

Returns:

networkx Graph

source on GitHub

visitErrorNode(node)

event

source on GitHub

visitTerminal(node)

event

source on GitHub