?
Solved

dangling if else problem causes curly braces not detected

Posted on 2008-11-13
6
Medium Priority
?
236 Views
Last Modified: 2013-11-18
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
     ;

Open in new window

0
Comment
Question by:kuntilanak
  • 3
  • 3
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
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

by:kuntilanak
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;
    
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kuntilanak
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

by:
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

by:kuntilanak
ID: 22985418
thanks now it's fixed
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

750 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