Python - Rply 将多个不同的规则分配给多个不同的函数
Python - Rply assign multiple different rules to multiple different functions
假设我有一个看起来像这样的 python-rply 代码(取自 here):
from rply import ParserGenerator, LexerGenerator
from rply.token import BaseBox
lg = LexerGenerator()
# Add takes a rule name, and a regular expression that defines the rule.
lg.add("PLUS", r"\+")
lg.add("MINUS", r"-")
lg.add("NUMBER", r"\d+")
lg.ignore(r"\s+")
# This is a list of the token names. precedence is an optional list of
# tuples which specifies order of operation for avoiding ambiguity.
# precedence must be one of "left", "right", "nonassoc".
# cache_id is an optional string which specifies an ID to use for
# caching. It should *always* be safe to use caching,
# RPly will automatically detect when your grammar is
# changed and refresh the cache for you.
pg = ParserGenerator(["NUMBER", "PLUS", "MINUS"],
precedence=[("left", ['PLUS', 'MINUS'])], cache_id="myparser")
@pg.production("main : expr")
def main(p):
# p is a list, of each of the pieces on the right hand side of the
# grammar rule
return p[0]
@pg.production("expr : expr PLUS expr")
@pg.production("expr : expr MINUS expr")
def expr_op(p):
lhs = p[0].getint()
rhs = p[2].getint()
if p[1].gettokentype() == "PLUS":
return BoxInt(lhs + rhs)
elif p[1].gettokentype() == "MINUS":
return BoxInt(lhs - rhs)
else:
raise AssertionError("This is impossible, abort the time machine!")
@pg.production("expr : NUMBER")
def expr_num(p):
return BoxInt(int(p[0].getstr()))
lexer = lg.build()
parser = pg.build()
class BoxInt(BaseBox):
def __init__(self, value):
self.value = value
def getint(self):
return self.value
这是一个简单的代码,所以当您键入此代码时:
parser.parse(lexer.lex("1 + 3"))
它将执行,给你 4
作为输出和答案。这是一个工作代码,但仍需要改进。调用 @pg.production
进行加法和减法的代码部分效率不高;我的意思是,如果您要向其中添加更多操作符,它会变得非常拥挤。有没有一种好的方法可以使该部分的 非拥挤 版本看起来像这样:
@pg.production("expr : expr PLUS expr")
def plus(p):
lhs = p[0].getint()
rhs = p[2].getint()
if p[1].gettokentype() == "PLUS":
return BoxInt(lhs + rhs)
else:
raise AssertionError("This is impossible, abort the time machine!")
@pg.production("expr : expr MINUS expr")
def minus(p):
lhs = p[0].getint()
rhs = p[2].getint()
if p[1].gettokentype() == "MINUS":
return BoxInt(lhs - rhs)
else:
raise AssertionError("This is impossible, abort the time machine!")
如果您拆分函数,使每个产生式都有自己的函数——这确实是最佳实践——那么检查运算符的令牌类型绝对没有意义。你知道它是什么,因为解析器的逻辑意味着该函数只会在与生产匹配的情况下被调用。
所以你可以写出合理紧凑的代码:
@pg.production("expr : expr PLUS expr")
def plus(p):
return BoxInt(p[0].getint() + p[2].getint())
@pg.production("expr : expr MINUS expr")
def minus(p):
return BoxInt(p[0].getint() - p[2].getint())
假设我有一个看起来像这样的 python-rply 代码(取自 here):
from rply import ParserGenerator, LexerGenerator
from rply.token import BaseBox
lg = LexerGenerator()
# Add takes a rule name, and a regular expression that defines the rule.
lg.add("PLUS", r"\+")
lg.add("MINUS", r"-")
lg.add("NUMBER", r"\d+")
lg.ignore(r"\s+")
# This is a list of the token names. precedence is an optional list of
# tuples which specifies order of operation for avoiding ambiguity.
# precedence must be one of "left", "right", "nonassoc".
# cache_id is an optional string which specifies an ID to use for
# caching. It should *always* be safe to use caching,
# RPly will automatically detect when your grammar is
# changed and refresh the cache for you.
pg = ParserGenerator(["NUMBER", "PLUS", "MINUS"],
precedence=[("left", ['PLUS', 'MINUS'])], cache_id="myparser")
@pg.production("main : expr")
def main(p):
# p is a list, of each of the pieces on the right hand side of the
# grammar rule
return p[0]
@pg.production("expr : expr PLUS expr")
@pg.production("expr : expr MINUS expr")
def expr_op(p):
lhs = p[0].getint()
rhs = p[2].getint()
if p[1].gettokentype() == "PLUS":
return BoxInt(lhs + rhs)
elif p[1].gettokentype() == "MINUS":
return BoxInt(lhs - rhs)
else:
raise AssertionError("This is impossible, abort the time machine!")
@pg.production("expr : NUMBER")
def expr_num(p):
return BoxInt(int(p[0].getstr()))
lexer = lg.build()
parser = pg.build()
class BoxInt(BaseBox):
def __init__(self, value):
self.value = value
def getint(self):
return self.value
这是一个简单的代码,所以当您键入此代码时:
parser.parse(lexer.lex("1 + 3"))
它将执行,给你 4
作为输出和答案。这是一个工作代码,但仍需要改进。调用 @pg.production
进行加法和减法的代码部分效率不高;我的意思是,如果您要向其中添加更多操作符,它会变得非常拥挤。有没有一种好的方法可以使该部分的 非拥挤 版本看起来像这样:
@pg.production("expr : expr PLUS expr")
def plus(p):
lhs = p[0].getint()
rhs = p[2].getint()
if p[1].gettokentype() == "PLUS":
return BoxInt(lhs + rhs)
else:
raise AssertionError("This is impossible, abort the time machine!")
@pg.production("expr : expr MINUS expr")
def minus(p):
lhs = p[0].getint()
rhs = p[2].getint()
if p[1].gettokentype() == "MINUS":
return BoxInt(lhs - rhs)
else:
raise AssertionError("This is impossible, abort the time machine!")
如果您拆分函数,使每个产生式都有自己的函数——这确实是最佳实践——那么检查运算符的令牌类型绝对没有意义。你知道它是什么,因为解析器的逻辑意味着该函数只会在与生产匹配的情况下被调用。
所以你可以写出合理紧凑的代码:
@pg.production("expr : expr PLUS expr")
def plus(p):
return BoxInt(p[0].getint() + p[2].getint())
@pg.production("expr : expr MINUS expr")
def minus(p):
return BoxInt(p[0].getint() - p[2].getint())