Next: , Previous: , Up: FAQ   [Contents][Index]


How can I match C-style comments?

You might be tempted to try something like this:

"/*".*"*/"       // WRONG!

or, worse, this:

"/*"(.|\n)"*/"   // WRONG!

The above rules will eat too much input, and blow up on things like:

/* a comment */ do_my_thing( "oops */" );

Here is one way which allows you to track line information:

<INITIAL>{
"/*"              BEGIN(IN_COMMENT);
}
<IN_COMMENT>{
"*/"      BEGIN(INITIAL);
[^*\n]+   // eat comment in chunks
"*"       // eat the lone star
\n        yylineno++;
}