igordejanovic / parglare

A pure Python LR/GLR parser - http://www.igordejanovic.net/parglare/
MIT License
136 stars 32 forks source link

Wrong positions with GLRParser and EMPTY #110

Closed Hyldrean closed 4 years ago

Hyldrean commented 4 years ago

Description

The start/end positions are sometimes wrong when we use the GLRParser and grammar with EMPTY. NB: I couldn't reproduce the bug with the LR Parser.

What I Did

Here is a minimal working example


from parglare import GLRParser, Grammar

rules = """
S: A A B;
A: letter;
B: letter | EMPTY;

terminals
letter: /\w/;
"""
g = Grammar.from_string(rules)
parser = GLRParser(g, build_tree=True, debug=False, debug_colors=True)
result = parser.parse("ab")
print(result[0].tree_str())

It gives the tree


S[0->1]
A[0->1]
  letter[0->1, "a"]
A[1->2]
  letter[1->2, "b"]
B[1->1]

One can easily see that it's rather

S[0->2]
A[0->1]
  letter[0->1, "a"]
A[1->2]
  letter[1->2, "b"]
B[2->2]
igordejanovic commented 4 years ago

Thanks for the report. Could you make a PR with failing regression test?

Hyldrean commented 4 years ago

It is the very first time I use py.test and I do a pull request. I hope all is fine.

igordejanovic commented 4 years ago

@Hyldrean It's perfect. Thanks!

igordejanovic commented 4 years ago

Fixed on master

Hyldrean commented 4 years ago

Thanks ! It works for me ;)