How can I detect one line comments like "//" in Flex and skip this line?
And Also:
In case of the "/*" comments im using the bellow. Will it work?
"/*" { comment(); }
%%
comment()
{
char c, c1;
loop:
while ((c = input()) != '*' && c != 0)
putchar(c);
if ((c1 = input()) != '/' && c != 0)
{
unput(c1);
goto loop;
}
if (c != 0)
putchar(c1);
}
Why don't you just use regular expressions to recognize the comments? The whole point of
lex/flex
is to save you from having to write lexical scanners by hand. The code you present should work (if you put the pattern/*
at the beginning of the line), but it's a bit ugly, and it is not obvious that it will work.Your question says that you want to skip comments, but the code you provide uses
putchar()
to print the comment, except for the/*
at the beginning. Which is it that you want to do? If you want to echo the comments, you can use anECHO
action instead of doing nothing.Here are the regular expressions:
Single line comment
This one is easy because in lex/flex,
.
won't match a newline. So the following will match from//
to the end of the line, and then do nothing.Multiline comment
This is a bit trickier, and the fact that * is a regular expression character as well as a key part of the comment marker makes the following regex a bit hard to read. I use
[*]
as a pattern which recognizes the character *; in flex/lex, you can use"*"
instead. Use whichever you find more readable. Essentially, the regular expression matches sequences of characters ending with a (string of) * until it finds one where the next character is a /. In other words, it has the same logic as your C code.The above requires the terminating
*/
; an unterminated comment will force the lexer to back up to the beginning of the comment and accept some other token, usually a / division operator. That's likely not what you want, but it's not easy to recover from an unterminated comment since there's no really good way to know where the comment should have ended. Consequently, I recommend adding an error rule:To detect single line comments :
This says any line which starts with // will be considered as comment line.
To detect multi line comments :
*
Explanation :
*
^"/*"
This says beginning should be /*.[^*]*
includes all characters including \n but excludes *.[*]*
says 0 or more number of stars.[^*]|[*]*
- "or" operator is applied to get any string."*/"
specifies */ as end.This will work perfectly in lex.
Below is the complete code of lex file :
For
//
you can read until you find the end of line\n
orEOF
, in case if the comment was at the end of file, for example:as for multiple lines comments
/* */
, you can read until you see*
and peek the next character, if it's/
this means this is the end of comment, if not just skip it with any other character. You shouldn't expectEOF
, means unclosed comment:Complete file: