Let's say I have the following rules:
rule1 : TOKEN rule2 TOKEN { printf("Found TOKEN\n") ; }
rule2 : ATOKEN { printf("Found ATOKEN\n") ; }
Here, the output will be the following:
Found ATOKEN
Found TOKEN
Because rule2
will be reduced first. Is there any way I can print something as soon as I get TOKEN
?
N.B I know I can do that from the scanner when it matches the TOKEN, but I need to print it from bison.
Bison allows actions to be within a rule (known as mid-rule actions).
In your example they could be used like this:
rule1 : TOKEN { printf("Found TOKEN 1\n"); } rule2 TOKEN { printf("Found TOKEN\n") ; }
rule2 : ATOKEN { printf("Found ATOKEN\n") ; }
but you should read the documentation carefully to see if this has the semantic effect that you desire.