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

Ignoring multiline comments with start states in compilers with Flex.

In a previous article I have presented a way for ignoring multiline comments with an old fashion way.

In this article I’ll demonstrate a more elegant Flex-like way for ignoring multiline comments.

For implementing this in a more Flex-like way we can use the start states feature of Flex.

So, we create an exclusive start state named COMMENT and handle the context of comments.

Below, is the implementation of ignoring multiline comments in compiler / interpreter lexers:

%x COMMENT

%%
"/*" { BEGIN(COMMENT); }

<COMMENT>"*/" { BEGIN(INITIAL); }

<COMMENT>(.|\n) ;

<COMMENT><<EOF>> { yyerror("Unexpected file end (unterminated comment).\n"); }
%%
  1. No comments yet.
  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: