I'm using Bison & Flex for 1 month more or less, so I'm sorry if I don't see something obvious (but I don't think it is).
I have a problem about freeing memory with Flex Bison. Here is what my code looks like:
parser.l
{DATE} { yylval.str= strdup(yytext);
pair<string,string> newpair = make_pair("DATE",yytext);
myvector.push_back(newpair);
return TOKEN_DATE ;}
This is one of the example of my .l file. I copy the value of yytext into yylval.str. Then I create a new pair with that content (key/value, actually), then I return the token date for bison. My parser .y is not more than yyparse; and when something is caught, it just prints it.
I tried to run valgrind on this and I have multiple errors concerning strdup. I know this function uses malloc, but I have no idea WHERE and WHEN to use FREE.
I probably guess it's in the .y file, but where ?
test:
TOKEN_DATE { cout << $1 << endl; // here ? and what to free ?}
I don't really get all of this, I would really appreciate a simple and clear explanation.
Thanks in advance,
EDIT:
I tried several things like:
test:
TOKEN_DATE TOKEN_TOTO TOKEN_BLABLA { cout << $1 << endl; free($1); free($2);}
| TOKEN_DATE test { cout << $1 << endl, free($1); }
It seems to compile and execute good, but valgrind still says to me that there is a problem with the malloc contained in the strdup function. But I cannot write free(yylval.str) inside the flex file, otherwise, bison will not be aware of the value (if I understood correctly, I tried it doesn't work). I really have no idea on how to remove this leaking problem.