Weird Rule Matching

Tom <[email protected]> Wed, 6 Apr 2022 22:10:01 +0100
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
Hi,

Sorry if this is incorrect place to ask, this is my first time using a 
mailing list.

I have a language that I would like to parse:

if N == 2 {

     A = 100;

}

if N == 3 {

     B = 123;

}

To do this I have the relevant rules:

program:
       statement program
     | statement
     ;
statement:
     assignment
     | conditional
     | loop
     | END

conditional:
     IF comparison '{' END program '}' END {
         std::cout << "F: " << line_num << std::endl;
         F_CONDITIONAL_BLOCK -= 1;

         if (F_CONDITIONAL_BLOCK > 0) {
             F_CONDITIONAL_ELSE_BLOCK = 
PREVIOUS_F_CONDITIONAL_ELSE_BLOCK.top();
             PREVIOUS_F_CONDITIONAL_ELSE_BLOCK.pop();
         } else {
             F_CONDITIONAL_ELSE_BLOCK = false;
         }

         link_with_parent();
     }
My rule to match the IF token is as follows:

if        {
             std::cout << "S: " << line_num + 1 << std::endl;

             /* some extra, non-related code */

             return IF;
         };

For the above program, I get the following output:

S: 1

S: 4

F: 3

F: 6

What is weird here is that IF token on line 4 is matched *before* the 
conditional rule is matched on line 3.

I know I have probably done something wrong, I have mostly come up with 
this myself, but I am pulling my hair out here trying to figure the 
issue out.

Tom.