Closed pdeibert closed 3 months ago
You can't mix standalone code and the normal lark distrubtion. lark.Tree
and example_parser.Tree
are different classes, and lark.Transformer
is not going to recognize example_parser.Tree
. Use example_parser.Transformer
for the standalone tree.
I was unaware of this, but it makes perfect sense.
Does this also include the transformer directly passed to the standalone parser upon creation? In the example the output seems to be correct, but I was wondering if I should also use example_parser.Transformer
here.
You should. In fact, you should never use both a generated standalone parser and the distribution in any situation.
The directly passed transformer happens to work -- in fact, it doesn't need to be a subclass of Transformer
at all, only it's methods are extracted and used later on. But you shouldn't rely on this.
Thank you for clarifying!
I am playing around with the standalone parser and am getting some strange behavior with the parse trees. Parsing an example with LALR and a generated standalone parser (based on the exact same grammar) results in seemingly identical trees. But when transforming them with the same transformer I get different results. Passing the transformer directly to the standalone parser again gives the correct behavior. Based on the example below it seems that the tree is traversed top-down instead of bottom-up, but I do not understand why or how to control this behavior.
Full example (including console output):
I am using Lark 1.1.9 and Python 3.11.3