Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformer Not Applying Expected Transformations in Lark Parser #1416

Open
chenshimeng opened this issue May 21, 2024 · 2 comments
Open

Transformer Not Applying Expected Transformations in Lark Parser #1416

chenshimeng opened this issue May 21, 2024 · 2 comments
Labels

Comments

@chenshimeng
Copy link

chenshimeng commented May 21, 2024

## Description
When using the Lark parser with a custom Transformer to parse and transform a simple grammar, the expected transformations specified in the Transformer class do not seem to be applied. Instead of getting the transformed output, I receive the raw parsed data.

## Steps to Reproduce
Here is the minimal code snippet to reproduce the issue:

```python
from lark import Lark, Transformer

grammar = """
?start: acos_func
?acos_func: ("acos" | "ACOS") "(" NUMBER ")"
NUMBER: /-?\d+(\.\d+)?/
%import common.WS
%ignore WS
"""

class MyTransformer(Transformer):
    def acos_func(self, args):
        return "ACOS called with argument: " + str(args[0])

parser = Lark(grammar, parser='lalr', transformer=MyTransformer())
test_input = "ACOS(1.0)"
print(parser.parse(test_input))

Expected Behavior

I expect the output to be:

ACOS called with argument: 1.0

This is based on the transformation defined in MyTransformer class for the acos_func.

Actual Behavior

Instead of the expected string, the output is just:

1.0

This suggests that the transformation acos_func in MyTransformer is not being applied.

Environment

  • Lark version: [lark-parser==0.12.0]
  • Python version: [3.10]
  • Operating System: [macOS]
@erezsh
Copy link
Member

erezsh commented May 21, 2024

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.

@chenshimeng
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants