# Failed tree-less parsing using python lark

## Question:

I use lark to parse lines of a log file. The log file contains some equations and I would like to extract the left hand side and right hand side of the equations and store them in a dictionary. However, in my context the parsing seems to be kind of slow. I accelerated the parsing by switching from the Earley algorithm to LALR(1) parsing, as suggested by the lark tutorial.
However, I would like to squeeze out the last bit of performance, by also »going tree-less«. Unfortunately, it does not work as expected. Consider the following MWE:

``````from lark import Lark, Transformer

OPT = True

def parser() -> Lark:
grammar = r"""
equations: [equation ("," equation)*]
equation: identifier "=" rhs
identifier: CNAME
rhs: num
| vector
vector: "[" [num ("," num)*] "]"
num: SIGNED_NUMBER
%import common.CNAME
%import common.SIGNED_NUMBER
%import common.WS
%ignore WS
"""
if OPT:
eq_parser = Lark(grammar, start="equations", parser="lalr", transformer=ToDict)
else:
eq_parser = Lark(grammar, start="equations", parser="lalr")
return eq_parser

class ToDict(Transformer):
def equations(self, eqs):
return {lhs: rhs for eq in eqs for lhs, rhs in eq.items()}

def equation(self, eq):
(ident, _rhs) = eq
return {ident: _rhs}

def rhs(self, num_vec):
(num_vec,) = num_vec
return num_vec

def identifier(self, ident):
(ident,) = ident
return str(ident)

def num(self, n):
(n,) = n
return float(n)

def vector(self, vec):
return list(vec)

if __name__ == "__main__":
line = "a=3.14, b=[1.41, 1.732]"
prsr = parser()
if OPT:
parsed = prsr.parse(line)
else:
parsed = ToDict().transform(prsr.parse(line))
print(parsed)
``````

If `OPT` is set to `False`, the expression `{'a': 3.14, 'b': [1.41, 1.732]}` is returned as expected. But if `OPT` is set to `True`, this happens:

``````Traceback (most recent call last):
File ".../mwe.py", line 55, in <module>
parsed = prsr.parse(line)
File ".../anaconda3/envs/lark/lib/python3.10/site-packages/lark/lark.py", line 625, in parse
return self.parser.parse(text, start=start, on_error=on_error)
File ".../anaconda3/envs/lark/lib/python3.10/site-packages/lark/parser_frontends.py", line 96, in parse
return self.parser.parse(stream, chosen_start, **kw)
File ".../anaconda3/envs/lark/lib/python3.10/site-packages/lark/parsers/lalr_parser.py", line 41, in parse
return self.parser.parse(lexer, start)
File ".../anaconda3/envs/lark/lib/python3.10/site-packages/lark/parsers/lalr_parser.py", line 171, in parse
return self.parse_from_state(parser_state)
File ".../anaconda3/envs/lark/lib/python3.10/site-packages/lark/parsers/lalr_parser.py", line 179, in parse_from_state
state.feed_token(token)
File ".../anaconda3/envs/lark/lib/python3.10/site-packages/lark/parsers/lalr_parser.py", line 150, in feed_token
value = callbacks[rule](s)
TypeError: ToDict.identifier() missing 1 required positional argument: 'ident'
``````

What is the error message trying to tell me?

`transformer=ToDict()`
`transformer=ToDict`
{"ft_lang_label":"__label__en","ft_lang_prob":0.5535359,"math_prob":0.7339041,"size":6015,"snap":"2023-14-2023-23","text_gpt3_token_len":1680,"char_repetition_ratio":0.13591748,"word_repetition_ratio":0.97282606,"special_character_ratio":0.28462178,"punctuation_ratio":0.23481782,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.988201,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-26T02:03:45Z\",\"WARC-Record-ID\":\"<urn:uuid:4c700197-a560-4413-99d6-33d6da744493>\",\"Content-Length\":\"21538\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3b023f92-942a-4517-8e19-f4b0a1434f84>\",\"WARC-Concurrent-To\":\"<urn:uuid:3c59be56-9c00-4bb1-b0a8-7f3d2cd5829f>\",\"WARC-IP-Address\":\"104.21.83.140\",\"WARC-Target-URI\":\"https://py4u.org/questions/75753922/\",\"WARC-Payload-Digest\":\"sha1:42R4WBSFSQZKN5IC43VAAX3KHAJNFKMO\",\"WARC-Block-Digest\":\"sha1:IQZ4NNBZR4VF3DPK2DYDTICVUXGQDHCA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945381.91_warc_CC-MAIN-20230326013652-20230326043652-00428.warc.gz\"}"}