dangling if else problem

Posted on 2008-11-06
Last Modified: 2013-11-18
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," Line number %d \n",linenumber );



Open in new window

Question by:kuntilanak
    LVL 5

    Expert Comment

    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. for a clear explanation.
    LVL 53

    Accepted Solution

    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


    Author Comment

    thanks infinity it works

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    PDF library for Delphi 2 81
    withoutString  challenge 40 119
    Add mobile access to browser application 3 108
    squareUp  challenge 22 80
    Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now