pythonyaccplyshift-reduce-conflict

Shift/reduce conflit ply yacc


i'm doing a ply yacc compiler and i'm having the following warning :

WARNING: 9 shift/reduce conflicts

Here are my rules to create a syntaxic tree.

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]

The conflits are on the p_expression_op function (with MUL_OP, ADD_OP, COMPARATOR). But I can't figure out to solve, do you have any idea ?

Thank you


Solution

  • As @rici said, precedence order was missing on my code:

    The following code solved the issue :

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

    Thank you !