0

I am trying to write a Yacc program that accepts assignment and if-elif-else statements. Also, if statements can be nested. In addition, it has to be tab sensitive.

However, I am getting 4 shift/reduce conflicts. Could you help me to find out what I am doing wrong?

Thank you all.

Here is my Yacc code:

IF_END-> :

ASSIGN-> =

COMPARISON-> == != < etc.

statements:
    
    statement
    |
    statement statements
    ;
    
statement:

    assignment_statement
    |
    if_else_statement
    ;
    
assignment_statement:

    VAR ASSIGN operand
    |
    VAR ASSIGN o_o_list
    ;
    
o_o_list:

    operand OPERATOR operand 
    |
    operand OPERATOR o_o_list
    ;
    
operand:

    VAR
    |
    INTEGER
    |
    FLOAT
    |
    STRING
    ;
    
if_else_statement:
    
    if elif else 
    |
    if 
    |
    if else 
    ;
    
    
if:

    IF comparison TAB statements 
    ;
    
elif:

     ELIF comparison TAB statements
    ;
    
else:

    ELSE IF_END TAB statements
    ;
    

comparison:

    operand COMPARISON operand IF_END
    ;

And this is my example input. I am getting an error at line 6 and it stops parsing.

x = 5
y = 7
z =  3.14
if x < z :
    if y < z :
        result = z * x - y      **//line 6**
        result = result / 2
    else:
        result = z * x + y
        result = result / 2
        if result > y :
            result = result / x
    y = x * 2
elif y < x :
    result = z
else:
    result = z * x * x * y
x = y
tuana
  • 1
  • 5
  • Else is a classic example of shift/reduce conflict. It's not an error, it's just a conflict and the parser by default chooses shift. You have a few different way to hide/solve the conflict: https://stackoverflow.com/a/1737613/3052438. This should not stop neither compilation nor execution. You probably have another problem which causes the error. – Piotr Siupa Jan 05 '23 at 12:46

0 Answers0