flex 开始条件(匹配字符串文字)
我正在 flex 手册< 中实现匹配 C 风格字符串的启动条件/a>.
我关心的部分是:
<str>\" { /* saw closing quote - all done */
BEGIN(INITIAL);
*string_buf_ptr = '\0';
/* return string constant token type and
* value to parser
*/
}
我没有返回令牌类型的问题,但我不确定在这种情况下如何传递字符串值。如果我在返回令牌时打印 yytext,它只是保留 " 终止符。
那么我如何获取字符串的值?
提前致谢;我是 flex 的新手。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不返回
yytext
,而是返回一个指向string_buf
的指针。yytext
包含终止符,因为它是匹配状态的最后一个正则表达式的内容。在示例的所有其他情况下(除了终止符),yytext
的内容将复制到string_buf
(例如,检查带有*string_buf_ptr++=*yptr++; 的行;
),以便缓冲区保存最终的字符串。You do not return
yytext
but you return a pointer tostring_buf
.yytext
contains 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 ofyytext
is copied tostring_buf
(e.g. check the lines with*string_buf_ptr++=*yptr++;
), so that buffer holds the final string.