Shift/reduce conflits on ocamlyacc

140 views Asked by At

I have the following parser:

%{

open t
open Lexer

%}
%token <int> INT
%token <float> FLOAT
%token <char> CHAR
%token <bool> BOOL

    %token PLUS Menos Mult Div Bigger Smaller MINUS TIMES 
    %token Equals Atribuicao SoE BoE And Or
    %token IF THEN ELSE BEGIN END FUNCTION WHILE SEQ RANGE DEF RETURN OF TO PV VIR DD
    %token RP LP SPL SPR LB RB 
    %token EOL




        %left PLUS MINUS        /* lowest precedence */
        %left TIMES Div         /* medium precedence */
        %nonassoc UMINUS       /* highest precedence */
        %start main            /* the entry point */

%type <int> main %%            
main:
| expr EOL                { $1 }

    expr:           
      INT               { $1 }  
    | IF LP expr RP         { $3 }
    | BEGIN expr END        { $2 }
    | RETURN expr PV        { $2 }
    | LP expr RP            { $2 }
    | expr PLUS expr        { $1 + $3 }
    | expr MINUS expr       { $1 - $3 }
    | expr TIMES expr           { $1 * $3 }
    | expr Div expr             { $1 / $3 }
    | MINUS expr %prec UMINUS   { - $2 }

I know that it is not complete and I am still building it and learning how to use ocamlyacc/menhir properly.

Well when I add this line:| expr Bigger expr {$1 > $3} it gives me 10 shift/reduce conflicts... But when I add this line with parentheses like this:

| LP expr RP Bigger LP expr RP {$2 > $6} LP represents '(' and RP represents ')'

It fits perfectly and gives me no errors and no shift/reduce conflicts

Is this the correct solution of the problem? If it is, then why? One more question, Am I doing something wrong on my parser? Or it is just not complete?

Thanks and sorry for something!

1

There are 1 answers

1
kdhp On BEST ANSWER

Bigger and Smaller need to be declared as %left or %right
associative along with the math operations:

%left Bigger Smaller
%left PLUS MINUS
%left TIMES Div
%nonassoc UMINUS