So I'm making a programming language using the lark
library in python, up until now, I was using the class Transformer()
, but now I've switched to the lark.visitors.Interpreter()
class. I've been copying over my progress from Transformer
to Interpreter
, but I cant seem to get the Indenter
class working. For example, if I parse
5 + 5
5 + 5
When I parse this, I'm getting the following error:
PS E:\ParserAndLexer> & C:/Python38/python.exe e:/ParserAndLexer/lite/lite_interpreter.py
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\lark\parsers\lalr_parser.py", line 59, in get_action
return states[state][token.type]
KeyError: 'NUMBER'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "e:/ParserAndLexer/lite/lite_interpreter.py", line 46, in <module>
tree = parser.parse(lite_code)
File "C:\Python38\lib\site-packages\lark\lark.py", line 464, in parse
return self.parser.parse(text, start=start)
File "C:\Python38\lib\site-packages\lark\parser_frontends.py", line 148, in parse
return self._parse(token_stream, start, set_parser_state)
File "C:\Python38\lib\site-packages\lark\parser_frontends.py", line 63, in _parse
return self.parser.parse(input, start, *args)
File "C:\Python38\lib\site-packages\lark\parsers\lalr_parser.py", line 35, in parse
return self.parser.parse(*args)
File "C:\Python38\lib\site-packages\lark\parsers\lalr_parser.py", line 88, in parse
action, arg = get_action(token)
File "C:\Python38\lib\site-packages\lark\parsers\lalr_parser.py", line 66, in get_action
raise UnexpectedToken(token, expected, state=state, puppet=puppet)
lark.exceptions.UnexpectedToken: Unexpected token Token('NUMBER', '5') at line 2, column 1.
Expected one of:
* __ANON_0
* $END
My code looks somewhat like this:
from lark.visitors import Interpreter
from lark import Lark
from lark.indenter import Indenter
class MainIndenter(Indenter):
NL_type = '_NL'
OPEN_PAREN_types = ['LPAR', 'LBRACE']
CLOSE_PAREN_types = ['RPAR', 'RBRACE']
INDENT_TYPE = '_INDENT'
DEDENT_type = '_DEDENT'
tab_len = 8
class MainInterpreter(Interpreter):
number = int
string = str
def __init__(self):
self.vars = {}
def add(self, tree):
return int(tree.children[0]) + int(tree.children[1])
def sub(self, tree):
return int(tree.children[0]) - int(tree.children[1])
def mul(self, tree):
return int(tree.children[0]) & int(tree.children[1])
def div(self, tree):
return int(tree.children[0]) / int(tree.children[1])
parser = Lark.open('lite\lite_parser.lark',
parser='lalr', postlex=MainIndenter())
grammar = '''
?start: expr
?expr: STRING -> string
| NUMBER -> number
| NUMBER "+" NUMBER -> add
| NUMBER "-" NUMBER -> sub
| NUMBER "*" NUMBER -> mul
| NUMBER "/" NUMBER -> div
| STRING "+" STRING -> str_add
| NAME -> get_var
%import common.ESCAPED_STRING -> STRING
%import common.NUMBER
%declare _INDENT _DEDENT
%import common.WS_INLINE
%ignore WS_INLINE
%import common.NEWLINE -> _NL
%ignore _NL
'''
test_input = '''
5 + 5
5 + 5
'''
if __name__ == '__main__':
tree = parser.parse(test_input)
print(MainInterpreter().visit(tree))
Help would be appreciated, thanks!