help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Conflicts shift/reduction


From: Eduardo Robles Elvira
Subject: Conflicts shift/reduction
Date: Wed, 13 Apr 2005 21:16:09 +0200
User-agent: KMail/1.7.2

Hello everybody !

I'm developing an Interpreter with flex & bison. The grammatical parser (with 
bison) part is mostly working; the problem is that there are a few problems I 
have yet to solve;

Normally bison tells me:
lea.y: conflicts: 2 shift/reduce, 4 reduce/reduce

I haven't found it to miswork even with that, so I don't think that it's 
problematic. Now I've been changing recursivity from left to right, and it 
has been a successful change, but one problem emerged, and is a 
shift/reduction conflict. When I turn this code:
<--------
elif_statement:
 '|' expr_bool cond_start
  sentence_list
  { $$ = TRelif_statement($2, $4); }
;

elif_statement_list:
 elif_statement_list elif_statement
  { $$ = TRelif_statement_list($2, $1); }
 | elif_statement
  { $$ = TRelif_statement_list($1, NULL); }
-------->
Into this other:
<--------
elif_statement:
 '|' expr_bool cond_start
  sentence_list
  { $$ = TRelif_statement($2, $4); }
;

elif_statement_list:
 elif_statement elif_statement_list
  { $$ = TRelif_statement_list($1, $2); }
 | elif_statement
  { $$ = TRelif_statement_list($1, NULL); }
-------->
This is the bison output:
lea.y: conflicts: 3 shift/reduce, 4 reduce/reduce
lea.y:475.11-476.57: warning: rule never reduced because of conflicts: 
elif_statement_list: elif_statement

How can I solve it ?

The other major problem is with my implementation of boolean expresions and 
plain expresions. It's convenient to me to separe both of them, because it's 
the right thing to do (TM).  But it happens that a boolean expression can 
also be considered a expression. right now, I have only this implemented (see 
the comment inside):
<--------
expr:
 INT_VAL
  { $$ = TRexpr_int($1); }
 // TODO: Sometimes a expr_bool can also be considered an expr,
 // for example when it reffers to a boolean variable, but if we
 // add here expr_bool, 91 reduce/reduce problems appear and
 // parsing becomes broken!
 | BOOL_VAL
  { $$ = TRexpr_expr_bool($1); }
-------->

The code for both of them, expr and expr_bool is large:
<--------
expr_bool:
 BOOL_VAL
  { $$ = TRexpr_bool_val($1); }
 | '(' expr_bool ')'
  { $$ = $2; }
 | struct_call
  { $$ = TRexpr_bool_struct($1); }
 | NOT_OP expr_bool %prec NEG
  { $$ = TRexpr_bool_not($2); }
 | expr_bool AND_OP expr_bool
  { $$ = TRexpr_bool_log('&', $1, $3); }
 | expr_bool OR_OP expr_bool
  { $$ = TRexpr_bool_log('|', $1, $3); }
 | expr '=' expr
  { $$ = TRexpr_bool('=', $1, $3); }
 | expr '<' expr
  { $$ = TRexpr_bool('<', $1, $3); }
 | expr '>' expr
  { $$ = TRexpr_bool('>', $1, $3); }
 | expr LE_OP expr
  { $$ = TRexpr_bool('l', $1, $3); }
 | expr GE_OP expr
  { $$ = TRexpr_bool('g', $1, $3); }
 | expr NOT_EQ expr
  { $$ = TRexpr_bool('n', $1, $3); }
 | function_call
  { $$ = TRexpr_bool_fcall($1); }
;

expr:
 INT_VAL
  { $$ = TRexpr_int($1); }
 // TODO: Sometimes a expr_bool can also be considered an expr,
 // usually when it reffers to a boolean variable, but if we
 // add here expr_bool 91 reduce/reduce problems appear!
 | BOOL_VAL
  { $$ = TRexpr_expr_bool($1); }
 | FLOAT_VAL
  { $$ = TRexpr_float($1); }
 | STR_VAL
  { $$ = TRexpr_str($1); }
 | struct_call
  { $$ = TRexpr_struct($1); }
 | expr '+' expr
  { $$ = TRexpr('+', $1, $3); }
 | expr '-' expr
  { $$ = TRexpr('-', $1, $3); }
 | expr '*' expr
  { $$ = TRexpr('*', $1, $3); }
 | expr '/' expr
  { $$ = TRexpr('/', $1, $3); }
 | expr '%' expr
  { $$ = TRexpr('%', $1, $3); }
 | expr '^' expr
  { $$ = TRexpr('^', $1, $3); }
 | '-' expr %prec NEG
  { $$ = TRexpr('n', $2, NULL); }
 | '(' expr ')'
  { $$ = $2; }
 | function_call
  { $$ = TRexpr_fcall($1); }
;
-------->

You can see the whole code in the SVN repository: 
http://svn.berlios.de/viewcvs/lea/trunk/ . *Any* help is appreciated !


Thanks for your time,
          Edulix.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]