Getting started in Lex / Flex

I use Flex and Bison for the parser generator, but I have problems with the start states in my scanner.

I use exclusive rules for commenting, but this grammar does not seem to match the specified tokens:

%x COMMENT

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

"=="                  { return EQUALEQUAL; }

.                     ;

In this simple example, the line:

// a == b

does not fully match the comment unless I include this rule:

<COMMENT>"=="             ;

How can I get around this without having to add all these tokens to my exclusive rules?

+5
source share
3 answers

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

in the documentation , as well as various options on the Internet.

, Flex:

   <INITIAL>{
     "//"              BEGIN(IN_COMMENT);
     }
     <IN_COMMENT>{
     \n      BEGIN(INITIAL);
     [^\n]+    // eat comment
     "/"       // eat the lone /
     }
+9

"+" [^ n]. , "==" , , -, . Flex , , "+", , , . COMMENT .

+2

:

, " "

*, , . , .

%x COMMENT

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

"=="                  { return EQUALEQUAL; }

.                     ;
0

All Articles