lark icon indicating copy to clipboard operation
lark copied to clipboard

Tree Matcher/reconstructor incorrectly (un-)flattens children

Open MegaIng opened this issue 4 years ago • 2 comments

Describe the bug

Sometimes (e.g. not reliable/random chance), the TreeMatcher (and therefore the Reconstructor applies trees like this:

expr
    literal "a"
    "5"
    "5"

as if they were

expr
    expr
        literal "a"
        "5"
    "5"

To Reproduce

parser = Lark.open_from_package("lark", "lark.lark", ("grammars",))

lark_reconstructor = Reconstructor(parser, {
    "_NL": lambda _: "\n",
    "_VBAR": lambda _: "|",
})

pattern = parser.parse("""
start: "a"~5..5
start: ("b"~5)~5
""")
print(pattern.pretty())
print(lark_reconstructor.reconstruct(pattern))

This script sometimes wrongly produces

start:("a"~5)~5
start:("b"~5)~5

(I came across this while working on ast_generator)


Maybe related: Sometimes, the tree_matcher outputs normally illegal constructs of the shape Tree('expansion',[Tree('expansion',[...])]) or Tree('<origin>', [Tree('<alias>',[...])]) This can be dealt with, but is kinda annoying. I don't have a simple example. Here is where I deal with it.

MegaIng avatar Jun 29 '21 09:06 MegaIng

Sounds like something that can be solved by changing priority / ordering of choices?

erezsh avatar Jun 29 '21 10:06 erezsh

@erezsh I am not sure. I don't know exactly how tree_matcher is implemented.

MegaIng avatar Jun 29 '21 11:06 MegaIng