@@ -81,7 +81,7 @@ Lark is great at handling ambiguity. Here is the result of parsing the phrase "f | |||||
 |  | ||||
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 | ||||
@@ -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) | ||||
@@ -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: | ||||
# | # | ||||
@@ -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 | |||||