I’m learning flex and met a issue about code block in rule section.
In flex’s manual http://westes.github.io/flex/manual/Comments-in-the-Input.html#Comments-in-the-Input, there’s a code block in rule section:
%{
/* code block */
%}
/* Definitions Section */
%x STATE_X
%%
/* Rules Section */
ruleA /* after regex */ { /* code block */ } /* after code block */
/* Rules Section (indented) */
<STATE_X>{
ruleC ECHO;
ruleD ECHO;
%{
/* code block */
%}
}
%%
/* User Code Section */
You could see there’s a second code block between two %%
, I have two questions:
YY_USER_ACTION
?A code block in the rules section has unpredictable results unless:
It occurs before the first pattern, or
It contains nothing other than white space or comments.
This particular code block consists only of white space and a comment. So the question of when it executes is pretty zen. (In the "sound of one hand clapping" sense.) It does nothing. When? Well, whenever. Nothing is hard to observe.
YY_USER_ACTION
happens just after the pattern is recognised, before the rule action (even if that action is empty). If you don't define YY_USER_ACTION
, it also does nothing so I suppose there is no difference from a comment. But normally it's defined to do something, and it is inserted in every rule, not just one place. So that's completely different.