我正在尝试为我的解析器使用TatSu(版本4.4.0)实现ModelWalker类,但是它不起作用。我为解决方案使用了description,但它引发了一些我不了解且无法自行解决的异常。你能帮我吗?
这是一个测试语法(文件test.ebnf),可用于重现我的错误:
@@grammar::TestGrammar
START =
(
PASCALCASEID SPACE
{ PASCALCASEID SPACE }* $
)
;
SPACE =
space:/[ \n\t]*/
;
PASCALCASEID =
pascalcaseid:(/[A-Z]([a-z0-9])*([A-Z][a-z0-9]+)*/)
;
我使用生成解析器
python -m tatsu --generate-parser test.ebnf --outfile testparser.py
并按照以下Python代码所示创建了walker:
from testparser import TestGrammarParser
from testparser import TestGrammarSemantics
from tatsu.model import NodeWalker
class TestGrammarWalker(NodeWalker):
def walk_Node(self, node):
print("Reached Node", node)
def walk_str(self, s):
return s
def walk_object(self, o):
raise Exception('Unexpected tyle %s walked', type(o).__name__)
def walk__PASCALCASEID(self, node):
print("found:" + str(self.walk(node.pascalcaseid)))
parser = TestGrammarParser()
model = parser.parse("FirstId SecondId ThirdId", semantics=TestGrammarSemantics(), rule_name="START")
walker = TestGrammarWalker()
walker.walk(model)
运行此代码时,出现此错误
Traceback (most recent call last):
File ".../test.py", line 22, in <module>
walker.walk(model)
File "...\venv\lib\site-packages\tatsu\walkers.py", line 18, in walk
return walker(node, *args, **kwargs)
File ".../test.py", line 14, in walk_object
raise Exception('Unexpected tyle %s walked', type(o).__name__)
Exception: ('Unexpected tyle %s walked', 'list')
如果我了解description的权利,则必须使用节点类型,并编写语法
PASCALCASEID::PascalCaseID =
pascalcaseid:(/[A-Z]([a-z0-9])*([A-Z][a-z0-9]+)*/)
;
在我的沃克班上
def walk__PascalCaseID(self, node):
print("found:" + str(self.walk(node.pascalcaseid)))
但不幸的是,这引发了另一个异常:
Traceback (most recent call last):
File "C:/Users/Andreas/PycharmProjects/fpl/test.py", line 20, in <module>
model = parser.parse("FirstId SecondId ThirdID", semantics=TestGrammarSemantics(), rule_name="START")
File "...\venv\lib\site-packages\tatsu\contexts.py", line 218, in parse
result = rule()
File "...\venv\lib\site-packages\tatsu\contexts.py", line 56, in wrapper
return self._call(ruleinfo)
File "...\venv\lib\site-packages\tatsu\contexts.py", line 519, in _call
result = self._recursive_call(ruleinfo)
File "...\venv\lib\site-packages\tatsu\contexts.py", line 548, in _recursive_call
return self._invoke_rule(ruleinfo, self.memokey)
File "...\venv\lib\site-packages\tatsu\contexts.py", line 595, in _invoke_rule
ruleinfo.impl(self)
File "...\testparser.py", line 86, in _START_
self._PASCALCASEID_()
File "...\venv\lib\site-packages\tatsu\contexts.py", line 56, in wrapper
return self._call(ruleinfo)
File "...\venv\lib\site-packages\tatsu\contexts.py", line 519, in _call
result = self._recursive_call(ruleinfo)
File "...\venv\lib\site-packages\tatsu\contexts.py", line 548, in _recursive_call
return self._invoke_rule(ruleinfo, self.memokey)
File "...\venv\lib\site-packages\tatsu\contexts.py", line 597, in _invoke_rule
node = self._invoke_semantic_rule(ruleinfo, node)
File "...\venv\lib\site-packages\tatsu\contexts.py", line 623, in _invoke_semantic_rule
node = semantic_rule(node, *(rule.params or ()), **(rule.kwparams or {}))
TypeError: PASCALCASEID() takes 2 positional arguments but 3 were given