Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start states in Lex / Flex

I'm using Flex and Bison for a parser generator, but having problems with the start states in my scanner.

I'm using exclusive rules to deal with commenting, but this grammar doesn't seem to match quoted tokens:

%x COMMENT

//                    { BEGIN(COMMENT); }
<COMMENT>[^\n]        ;
<COMMENT>\n           { BEGIN(INITIAL); }

"=="                  { return EQUALEQUAL; }

.                     ;

In this simple example the line:

// a == b

isn't matched entirely as a comment, unless I include this rule:

<COMMENT>"=="             ;

How do I get round this without having to add all these tokens into my exclusive rules?

like image 381
Dan Avatar asked Jul 15 '09 10:07

Dan


1 Answers

Matching C-style comments in Lex/Flex or whatever is well documented:

in the documentation, as well as various variations around the Internet.

Here is a variation on that found in the Flex documentation:

   <INITIAL>{
     "//"              BEGIN(IN_COMMENT);
     }
     <IN_COMMENT>{
     \n      BEGIN(INITIAL);
     [^\n]+    // eat comment
     "/"       // eat the lone /
     }
like image 187
Aiden Bell Avatar answered Oct 12 '22 17:10

Aiden Bell