c++compiler-constructionflex-lexerlexer

where does `input(yytext, yyleng)` function come from in flex scanner?


I'm doing Stanford's CS143: Compiler on edx I saw this code from 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;    
    }

it's a scanner rule for matching string constants in flex. What I don't understand is where does the std::string input(yytext, yyleng); from?

I though It could be a flex's routine so I tried to find it in the flex' manual. I found this:

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

But it doesn't seem like what I'm looking for.

So I though it could be from the libraries like they were included on the top of the file

#include <cool-parse.h>
#include <stringtab.h>
#include <utilities.h>
#include <string>

I gave them a look but haven't found anything.

I'm pretty new into compiler, any help would be much appreciated.


Solution

  • This input here is not a function. It's a std::string. Specifically, it's a call of a constructor of std::string which takes pointer to character array and its length.

    See documentation (variant 4).