Search code examples
regexunixflex-lexer

Unix Flex Regex for Multi-Line Comments


I am making a Lexical Analyzer using Flex on Unix. If you've ever used it before you know that you mainly just define the regex for the tokens of whatever language you are writing the Lexical Analyzer for. I am stuck on the final part. I need the correct Regex for multi-line comments that allows something like

/* This is a comment \*/

but also allows

/* This **** //// is another type of comment */

Can anyone help with this?


Solution

  • You don't match C style comments with a simple regular expression in Flex; they require a more complex matching method based on start states. The Flex FAQ says how (well, they do for the /*...*/ form; handling the other form in just the <INITIAL> state should be simple).