help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Weird Rule Matching


From: Tom
Subject: Weird Rule Matching
Date: Wed, 6 Apr 2022 22:10:01 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0

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.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]