移位/减少重叠层



我正在做一个 ply yacc 编译器,我有以下警告:

WARNING: 9 shift/reduce conflicts

以下是我创建语法树的规则。

def p_programme_statement(p):
''' programme : statement  '''
p[0] = AST.ProgramNode(p[1])
def p_programme_recursive(p):
''' programme : statement ENTER programme '''
p[0] = AST.ProgramNode([p[1]]+p[3].children)
def p_statement(p):
''' statement : assignation
| structure  '''
p[0] = p[1]
def p_expression_num_or_var(p):
'''expression : INT
| FLOAT 
| IDENTIFIER 
'''
p[0] = AST.TokenNode(p[1])
def p_expression_op(p):
''' expression : expression MUL_OP expression 
| expression ADD_OP expression
| expression COMPARATOR expression '''
p[0] = AST.OpNode(p[2], [p[1], p[3]])
def p_assign(p):
''' assignation : IDENTIFIER EQU expression '''
p[0] = AST.AssignNode([AST.TokenNode(p[1]),p[3]])

def p_statement_print(p):
''' statement : PRINT expression '''
p[0] = AST.PrintNode(p[2])

def p_structure_if(p):
'''structure : IF expression POINTS ENTER TAB programme '#' '''
p[0] = AST.IfNode([p[2],p[6]])
def p_structure_function(p):
'''structure : DEF expression '(' ')' POINTS ENTER TAB programme '#' '''
p[0] = AST.FunctionNode([p[2],p[8]])
def p_structure_while(p):
''' structure : WHILE expression POINTS ENTER TAB programme '#' '''
p[0] = AST.WhileNode([p[2],p[6]])
def p_expression_paren(p):
'''expression : '(' expression ')' '''
p[0] = p[2]

p_expression_op函数(带MUL_OP、ADD_OP、比较器(。但是我想不通解决,你有什么想法吗?

谢谢

正如@rici所说,我的代码缺少优先级顺序:

以下代码解决了该问题:

precedence = (
('left', 'ADD_OP'),
('left', 'MUL_OP'),
('left','COMPARATOR')
)

谢谢!

相关内容

  • 没有找到相关文章

最新更新