dangling if else problem causes curly braces not detected

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

kuntilanakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> 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 ?
kuntilanakAuthor Commented:
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

Infinity08Commented:
>> 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.)
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

kuntilanakAuthor Commented:
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
Infinity08Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kuntilanakAuthor Commented:
thanks now it's fixed
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.