Skip to content

Commit

Permalink
Add tip in the docs on ambiguity investigation
Browse files Browse the repository at this point in the history
  • Loading branch information
igordejanovic committed Jan 28, 2021
1 parent 69cb160 commit eaf2a27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/disambiguation.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,9 @@ with pytest.raises(ParseError):
`custom_token_recognition` can be used to implement custom lexical
disambiguation by calling `get_tokens` and then reducing returned list to a
list with a single result.


!!! tip

See the end of [the parse trees section](parse_trees.md) for a tip on how to
investigate ambiguities in GLR parsing.
18 changes: 17 additions & 1 deletion docs/parse_trees.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,20 @@ with `tree_str()`:

You can use `tree_str()` on the root of the parse tree to get the string
representation of the parse tree. This can be handy to compare multiple
trees returned by GLR parser to analyse ambiguity.
trees returned by GLR parser to analyze ambiguity.

For example:

# Here we have GLR parser which returns multiple parse trees due to ambiguity.
# It is important to turn on tree building.
parser = GLRParser(g, build_tree=True)
trees = parser.parse_file('some_file')
for idx, tree in enumerate(trees):
with open(f'tree_{idx:03d}.txt', 'w') as f:
f.write(tree.tree_str())

Now you can run any diff tool on the produced outputs to see where are the ambiguities:

$ meld tree_000.txt tree_001.txt


0 comments on commit eaf2a27

Please sign in to comment.