I can't figure out what do the following expressions mean?
I know basics of syntax for regex in flex and tried to figure out the meaning of regex but could not.I have been trying for 3 hours.
%%
"/*".*"*/" {int i = 0;
while (yytext[i]!='\0') {
if(yytext[i]=='\n')
{
lineno++;
colno=1;
}
else
colno++;
i++;
}
}
"//".*"\n" { lineno++; colno=1;}
(\"(.)*\") {colno+=strlen(yytext);}
(\'(.)\') {colno+=strlen(yytext);}
My question is to tell me the meaning of these four regular expressions in the code
.*
(or (.)*
, which is identical in meaning) matches the longest sequence of characters other than newline. Flex lets you quote characters either by putting them in doubke quotes ("//"
) or by using a backslash (\"
). So the four patterns match the longest sequence in the current line consisting of characters
From /*
up to */
From //
up to the end of the line
From "
up to "
From '
up to ``'`.
Only the second one will work as intended. All of the others will match too much if there are two matches on the same line, and the first one will not match multiline comments.
There isn't much to flex patterns other than the basics. All pattern syntaxes are described in a very short chapter of the flex manual.