我不知道如何在我的语法中解决这种转变/减少冲突。
#coding: utf-8
import ply.lex as lex
import ply.yacc as yacc
tokens = [
'LPAREN',
'RPAREN',
'OR',
'KEYWORD',
]
t_KEYWORD = r'[A-Za-z]{1,100}'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_OR = r'\|'
def t_error(t):
print("Illegeal characters")
t.lexer.skip(1)
lexer = lex.lex()
test1 = '((C|D)|A|B)'
test2 = '(A|B|(C|D))'
def p_expression_normal(p):
'''
expression : KEYWORD
| LPAREN KEYWORD OR KEYWORD RPAREN
'''
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = [p[2], p[3], p[4]]
print p[0]
def p_expression_operation(p):
'''
expression : LPAREN expression OR expression OR expression RPAREN
'''
p[0] = [p[2], p[3], p[4], p[5], p[6]]
print p[0]
def p_error(p):
print("Syntax error in input!")
parser = yacc.yacc()
print 'test1 start....................'
parser.parse(test1, lexer=lexer)
print 'test2 start....................'
parser.parse(test2, lexer=lexer)
有2个变量'test1':'((C | D)| A | B)'和'test2':'(A | B |(C | D))'实际上是相同的逻辑。< / p>
在控制台中,它显示“警告:1班次/减少冲突”。 'test1'给出了正确的结果,但test2中有错误。
WARNING: 1 shift/reduce conflict
test1 start....................
['C', '|', 'D']
['A']
['B']
[['C', '|', 'D'], '|', ['A'], '|', ['B']]
test2 start....................
Syntax error in input!
['C', '|', 'D']
Syntax error in input!
有人可以帮忙解决这个问题吗?非常感谢。