Solved

# dangling if else problem

Posted on 2008-11-06
831 Views
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);

}
0
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.

http://everything2.com/e2node/Shift%252FReduce%2520Conflict for a clear explanation.
0

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

;

0

Author Comment

thanks infinity it works
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 81
withoutString  challenge 40 119
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.

#### Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!