• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 863
  • Last Modified:

dangling if else problem

I know to eliminate the shift reduce conflict is by using the %prec operator, I did already but still got this:

179: shift/reduce conflict (shift 190, reduce 36) on TELSE
state 179
      stmt : TIF '(' boolexp ')' stmt .  (36)
      stmt : TIF '(' boolexp ')' stmt . TELSE stmt  (37)

      TELSE  shift 190
      TID  reduce 36
      TINTCON  reduce 36
      TCHARCON  reduce 36
      TSTRCON  reduce 36
      TIF  reduce 36
      TWHILE  reduce 36
      TFOR  reduce 36
      TRETURN  reduce 36
      ';'  reduce 36
      '}'  reduce 36
      '{'  reduce 36


My rule is below:
%right ELSE
 
stmt			: 	TIF '(' boolexp ')' stmt %prec ELSE
					{
				
					/* tree */
					$$.node = MakeTreeStmt($3.node,	$5.node,	NULL,	NULL,	T_STMT_IFELSE,	NULL);
					
					}
				| TIF '(' boolexp ')' stmt TELSE stmt  
					{
					
					/* tree */
					$$.node = MakeTreeStmt($3.node,	$5.node,	$7.node,	NULL,	T_STMT_IFELSE,	NULL);
					
					}
				| TIF '(' expr %prec ELSE
					{
					fprintf(stderr,"Illegal if statement\n");
					fprintf(stderr,"...at Line number %d \n",linenumber );
					exit(0);
					}

Open in new window

0
kuntilanak
Asked:
kuntilanak
1 Solution
 
zmoCommented:
if/then/else is a common shift reduce problem...

      stmt : TIF '(' boolexp ')' stmt .  (36)
      stmt : TIF '(' boolexp ')' stmt . TELSE stmt  (37)

and by default yacc does what you want, ie a shift operation. You can try using %expect to do the same as the default without the warning message.


http://everything2.com/e2node/Shift%252FReduce%2520Conflict for a clear explanation.
0
 
Infinity08Commented:
The if-else shift/reduce conflict can be resolved like this :
%nonassoc TELSE
%nonassoc LOWER_THAN_TELSE
 
stmt : TIF '(' boolexp ')' stmt %prec LOWER_THAN_TELSE
     | TIF '(' boolexp ')' stmt TELSE stmt
     ;

Open in new window

0
 
kuntilanakAuthor Commented:
thanks infinity it works
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now