【发布时间】:2014-11-03 13:32:01
【问题描述】:
我正在使用 Ply 进行教学,我非常喜欢它。 我虽然使用装饰器来避免在某些函数中重复我想要的一些代码。 所以,我尝试使用以下代码:
import ply.yacc as yacc
from functools import wraps
from CLexer import Lexico
def producciones(function):
"""
Decorator for each of the functions which represents
grammatical rules.
"""
variable = function.__doc__.split(':')[0].strip()
@wraps(function)
def wrapper(*args,**kargs):
result = []
for e in args[1][1:]:
tmp = Node()
if isinstance(e,Node):
tmp = e
else:
tmp.type = str(e)
result.append(tmp)
tmp = Node(result)
tmp.type = variable
args[1][0] = tmp
function(*args, **kargs)
return wrapper
class Sintaxis:
tokens = Lexico.tokens
start = 'programa'
@producciones
def p_program(self, p):
"""
program : ABREPAREN program CIERRAPAREN program
|
"""
def p_error(self, p):
print("Syntax error at '%s'" % p.value)
def run(self, s):
lexico = Lexico()
lexico.build()
global tokens
self.parser = yacc.yacc(debug = True, module= self)
result =self.parser.parse(s,lexico)
return result
if __name__ == '__main__':
with open("prueba.txt") as f:
texto=f.read()
parser = Sintaxis()
result = parser.run(texto)
我的问题是尝试使用装饰器时,出现以下错误:
ERROR: new.py:15: Rule 'p_program' requires an argument
我在文档中没有发现这个错误,p_program 方法似乎接受了两个参数...有什么线索吗? 感谢您的帮助。
【问题讨论】:
-
return wrapper 可能缩进不正确。
标签: python parser-generator ply