flex 开始条件(匹配字符串文字)

发布于 2024-12-29 20:54:05 字数 557 浏览 4 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2025-01-05 20:54:05

您不返回yytext,而是返回一个指向string_buf的指针。

yytext 包含终止符,因为它是匹配状态的最后一个正则表达式的内容。在示例的所有其他情况下(除了终止符),yytext 的内容将复制到 string_buf (例如,检查带有 *string_buf_ptr++=*yptr++; 的行;),以便缓冲区保存最终的字符串。

You do not return yytext but you return a pointer to string_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 of yytext is copied to string_buf (e.g. check the lines with *string_buf_ptr++=*yptr++;), so that buffer holds the final string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文