瀏覽代碼

Merge branch 'omega16-graphivz_dot_output'

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
Erez Sh 4 年之前
父節點
當前提交
4e37b754f5
共有 4 個文件被更改,包括 18 次插入3 次删除
  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 查看文件

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

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

See the code and more [examples here](https://github.com/lark-parser/lark/tree/master/examples)
[Read the code here](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


+ 2
- 0
docs/features.md 查看文件

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


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


+ 4
- 0
examples/fruitflies.py 查看文件

@@ -33,9 +33,13 @@ sentence = 'fruit flies like bananas'
def make_png(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__':
print(parser.parse(sentence).pretty())
# make_png(sys.argv[1])
# make_dot(sys.argv[1])

# Output:
#


+ 11
- 2
lark/tree.py 查看文件

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


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)

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

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

Loading…
取消
儲存