【问题标题】:flex Start Conditions (matching string literals)flex 开始条件(匹配字符串文字)
【发布时间】:2012-01-31 19:45:27
【问题描述】:

我正在实现匹配 flex manual 中的 C 样式字符串的起始条件。

我关心的部分是:

     <str>\"        { /* saw closing quote - all done */
             BEGIN(INITIAL);
             *string_buf_ptr = '\0';
             /* return string constant token type and
              * value to parser
              */
             }

我返回令牌类型没有问题,但我不确定在这种情况下如何传递字符串值。如果我在返回令牌时打印 yytext,它只是持有 " 终止符。

那么我该如何获取字符串的值呢?

提前致谢;我是 flex 新手。

【问题讨论】:

  • 不是string_buf_ptr里面的值吗?只需将其复制到 yylval(如果可重入不成问题,则直接使用它)。

标签: compiler-construction flex-lexer


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 2017-04-16
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多