Commit 56cb6485 authored by Léo Grange's avatar Léo Grange
Browse files

fix shift/reduce conflicts with very simple rule

All binary operators have the same precedance due to a "feature" of yacc
that makes difficult to write something better without some
duplications.
parent 74aab7d4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -124,6 +124,7 @@ void NBinaryOperator::printNode(std::ostream &o) {

void NUnaryOperator::printNode(std::ostream &o) {
	o << tokenToString(op) << " ";
	exp->printNode(o);
}


+25 −5
Original line number Diff line number Diff line
@@ -81,11 +81,30 @@ std::unique_ptr<T> UNIQUE(T *ptr) { return std::unique_ptr<T>(ptr); }
%type <cond> cond


%type <token> unop
%type <token> binop
/*%type <token> unop
%type <token> binop*/
%type <token> compop


/* precedence to solve shift/reduce conflicts explicitly */
%precedence TOK_THEN
%precedence TOK_ELSE


/* operator associativity to avoid shift/reduce conflicts */
/*
%left TOK_OR
%left TOK_AND
%left TOK_LSHIFT TOK_RSHIFT
%left TOK_PLUS TOK_MINUS
%left TOK_MUL TOK_DIV
*/
%left TOK_OR TOK_AND TOK_LSHIFT TOK_RSHIFT TOK_PLUS TOK_MINUS TOK_MUL TOK_DIV ANY_BINOP
%type <token> binop
%type <token> unop

%right UNARY_MINUS


%start program

@@ -160,7 +179,8 @@ ident : TOK_ID { $$ = new NIdentifier(*$1); delete $1; }
literal : TOK_INT { $$ = new NInteger(*$1); delete $1; }
		;

binop : TOK_PLUS | TOK_MINUS | TOK_MUL | TOK_DIV | TOK_AND | TOK_OR ;
binop : TOK_PLUS | TOK_MINUS | TOK_MUL | TOK_DIV | TOK_AND | TOK_OR
	  | TOK_LSHIFT | TOK_RSHIFT;

compop : TOK_EQ | TOK_NE | TOK_GT | TOK_GE | TOK_LT | TOK_LE ;

@@ -175,8 +195,8 @@ variable : ident { $$ = new NBitfieldVariable(UNIQUE($1)); }

expr : variable { $$ = $1; }
	 | literal { $$ = $1; }
	 | unop expr { $$ = new NUnaryOperator($1, UNIQUE($2)); }
	 | expr binop expr { $$ = new NBinaryOperator($2, UNIQUE($1), UNIQUE($3)); }
	 | unop expr %prec UNARY_MINUS { $$ = new NUnaryOperator($1, UNIQUE($2)); }
	 | expr binop expr %prec ANY_BINOP { $$ = new NBinaryOperator($2, UNIQUE($1), UNIQUE($3)); }
	 | TOK_LPAR expr TOK_RPAR { $$ = $2; }
	 ;