I want to use the flex with string buffer, not the default stdin, so I'm using yy_scan_string
. It almost works fine, except the "ends-with" pattern.
e.g.
%%
ab$ {//do something}
%%
ab$
means matching "ab" if it is exact the ending string, using stdin as input, it works, but with yy_scan_string
, it's not.
Test:
%option noyywrap
%{
#include <stdio.h>
%}
%%
ab$ { printf("match ab$\n"); }
%%
int main(int argc, char ** argv)
{
if (argc > 1)
yy_scan_string(argv[1]);
yylex();
if (argc > 1)
yylex_destroy();
}
Then with flex test.l; gcc lex.yy.c;
if using echo ab | ./a.out
, it prints match ab$
, and works fine, but if using ./a.out ab
, it only prints the input ab
, not match that ab$
rule.