pythonlark-parser

Trouble parsing next line Lark


What's up.

So, I'm having trouble getting more than one line to be accepted properly by the parser.


from lark import Lark



class SetLanguageLarkParser():

    def __init__(self):
        self.setParser = Lark(r"""
            start: statement+ 

            statement : "DISP" expr ";"-> display
                        | name "=" expr ";"-> assign

            expr : expr "+" rest         -> union
                    | expr "-" rest      -> differnce
                    | expr "*" rest      -> cross
                    | expr "|" rest      -> intersection
                    | rest

            rest : expr
                | content | name

            content : "{" [ [NUMBER|WORD] ("," [NUMBER|WORD])*] "}"
            name : WORD

            %import common.WORD
            %import common.NUMBER
            %import common.WS
            %ignore WS
        """, start='start')




def main():

    data = """

            x = {0};
            S = {1,2,3};

            """

    setLangP = SetLanguageLarkParser()
    print(setLangP.setParser.parse(data).pretty())
main()


Ultimately if I feed the parser a string like "S={1,2,3};" it works fine, but I feed it multiple strings like
"S={1,2,3};
X={0};"
It does not properly parse. The error I receive is UnexpectedCharacters: No terminal matches 'S' in the current parser context

Thanks in advance.


Solution

  • Looks like I turned to Stackoverflow too quickly. I changed the start from "statement" to "start" in the parser initialization🤦‍♂️. Thanks to whoever gave this a glance.