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:


"/*" { BEGIN(COMMENT); }


<COMMENT>(.|\n) ;

<COMMENT><<EOF>> { yyerror("Unexpected file end (unterminated comment).\n"); }