Flex Scanner 中的“input(yytext, yyleng)”函数从何而来?

问题描述 投票:0回答:1

我正在 edx 上做斯坦福大学的 CS143:编译器 我从 this repo

看到了这段代码
 /* string ends, we need to deal with some escape characters */
<STRING>\" {
    std::string input(yytext, yyleng);

    // remove the '\"'s on both sizes.
    input = input.substr(1, input.length() - 2);

    std::string output = "";
    std::string::size_type pos;
    
    if (input.find_first_of('\0') != std::string::npos) {
        yylval.error_msg = "String contains null character";
        BEGIN 0;
        return ERROR;    
    }

这是一个扫描器规则,用于匹配

string constants
中的
flex
。 我不明白的是
std::string input(yytext, yyleng);
从哪里来?

我认为这可能是 Flex 的例程,所以我尝试在 Flex 的手册中找到它。我发现了这个:

input() reads the next character from the input stream.

但这似乎不是我要找的。

所以我认为它可能来自图书馆,就像它们包含在文件

的顶部一样
#include <cool-parse.h>
#include <stringtab.h>
#include <utilities.h>
#include <string>

我看了一下,但没有发现任何东西。

我对

compiler
还很陌生,任何帮助将不胜感激。

c++ compiler-construction flex-lexer lexer
1个回答
0
投票

这里的

input
不是一个函数。这是一个
std::string
。 具体来说,它是对
std::string
构造函数的调用,它接受指向字符数组及其长度的指针。

请参阅 文档(变体 4)。

© www.soinside.com 2019 - 2024. All rights reserved.