我正在使用Ply進行教學,我非常喜歡它。 我雖然使用裝飾不重複一些代碼,我想在一些功能。 所以,我嘗試使用下面的代碼:使用python和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似乎接受兩個參數......任何線索? 謝謝你的幫助。
返回包裝可能不正確地縮進。 – 2014-11-03 13:51:51