You can solve this by allowing double quotes as an alternative to any single character in the string. At the moment you have a character in the string as [^"\n]
. To expand that to allow the double quotes as an alternative you write [^"\n]|\"\"
. A complete example flex program would look like this:
stringchar ([^\"\r\n]|\"\")
ws [ \t\r\n]+
%%
{ws} printf("Whitespace: \"%s\"\n",yytext);
\"{stringchar}*\" printf("String: %s\n",yytext);
[^ \t\r\n]+ printf("Not a string: \"%s\"\n",yytext);
%%
You should also note that flex has problems with matching adjoining tokens (see How to make lex/flex recognize tokens not separated by whitespace? ) for details. This means that "Hello"World"
gets matched as "Not a String"
and not a String
followed by "Not a String"
.