(flex.info)How can I match C-style comments?


Next: The period isn't working the way I expected. Prev: Why doesn't yyrestart() set the start state back to INITIAL? Up: FAQ
Enter node , (file) or (file)node

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++;
     }


automatically generated by info2www version 1.2.2.9