Lex and Start Conditions

eifrig@server.cs.jhu.edu (Jonathan Eifrig)
24 Jan 91 03:59:26 GMT

          From comp.compilers

Related articles
Lex and Start Conditions eifrig@server.cs.jhu.edu (1991-01-24)
Re: Lex and Start Conditions bliss@sp64.csrd.uiuc.edu (1991-01-24)
Re: Lex and Start Conditions peter@objy.com (1991-01-24)
| List of all articles for this month |

Newsgroups: comp.compilers
From: eifrig@server.cs.jhu.edu (Jonathan Eifrig)
Summary: How can one specify a start state?
Keywords: lex, question
Organization: Compilers Central
Date: 24 Jan 91 03:59:26 GMT

Here's a Lex usage question concerning start conditions that I encountered:


I want to have my Lex-generated scanner strip out comments. Conceptually,
this is easy to do with start conditions. For example:


%Start NORM COM


%%


<NORM>{natnum}+ {return(ID);}


<NORM>"/*" {BEGIN COM;}


<COM>"*/" {BEGIN NORM;}


<COM>[^] { /* Do nothing */}






The idea is to have a simple little rule that eats a single character up and
discards it while searching for the close-comment symbol; thereby stripping
the comment out. So far, so good.


This works great, except that the automaton has to be started up in the
correct (meta) state (in this case, NORM). What is the best way to do this?
I've found two ways of doing this, neither of which is very pretty.


Option 1: Kick the automaton into the NORM state manually before parsing.
This basically involves having a main() like:


main()
{
...
BEGIN NORM;
yyparse();
}


Unfortunately, this requires importing the BEGIN macro into the main program
file, which is sort of unappealing.


Option 2: Use the undocumented INITIAL start condition. Substitute INITIAL
for NORM above. Unfortuately, INITIAL isn't a "real" start condition, so we
can't just BEGIN INITIAL, but have to use BEGIN 0, which is very cheezy. In
addition, I have no idea how portable it is. Using "undocumented features"
seems like a bad idea to me.


Does anyone have any other suggestions?


Jack Eifrig eifrig@cs.jhu.edu
[You could use exclusive states in flex. -John]
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.