These days I am studying the flex and bison. In the work we often handle the string and comment.
So I store the code on my blog for reference in the future.
The code to handle the c string:
%x STR
%%
/" {BEGIN(STR);}
<STR>/" {BEGIN(INTITIAL);}
<STR>///" {skip();}
<STR>/n {error();}
<STR> . {skip();}
The code to handle the c comment:
%x COMMENT
%%
"/*" {BEGIN(COMMENT);}
<COMMENT>[^*/n]* {skip();}
<COMMENT>"*"+[^*/n]* {skip();}
<COMMENT>/n ++ line_num;
<COMMENT>"*"+"/" BEGIN(INITIAL);