Closed Daniel63656 closed 8 months ago
Using Transformer I can get callbacks when production rules get applied. Like this:
from lark import Lark, Transformer class MyTransformer(Transformer): def sentence(self, children): print("Callback for sentence:", children) return children 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 # Define your grammar grammar = """ sentence: noun verb noun -> simple | noun verb "like" noun -> comparative noun: adj? NOUN verb: VERB adj: ADJ NOUN: "flies" | "bananas" | "fruit" VERB: "like" | "flies" ADJ: "fruit" %import common.WS %ignore WS """ # Create the Lark parser with the grammar and transformer parser = Lark(grammar, parser="lalr", start='sentence', transformer=MyTransformer()) # Parse and transform sentence = 'fruit flies like bananas' result = parser.parse(sentence)
Can I get a callback when a rule is popped from the stack together with the complete content of the rule?
Using Transformer I can get callbacks when production rules get applied. Like this:
Can I get a callback when a rule is popped from the stack together with the complete content of the rule?