Home > Articles, Compilers & Interpreters > Ignoring multiline comments in compilers with Flex.

Ignoring multiline comments in compilers with Flex.

In your lexer when lexical analysis is performed you may want to ignore any multiline C-like comments.

One way to achieve this is to add to your regular expressions in Flex lexer also the following:

"/*"     { /* ignore multiline comments. */
           register int c;

           for (;;)
           {
             while ((c = input ()) != '*' && c != EOF)
               ; /* eat up text of comment. */

             if (c == '*')
             {
               while ((c = input ()) == '*')
                 ;

               if (c == '/')
                 break; /* found the end. */
             }

             if (c == EOF)
             {
               printf ("Error: EOF in comment.\n");
               break;
             }
           }
         }
  1. Luizao
    October 7, 2015 at 21:46

    Thank you very much, you saved my life.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: