Solved

dangling if else problem causes curly braces not detected

Posted on 2008-11-13
Medium Priority
236 Views
I've tried to use the approach below, however then after using the %prec LOWER_THAN_ELSE

when I have a { it fails to detect it
``````%nonassoc TELSE
%nonassoc LOWER_THAN_TELSE

stmt : TIF '(' boolexp ')' stmt %prec LOWER_THAN_TELSE
| TIF '(' boolexp ')' stmt TELSE stmt
;
``````
0
Question by:kuntilanak
• 3
• 3

LVL 53

Expert Comment

ID: 22958165
>> when I have a { it fails to detect it

How does the { fit into these rules ? Can you show the rules you used as well as the sample input that fails ?
0

Author Comment

ID: 22959673
Here's an example:

the problem is if there's an else statement.. sorry..not the braces

TIF '(' boolexp ')' stmt %prec LOWER_THAN_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);

}

If I remove the else stmt it works

``````void main(void){
int d;
if (1 == 1)
d = 2;
else
d = 5;

}
``````
0

LVL 53

Expert Comment

ID: 22960569
>> If I remove the else stmt it works

What happens if it doesn't work ? Can you describe that as detailed as possible ? What's the parse path it follows ? Does it fail ?

>> Here's an example:

I meant all rules used (stmt most importantly, but also its sub-rules etc.)
0

Author Comment

ID: 22981397
here's my whole parse.y file and notice that I've removed the :

%prec LOWER_THAN_TELSE

as when I use it doesn't accept the if else...
parse.txt
0

LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 22982993
1) This line is not supposed to be there :

>> %token LOWER_THAN_ELSE

2) The order is wrong :

>> %nonassoc TELSE
>> %nonassoc LOWER_THAN_ELSE

has to be :

%nonassoc LOWER_THAN_ELSE
%nonassoc TELSE

because the else block binds to the closest if, thus TELSE has to have higher precedence.

3) You need to actually use LOWER_THAN_ELSE in order to solve the conflict :

stmt : TIF '(' boolexp ')' stmt %prec LOWER_THAN_ELSE
| TIF '(' boolexp ')' stmt TELSE stmt

Otherwise the parser does not know where to use the precedence rules.

4) What's this supposed to do ?

>>                         | TIF '(' expr
0

Author Comment

ID: 22985418
thanks now it's fixed
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll