I’m implementing the start condition for matching C-style strings in the flex manual.
The segment I’m concerned with is:
<str>\" { /* saw closing quote - all done */
BEGIN(INITIAL);
*string_buf_ptr = '\0';
/* return string constant token type and
* value to parser
*/
}
I have no issue returning the token type, but I’m unsure how to pass the string value in this situation. If I print yytext when the token is returned, it’s simply holding the ” terminator.
So how would I get the string’s value?
Thanks in advance; I’m new to flex.
You do not return
yytextbut you return a pointer tostring_buf.yytextcontains the terminator because that is the content of the last regular expression for the matched state. In all other cases (but the terminator) of your example, the content ofyytextis copied tostring_buf(e.g. check the lines with*string_buf_ptr++=*yptr++;), so that buffer holds the final string.