tags:

views:

242

answers:

2

i want my yylex to parse a string rather than a file or stdout.how can v do it in solaris

+1  A: 

If you are using the real lex and not gnu flex I believe you can simply define your own

int input(void);

This can return characters from a string or whatever you want.

Alternatively, I believe you could write the string to a file, and open the file on stream yyin. I suspect this would work with either implementation.

If using GNU flex then I think you redefine the YYINPUT() macro,

DigitalRoss
A: 

Here is something that should work with any implementation, although risky by using popen.

$ cat a.l
%%
"abc" {printf("got ABC\n");}
"def" {printf("got DEF\n");}
. {printf("got [%s]\n", yytext);}
%%
int main(int argc, char **argv)
{
    return(lex("abcdefxyz"));
}
lex(char *s)
{
    FILE *fp;
    char *cmd;
    cmd=malloc(strlen(s)+16);
    sprintf(cmd, "/bin/echo %s", s); // major vulnerability here ...
    fp=popen(cmd, "r");
    dup2(fileno(fp), 0);
    return(yylex());
}
yywrap()
{
    exit(0);
}
$ ./a
got ABC
got DEF
got [x]
got [y]
got [z]
jlliagre