c++ - Flex lexer output modification -
how can use flex lexer in c++ , modify token's yytext
value? lets say, have rule this:
"/*" { char c; while(true) { c = yyinput(); if(c == '\n') ++mylineno; if (c==eof){ yyerror( "eof occured while processing comment" ); break; } else if(c == '*') { if((c = yyinput()) == '/'){ return(tokens::comment);} else unput(c); } } }
and want token tokens::comment
value of comment between /*
, */
. (the bove solution gives "/*" value.
additional, important tracking line number, i'm looking solution supporting it.
edit of course can modify yytext
, yyleng
values (like yytext+=1; yyleng-=1
, still cannot solve above problem)
i still think start conditions right answer.
%x c_comment char *str = null; void addtostring(char *data) { if(!str) { str = strdup(data); } else { /* handle string concatenation */ } } "/*" { begin(c_comment); } <c_comment>([^*\n\r]|(\*+([^*/\n\r])))* { addtostring(yytext); } <c_comment>[\n\r] { /* handle tracking, add string if desired */ } <c_comment>"*/" { begin(initial); }
i used following references:
http://ostermiller.org/findcomment.html
https://stackoverflow.com/a/2130124/1003855
you should able use similar regular expression handle strings.
Comments
Post a Comment