Parsing and visualazing trees
Parsing a string
To parse a string use parse()
from maquinas.contextfree.cfg import ContextFreeGrammar as CFG
g=CFG("S-> ACB; C-> ACB; C -> AB; A -> a; B->b")
roots,chart,forest=g.parse("aabcc")
The function returns tree elements _roots_ symbols, the _chart_ structure and the _forest_ parse
To verify is the string was part of the language grammar check the roots
print(f'Accepts?', "yes" if roots else "no" )
To print the chart use print_chart()
g.print_chart(s,chart,pointers=True)
To extract the trees from the forest use extract_trees()
trees=g.extract_trees(forest)
Saving trees images
To save trees into an image file you can use save_trees_img()
g.save_trees_img(trees,filename=filename)
Visualizing in Notebooks
To display the forest grpah use graph_forest()
m.graph_forest(forest)
To display trees use graph_trees()
m.graph_trees(trees)
To display a tree use graph_tree()
m.graph_tree(trees[0])