Closed chenshimeng closed 2 weeks ago
I think that you're right that your function isn't being called.
That's because you defined it as ?acos_func
, and the "?" tells Lark that if there is only 1 argument, to inline the rule and not call its callback. And your rule only has one argument, NUMBER.
Thank you for your assistance and explanation regarding the behavior of the ? prefix in Lark's grammar rules. Following your guidance, I have conducted further tests and confirmed that removing the ? prefix indeed allows the acos_func transformation method to be called as expected. Additionally, I discovered that if I retain the ? prefix in the rule ?acos_func: ("acos" | "ACOS") "(" NUMBER ")" but append -> acos_func at the end, the transformation method acos_func also gets called correctly.
Could you please advise which solution is considered more optimal or appropriate in terms of Lark's best practices? Are there any considerations regarding performance, readability, or future maintenance that I should be aware of when choosing between these two approaches?
Thank you once again for your support. Your explanation was instrumental in helping me understand and resolve the issue.
Well, alias (->
) doesn't have performance costs. Using ?
does have a small cost, but I think it's very negligible.
Expected Behavior
I expect the output to be:
This is based on the transformation defined in
MyTransformer
class for theacos_func
.Actual Behavior
Instead of the expected string, the output is just:
This suggests that the transformation
acos_func
inMyTransformer
is not being applied.Environment