/* csv.l */ %{ #define _XOPEN_SOURCE 600 #include #include #include "csv.tab.h" %} %% \"([^"]|\"\")*\" { /* yyleng is precomputed strlen(yytext) */ size_t i, n = yyleng; char *s; s = yylval.str = calloc(n, 1); if (!s) return FIELD; /* copy yytext, changing "" to " */ for (i = 1 /*skip 0="*/; i < n-1; i++) { *s++ = yytext[i]; if (yytext[i] == '"') i++; /* skip second one */ } return FIELD; } [^",\r\n]+ { yylval.str = strdup(yytext); return FIELD; } \n|\r\n { return CRLF; } . { return *yytext; } %%