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

Checking for allowed tokens with accepts() triggers transformer callbacks #1369

Closed
Daniel63656 opened this issue Nov 17, 2023 · 4 comments
Closed
Labels

Comments

@Daniel63656
Copy link

Daniel63656 commented Nov 17, 2023

I use a transformer to get callblacks whenever a rule finishes. I use an interactive parser. When I check for the next tokens that can be parsed, the callbacks are triggered several times:

from lark import Lark, Token, Transformer

grammar ="""
    sentence: noun verb noun
            | noun verb "like" noun

    noun: adj? NOUN
    verb: VERB
    adj: ADJ

    NOUN: "flies" | "bananas" | "fruit"
    VERB: "like" | "flies"
    ADJ: "yummy"
"""

class MyTransformer(Transformer):
    def adj(self, children):
        print("Callback for adj:", children)
        return children

    def noun(self, children):
        print("Callback for noun:", children)
        return children

    def verb(self, children):
        print("Callback for verb:", children)
        return children
    

parser = Lark(grammar, parser='lalr', start='sentence', transformer=MyTransformer())
interactive = parser.parse_interactive()

#parser.parse('yummy')
#interactive.accepts()

interactive.feed_token(Token('ADJ', 'yummy'))
interactive.feed_token(Token('NOUN', 'fruit'))
print("----FROM HERE ON NO CALLBACKS SHOULD HAPPEN----")
interactive.accepts()

Is this a bug?

@erezsh
Copy link
Member

erezsh commented Nov 18, 2023

Works fine for me.

Are you using the latest Lark version? i.e. 1.1.8

@Daniel63656
Copy link
Author

my version is
lark-parser 0.12.0 pypi_0 pypi

which seems to be the newest version. If I run
$ pip install --upgrade lark-parser
I get:
Requirement already satisfied: lark-parser in c:\users\daniel\anaconda3\envs\musicml\lib\site-packages (0.12.0)

Code output is:

Callback for adj: [Token('ADJ', 'yummy')]
----FROM HERE ON NO CALLBACKS SHOULD HAPPEN----
Callback for noun: [[Token('ADJ', 'yummy')], Token('NOUN', 'fruit')]
Callback for noun: [[Token('ADJ', 'yummy')], Token('NOUN', 'fruit')]
{'VERB'}

@erezsh
Copy link
Member

erezsh commented Nov 18, 2023

pip uninstall lark-parser
pip install lark -U

@Daniel63656
Copy link
Author

Thanks a lot that fixed it. Maybe consider removing the wheels for lark-parser to avoid confusion?

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