Xref: utzoo comp.lang.c:29211 comp.unix.wizards:22208 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!brutus.cs.uiuc.edu!ux1.cso.uiuc.edu!csrd.uiuc.edu!sp14.csrd.uiuc.edu!pommerel From: pommerel@sp14.csrd.uiuc.edu (Claude Pommerell) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: Lex and initial start conditions Message-ID: <1990May30.174745.1161@csrd.uiuc.edu> Date: 30 May 90 17:47:45 GMT References: <6342@crabcake> Sender: usenet@csrd.uiuc.edu (news) Reply-To: pommu@iis.ethz.ch (Claude Pommerell) Organization: Center for Supercomputing Research and Development Lines: 64 Lex rules that do not begin with any starting condition ... 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; %} \/\* { commentLevel = 1; BEGIN InCCom; } \/ { BEGIN NewCCom; } \* { BEGIN EndCCom; } \* { ++commentLevel; BEGIN InCCom; } \/ { if (--commentLevel) BEGIN InCCom; else BEGIN Text; } [^\*\/] { BEGIN InCCom; } [^\/\*] | \/ | \* ; ------------------------------------------------------------------- All the other (true regular context-free) rules start with initial condition . 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@iis.ethz.ch)