lex program start conditions

A rule may be associated with any start condition.

However, the lex program recognizes the rule only when in that associated start condition. You can change the current start condition at any time.

Define start conditions in the definitions section of the specification file by using a line in the following form:
%Start  name1 name2

where name1 and name2 define names that represent conditions. There is no limit to the number of conditions, and they can appear in any order. You can also shorten the word Start to s or S.

When using a start condition in the rules section of the specification file, enclose the name of the start condition in <> (less than, greater than) symbols at the beginning of the rule. The following example defines a rule, expression, that the lex program recognizes only when the lex program is in start condition name1:
<name1> expression
To put the lex program in a particular start condition, execute the action statement in the action part of a rule; for instance, BEGIN in the following line:
BEGIN name1;

This statement changes the start condition to name1.

To resume the normal state, enter:
BEGIN 0;
or
BEGIN INITIAL;

where INITIAL is defined to be 0 by the lex program. BEGIN 0; resets the lex program to its initial condition.

The lex program also supports exclusive start conditions specified with %x (percent sign, lowercase x) or %X (percent sign, uppercase X) operator followed by a list of exclusive start names in the same format as regular start conditions. Exclusive start conditions differ from regular start conditions in that rules that do not begin with a start condition are not active when the lexical analyzer is in an exclusive start state. For example:
%s      one
%x      two
%%
abc     {printf("matched ");ECHO;BEGIN one;}
<one>def         printf("matched ");ECHO;BEGIN two;}
<two>ghi         {printf("matched ");ECHO;BEGIN INITIAL;}

In start state one in the preceding example, both abc and def can be matched. In start state two, only ghi can be matched.