Browse Source

Merge branch 'graphivz_dot_output' of https://github.com/omega16/lark into omega16-graphivz_dot_output

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
Erez Sh 4 years ago
parent
commit
d238541916
4 changed files with 18 additions and 3 deletions
  1. +1
    -1
      README.md
  2. +2
    -0
      docs/features.md
  3. +4
    -0
      examples/fruitflies.py
  4. +11
    -2
      lark/tree.py

+ 1
- 1
README.md View File

@@ -81,7 +81,7 @@ Lark is great at handling ambiguity. Here is the result of parsing the phrase "f


![fruitflies.png](examples/fruitflies.png) ![fruitflies.png](examples/fruitflies.png)


See the code and more [examples here](https://github.com/lark-parser/lark/tree/master/examples)
See the code to make [this image](https://github.com/lark-parser/lark/tree/master/examples/fruitflies.py) and more [examples here](https://github.com/lark-parser/lark/tree/master/examples)




## List of main features ## List of main features


+ 2
- 0
docs/features.md View File

@@ -26,6 +26,8 @@
- Support for external regex module ([see here](classes.md#using-unicode-character-classes-with-regex)) - Support for external regex module ([see here](classes.md#using-unicode-character-classes-with-regex))
- Import grammars from Nearley.js ([read more](nearley.md)) - Import grammars from Nearley.js ([read more](nearley.md))
- CYK parser - CYK parser
- Transform your parse tree to dot or png files for better visualization ([see_example](https://github.com/lark-parser/lark/blob/master/examples/fruitflies.py))



### Experimental features ### Experimental features
- Automatic reconstruction of input from parse-tree (see examples) - Automatic reconstruction of input from parse-tree (see examples)


+ 4
- 0
examples/fruitflies.py View File

@@ -33,9 +33,13 @@ sentence = 'fruit flies like bananas'
def make_png(filename): def make_png(filename):
tree.pydot__tree_to_png( parser.parse(sentence), filename) tree.pydot__tree_to_png( parser.parse(sentence), filename)


def make_dot(filename):
tree.pydot__tree_to_dot( parser.parse(sentence), filename)

if __name__ == '__main__': if __name__ == '__main__':
print(parser.parse(sentence).pretty()) print(parser.parse(sentence).pretty())
# make_png(sys.argv[1]) # make_png(sys.argv[1])
# make_dot(sys.argv[1])


# Output: # Output:
# #


+ 11
- 2
lark/tree.py View File

@@ -160,6 +160,15 @@ class SlottedTree(Tree):




def pydot__tree_to_png(tree, filename, rankdir="LR", **kwargs): def pydot__tree_to_png(tree, filename, rankdir="LR", **kwargs):
graph = pydot__tree_to_graph(tree, rankdir, **kwargs)
graph.write_png(filename)


def pydot__tree_to_dot(tree, filename, rankdir="LR", **kwargs):
graph = pydot__tree_to_graph(tree, rankdir, **kwargs)
graph.write(filename)

def pydot__tree_to_graph(tree, rankdir="LR", **kwargs):
"""Creates a colorful image that represents the tree (data+children, without meta) """Creates a colorful image that represents the tree (data+children, without meta)


Possible values for `rankdir` are "TB", "LR", "BT", "RL", corresponding to Possible values for `rankdir` are "TB", "LR", "BT", "RL", corresponding to
@@ -197,5 +206,5 @@ def pydot__tree_to_png(tree, filename, rankdir="LR", **kwargs):
return node return node


_to_pydot(tree) _to_pydot(tree)
graph.write_png(filename)
return graph

Loading…
Cancel
Save