Lex and initial start conditions

Claude Pommerell pommerel at sp14.csrd.uiuc.edu
Thu May 31 03:47:45 AEST 1990


Lex rules that do not begin with any starting condition <cond>... are valid for
ALL possible starting conditions. BEGIN 0 resets the Lex interpreter in
its initial
state where it has no explicit starting condition, so that only the
untagged rules
are valid.

There is a way to solve your problem, Jack. You know from the Lex manual
that every
text enclosed by lines starting with "%{" and "%}" is inserted literally
in the C
program generated by Lex. In fact, if you put this insertion text before
the first
line starting with "%%" (that is, in the definitions section of your Lex
source), it
gets inserted at the global scope of the C program, so this is perfect
to declare
externals and such.

However, if you put such an insertion text after "%%" (in the rules
section of your
Lex source), it gets inserted at the start of the body of the function
that performs
the lexical analysis, so you can use it to specify an initial condition.

This is my Lex source to skip nested C-like comments:
-------------------------------------------------------------------
%{
/* context in recursive C-like comments */
static int commentLevel;
%}
  /* Starting conditions to support recursive C-like comments */
%START  Text NewCCom InCCom EndCCom
%%
%{
/* Set the initial condition */
BEGIN Text;
commentLevel = 0;
%}
<Text>\/\*                      { commentLevel = 1; BEGIN InCCom; }
<InCCom>\/                      { BEGIN NewCCom; }
<InCCom>\*                      { BEGIN EndCCom; }
<NewCCom>\*                     { ++commentLevel; BEGIN InCCom; }
<EndCCom>\/                     { if (--commentLevel)
                                    BEGIN InCCom;
                                  else
                                    BEGIN Text;
                                }
<NewCCom,EndCCom>[^\*\/]        { BEGIN InCCom; }
<InCCom>[^\/\*]                 |
<NewCCom>\/                     |
<EndCCom>\*                     ;
-------------------------------------------------------------------
All the other (true regular context-free) rules start with initial
condition <Text>.

This solution seems to be portable. I used it on Alliant, Convex, and
Cray computers
without ever having trouble with it. I will report the fix in case I
have problems
porting it further.

							Claude Pommerell
							(pommy at iis.ethz.ch)



More information about the Comp.lang.c mailing list