Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing ambiguous data results in unhelpful exception. #25

Closed
thijsmie opened this issue Sep 13, 2017 · 2 comments
Closed

Parsing ambiguous data results in unhelpful exception. #25

thijsmie opened this issue Sep 13, 2017 · 2 comments

Comments

@thijsmie
Copy link

thijsmie commented Sep 13, 2017

Consider the following snippet:

from lark import Lark

parser = Lark("""
?inner: "a"

outer: "a" "b" -> one
    | inner "b" -> two
    
start: outer

%import common.WS
%ignore WS
""")

parser.parse("a b")

This grammar definition makes no sense, but never mind that. The output produced is:

Traceback (most recent call last):
  File "test2.py", line 15, in <module>
    parser.parse("a b")
  File "*/site-packages/lark/lark.py", line 190, in parse
    return self.parser.parse(text)
  File "*/site-packages/lark/parser_frontends.py", line 137, in parse
    return self.parser.parse(text)
  File "*/site-packages/lark/parsers/xearley.py", line 131, in parse
    ResolveAmbig().visit(tree) 
  File "*/site-packages/lark/tree.py", line 132, in visit
    getattr(self, subtree.data, self.__default__)(subtree)
  File "*/site-packages/lark/parsers/earley.py", line 282, in _ambig
    _resolve_ambig(tree)
  File "*/site-packages/lark/parsers/earley.py", line 273, in _resolve_ambig
    best = min(tree.children, key=cmp_to_key(_compare_drv))
  File "*/site-packages/lark/parsers/earley.py", line 263, in _compare_drv
    c = _compare_drv(t1, t2)
  File "*/site-packages/lark/parsers/earley.py", line 263, in _compare_drv
    c = _compare_drv(t1, t2)
  File "*/site-packages/lark/parsers/earley.py", line 241, in _compare_drv
    return -compare(tree1, tree2)
  File "*/site-packages/lark/utils.py", line 86, in compare
    elif a > b:
TypeError: '>' not supported between instances of 'Derivation' and 'Token'

I would expect some kind of parser error that notifies me of an unresolvable ambiguity. (I found this because my parser would work in version 0.3.1 but not in 0.3.6, so somewhere along the line the behaviour of ambiguities changed. I've been unable to construct a small example that shows this behaviour though.)

@erezsh
Copy link
Member

erezsh commented Sep 13, 2017

Fixed and pushed as 0.3.7
Thanks for catching the bug and giving a useful example!

Just a note, @thijsmie : Lark chooses a single derivation by default, using a sensible (but arbitrary) set of rules.
If you want to see all possible derivations, so you can choose for yourself, you'll have to call Lark(..., ambiguity='explicit')
Good luck!

@erezsh erezsh closed this as completed Sep 13, 2017
@thijsmie
Copy link
Author

Thanks for the quick fix! In my case the ambiguity resulted from a mistake in my grammar definition, but I'll keep the "explicit" option in mind for future projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants