Search code examples
pythonparsinglexersly

How to parse multiple statements in sly?


I am trying to parse some code using sly. I would like to separate the statements with a semicolon.

I have defined a token called SEMI which represents a semicolon:

class MyLexer(Lexer):
    tokens = {
        ...,
        SEMI
    }

    SEMI = r";"
    ...

If I use SEMI inside the parser class like so:

class MyParser(Parser):
    ...
    @_("OUTPUT expr SEMI")
    def statement(self, p):
        return ("output", p.expr)

and put multiple statements in the code I'm trying to parse separated with a semicolon like so:

output 1;output 2;

I get the following error:

sly: Syntax error at line 1, token=OUTPUT

Does anyone know how to make sly parse multiple statements which are separated with a semicolon (or any other character, such as a newline)?


Solution

  • By default the parser only parses one statement. To parse multiple statements:

    @_('statements')
    def program(self, p):
        return p.statements
    
    @_('statement')
    def statements(self, p):
        return (p.statement, )
    
    @_('statements statement')
    def statements(self, p):
        return p.statements + (p.statement, )