当使用lark.visitors.Interpreter类时,Lark解析器Indenter类不起作用

问题描述

因此,我正在使用python中的lark库制作一种编程语言,到目前为止,我使用的是Transformer()类,但是现在我切换到了lark.visitors.Interpreter()类。我一直在复制从TransformerInterpreter的进度,但似乎无法使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,*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))

将不胜感激,谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)