所以我正在使用lark
python中的库制作一种编程语言,到目前为止,我一直在使用类Transformer()
,但现在我已经切换到lark.visitors.Interpreter()
类。我一直在将我的进度从 复制Transformer
到Interpreter
,但我似乎无法让Indenter
课堂正常工作。例如,如果我解析
5 + 5
5 + 5
当我解析这个时,我收到以下错误:
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
我的代码看起来有点像这样:
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))
帮助将不胜感激,谢谢!