目前我正在使用Yacc / Lex的python实现来构建一个公式解析器,用于将公式的字符串转换为一组类定义的操作数。到目前为止,我已经取得了很大的成功,但由于括号和几个shift / reduce错误的模糊性,我已经开始定义解析规则了。
我正在研究的公式的Backus Naur表格是
phi ::= p ; !p ; phi_0 & phi_1 ; phi_0 | phi_1 ; AX phi ; AF phi ; AG phi ; AU phi_0 U phi_1.
此外,我一直试图允许任意匹配的括号,但这也是很多混乱的来源,我正在考虑减少错误的来源。对于我将它应用于括号的任务而言,它是非常必要的,因此强制对公式进行特定的评估,所以我必须解决这个问题。
目前我的解析器是在一个用
构建词法分析器的类中定义的 tokens = (
'NEGATION',
'FUTURE',
'GLOBAL',
'NEXT',
'CONJUNCTION',
'DISJUNCTION',
'EQUIVALENCE',
'IMPLICATION',
'PROPOSITION',
'LPAREN',
'RPAREN',
'TRUE',
'FALSE',
)
# regex in order of parsing precedence
t_NEGATION = r'[\s]*\![\s]*'
t_FUTURE = r'[\s]*AF[\s]*'
t_GLOBAL = r'[\s]*AG[\s]*'
t_NEXT = r'[\s]*AX[\s]*'
t_CONJUNCTION = r'[\s]*\&[\s]*'
t_DISJUNCTION = r'[\s]*\|[\s]*'
t_EQUIVALENCE = r'[\s]*\<\-\>[\s]*'
t_IMPLICATION = r'[\s]*[^<]\-\>[\s]*'
t_LPAREN = r'[\s]*\([\s]*'
t_RPAREN = r'[\s]*\)[\s]*'
t_PROPOSITION = r'[\s]*[a-z]+[-\w\._]*[\s]*'
t_TRUE = r'[\s]*TRUE[\s]*'
t_FALSE = r'[\s]*FALSE[\s]*'
precedence = (
('left', 'ASSIGNMENT'),
('left', 'NEGATION'),
('left', 'GLOBAL','NEXT','FUTURE'),
('left', 'CONJUNCTION'),
('left', 'DISJUNCTION'),
('left', 'EQUIVALENCE'),
('left', 'IMPLICATION'),
('left', 'AUB', 'AUM'),
('left', 'LPAREN', 'RPAREN', 'TRUE', 'FALSE'),
)
lexer = lex.lex()
lexer.input(formula)
解析规则为
def p_double_neg_paren(p):
'''formula : NEGATION LPAREN NEGATION LPAREN PROPOSITION RPAREN RPAREN
'''
stack.append(p[5].strip())
def p_double_neg(p):
'''formula : NEGATION NEGATION PROPOSITION
'''
stack.append(p[3].strip())
def p_double_neg_inner_paren(p):
'''formula : NEGATION NEGATION LPAREN PROPOSITION RPAREN
'''
stack.append(p[4].strip())
def p_double_neg_mid_paren(p):
'''formula : NEGATION LPAREN NEGATION PROPOSITION RPAREN
'''
stack.append(p[4].strip())
def p_groupAssignment(p):
'''formula : PROPOSITION ASSIGNMENT ASSIGNVAL
'''
stack.append(p[1].strip() + p[2].strip() + p[3].strip())
def p_neg_paren_take_outer_token(p):
'''formula : NEGATION LPAREN PROPOSITION RPAREN
| NEGATION LPAREN TRUE RPAREN
| NEGATION LPAREN FALSE RPAREN
'''
stack.append(Neg(p[3]))
def p_neg_take_outer_token(p):
'''formula : NEGATION PROPOSITION
| NEGATION TRUE
| NEGATION FALSE
'''
stack.append(Neg(p[2].strip()))
def p_neg_take_outer_token_paren(p):
'''formula : LPAREN NEGATION PROPOSITION RPAREN
| LPAREN NEGATION TRUE RPAREN
| LPAREN NEGATION FALSE RPAREN
'''
stack.append(Neg(p[3].strip()))
def p_unary_paren_nest_take_outer_token(p):
'''formula : GLOBAL LPAREN LPAREN NEGATION formula RPAREN RPAREN
| NEXT LPAREN LPAREN NEGATION formula RPAREN RPAREN
| FUTURE LPAREN LPAREN NEGATION formula RPAREN RPAREN
'''
if len(stack) >= 1:
if p[1].strip() == 'AG':
stack.append(['AG', ['!', stack.pop()]])
elif p[1].strip() == 'AF':
stack.append(['AF', ['!', stack.pop()]])
elif p[1].strip() == 'AX':
stack.append(['AX', ['!', stack.pop()]])
def p_unary_paren_take_outer_token(p):
'''formula : GLOBAL LPAREN formula RPAREN
| NEXT LPAREN formula RPAREN
| FUTURE LPAREN formula RPAREN
'''
if len(stack) >= 1:
if p[1].strip() == "AG":
stack.append(AG(stack.pop()))
elif p[1].strip() == "AF":
stack.append(AF(stack.pop()))
elif p[1].strip() == "AX":
stack.append(AX(stack.pop()))
def p_unary_take_outer_token(p):
'''formula : GLOBAL formula
| NEXT formula
| FUTURE formula
'''
if len(stack) >= 1:
if p[1].strip() == "AG":
stack.append(AG(stack.pop()))
elif p[1].strip() == "AF":
stack.append(AF(stack.pop()))
elif p[1].strip() == "AX":
stack.append(AX(stack.pop()))
def p_unary_take_outer_token_prop(p):
'''formula : GLOBAL PROPOSITION
| NEXT PROPOSITION
| FUTURE PROPOSITION
'''
if len(stack) >= 1:
if p[1].strip() == "AG":
stack.append(AG(stack.pop()))
elif p[1].strip() == "AF":
stack.append(AF(stack.pop()))
elif p[1].strip() == "AX":
stack.append(AX(stack.pop()))
def p_binary_take_outer_token(p):
'''formula : formula CONJUNCTION formula
| formula DISJUNCTION formula
| formula EQUIVALENCE formula
| formula IMPLICATION formula
'''
if len(stack) >= 2:
a, b = stack.pop(), stack.pop()
if self.IMPLICATION.search(p[2].strip()) and not self.EQUIVALENCE.search(p[2].strip()):
stack.append(Or(a, Neg(b)))
elif self.EQUIVALENCE.search(p[2].strip()):
stack.append(And(Or(Neg(a), b), Or(Neg(b), a)))
else:
if p[2].strip() == "|":
stack.append(Or(b, a))
elif p[2].strip() == "&":
stack.append(And(b, a))
def p_binary_paren_take_outer_token(p):
'''formula : LPAREN formula RPAREN CONJUNCTION LPAREN formula RPAREN
| LPAREN formula RPAREN DISJUNCTION LPAREN formula RPAREN
| LPAREN formula RPAREN EQUIVALENCE LPAREN formula RPAREN
| LPAREN formula RPAREN IMPLICATION LPAREN formula RPAREN
'''
if len(stack) >= 2:
a, b = stack.pop(), stack.pop()
if self.IMPLICATION.search(p[4].strip()) and not self.EQUIVALENCE.search(p[4].strip()):
stack.append(Or(a, Neg(b)))
elif self.EQUIVALENCE.search(p[4].strip()):
stack.append(And(Or(Neg(a), b), Or(Neg(b), a)))
else:
if p[4].strip() == "|":
stack.append(Or(b, a))
elif p[4].strip() == "&":
stack.append(And(b, a))
def p_binary_lparen_take_outer_token(p):
'''formula : LPAREN formula RPAREN CONJUNCTION formula
| LPAREN formula RPAREN DISJUNCTION formula
| LPAREN formula RPAREN EQUIVALENCE formula
| LPAREN formula RPAREN IMPLICATION formula
'''
if len(stack) >= 2:
a = stack.pop()
b = stack.pop()
if self.IMPLICATION.search(p[4].strip()) and not self.EQUIVALENCE.search(p[4].strip()):
stack.append(Or(a, Neg(b)))
elif self.EQUIVALENCE.search(p[4].strip()):
stack.append(And(Or(Neg(a), b), Or(Neg(b), a)))
else:
if p[4].strip() == "|":
stack.append(Or(b, a))
elif p[4].strip() == "&":
stack.append(And(b, a))
def p_binary_rparen_take_outer_token(p):
'''formula : formula CONJUNCTION LPAREN formula RPAREN
| formula DISJUNCTION LPAREN formula RPAREN
| formula EQUIVALENCE LPAREN formula RPAREN
| formula IMPLICATION LPAREN formula RPAREN
'''
if len(stack) >= 2:
a = stack.pop()
b = stack.pop()
if self.IMPLICATION.search(p[4].strip()) and not self.EQUIVALENCE.search(p[4].strip()):
stack.append(Or(a, Neg(b)))
elif self.EQUIVALENCE.search(p[4].strip()):
stack.append(And(Or(Neg(a), b), Or(Neg(b), a)))
else:
if p[4].strip() == "|":
stack.append(Or(b, a))
elif p[4].strip() == "&":
stack.append(And(b, a))
def p_proposition_take_token_paren(p):
'''formula : LPAREN formula RPAREN
'''
stack.append(p[2].strip())
def p_proposition_take_token_atom(p):
'''formula : LPAREN PROPOSITION RPAREN
'''
stack.append(p[2].strip())
def p_proposition_take_token(p):
'''formula : PROPOSITION
'''
stack.append(p[1].strip())
def p_true_take_token(p):
'''formula : TRUE
'''
stack.append(p[1].strip())
def p_false_take_token(p):
'''formula : FALSE
'''
stack.append(p[1].strip())
# Error rule for syntax errors
def p_error(p):
print "Syntax error in input!: " + str(p)
os.system("pause")
return 0
我可以看到lex \ yacc规则相当混乱,为了简洁和整洁,我删除了每条规则中的大部分调试代码但是有人能看到我在哪里出错吗?我应该将括号处理移到另一种方法还是可以用现在的方法完成?是否有其他方法可以将这些公式字符串处理到预定义的类操作中而不会获得所有的shift / reduce错误?
很抱歉在线播放我所有的脏代码,但我真的可以帮助那些困扰我好几个月的东西了。感谢。
答案 0 :(得分:2)
开始小!!!无论你最终使用哪个解析器库,尝试只做一个简单的二进制操作,如expr&amp; expr,并使其工作。然后添加对“|”的支持。现在你有两个不同的运算符,你有足够的代表操作的优先级,而括号实际上起了作用。这个BNF看起来像是:
atom := TRUE | FALSE | '(' expr ')'
and_op := atom '&' atom
or_op := and_op '|' and_op
expr = or_op
你看到它是如何工作的吗?没有明确的“左转”,“流行权利”的东西。找出你的其他操作的优先级,然后扩展这个递归中缀表示法解析器以反映它们。但是,除非你先把这个最小的位工作,否则不要做任何事情。否则,你只是想立刻解决太多问题。
答案 1 :(得分:2)
您的BNF与您的lexing代码并不完全匹配,因为您的代码包含对'&lt; - &gt;'和' - &gt;'的引用运算符,赋值语句和我假设的命题基本上是一个小写标识符。我找了这种语言的在线参考,但没有找到。此外,您没有发布任何测试用例。所以我最好猜测你的语言BNF应该是什么。
"""
phi ::= p
!p
phi_0 & phi_1
phi_0 | phi_1
AX phi
AF phi
AG phi
AU phi_0 U phi_1
"""
from pyparsing import *
LPAR,RPAR = map(Suppress,"()")
NOT = Literal("!")
TRUE = Keyword("TRUE")
FALSE = Keyword("FALSE")
AX, AF, AG, AU, U = map(Keyword, "AX AF AG AU U".split())
AND_OP = "&"
OR_OP = "|"
ident = Word(alphas.lower())
phi = Forward()
p = Optional(NOT) + (TRUE | FALSE | ident | Group(LPAR + phi + RPAR) )
binand = p + ZeroOrMore(AND_OP + p)
binor = binand + ZeroOrMore(OR_OP + binand)
phi << (
Group(AX + phi) |
Group(AF + phi) |
Group(AG + phi) |
Group(AU + phi + U + phi) |
binor)
assign = ident + "=" + Group(phi)
equiv = Group(phi) + "<->" + Group(phi)
implicate = Group(phi) + "->" + Group(phi)
statement = assign | equiv | implicate
tests = """\
a=TRUE
b = FALSE
c = !TRUE
d <-> b & !c
AG b & d -> !e""".splitlines()
for t in tests:
print statement.parseString(t).asList()
打印:
['a', '=', ['TRUE']]
['b', '=', ['FALSE']]
['c', '=', ['!', 'TRUE']]
[['d'], '<->', ['b', '&', '!', 'c']]
[[['AG', 'b', '&', 'd']], '->', ['!', 'e']]
Group类帮助将结果组织成准AST。在pyparsing wiki上有很多例子可以帮助你从这里开始。我建议查看simpleBool.py示例,了解如何让解析器生成一个求值程序。