/* csv.y - CSV parser using Bison */ /* a "pure" api means communication variables like yylval won't be global variables, and yylex is assumed to have a different signature */ %define api.pure true /* change prefix of symbols from yy to "csv" to avoid clashes with any other parsers we may want to link */ %define api.prefix {csv} /* generate much more meaningful errors rather than the uninformative string "syntax error" */ %define parse.error verbose /* enable location tracking for better error reporting */ %locations /* Bison offers different %code insertion locations in addition to yacc's %{ %} construct. The "top" location is good for headers and feature flags like the _XOPEN_SOURCE we use here */ %code top { /* XOPEN for strdup */ #define _XOPEN_SOURCE 600 #include #include #include /* Bison versions 3.7.5 and above provide the YYNOMEM macro to allow our actions to signal the unlikely event that they couldn't allocate memory. Thanks to the Bison team for adding this feature at my request. :) YYNOMEM causes yyparse() to return 2. The following conditional define allows us to use the functionality in earlier versions too. */ #ifndef YYNOMEM #define YYNOMEM goto yyexhaustedlab #endif } %code requires { #include #include #include #include /* Use vector from collection library instead of custom linked lists */ #include #include } /* Add another argument in yyparse() so that we can communicate any parser state to the caller. We can't return the result directly, since the return value is already reserved as an int, with 0=success, 1=error, 2=nomem */ %parse-param {void *parser_state} /* param adds an extra param to yyparse (like parse-param) but also causes yyparse to send the value to yylex. In our case the caller will initialize their own scanner instance and pass it through */ %param {void *scanner} %code { /* Function declarations - updated for pure API */ int csverror(CSVLTYPE *locp, void *parser_state, void *scanner, const char *s); int csvlex(void *lval, CSVLTYPE *locp, void *scanner); bool one_empty_field(vector *fields); /* Interface with the CSV parser library */ void csv_parser_add_record(void *parser_state, vector *fields); void csv_parser_set_error_with_location(void *parser_state, const char *error, int line, int column); } %union { char *str; vector *fields; } %token CRLF %token FIELD %type field.opt %type record /* Bison memory management - automatically free memory on errors */ %destructor { free($$); } %destructor { if ($$) v_free($$); } %% file : consumed_record | file CRLF consumed_record ; /* A record can be constructed in two ways, but we want to run the same side effect for either case. We add an intermediate non-terminal symbol "consumed_record" just to perform the action. In library code, this would be a good place to send the the record to a callback function. */ consumed_record : record { /* a record comprised of exactly one blank field is a blank record, which we can skip */ if (!one_empty_field($1)) { /* Send the record to the parser library */ csv_parser_add_record(parser_state, $1); } /* Memory is automatically freed by %destructor */ } ; record : field.opt { /* Create vector and add first field */ $$ = v_new(); if (!$$) YYNOMEM; /* Set destructor for automatic cleanup */ v_dtor($$, derp_free, NULL); if (!v_append($$, $1)) { free($1); v_free($$); YYNOMEM; } } | record ',' field.opt { /* Append field to existing vector */ if (!v_append($1, $3)) { free($3); YYNOMEM; } $$ = $1; } ; field.opt : %empty { $$ = calloc(1,1); if (!$$) YYNOMEM; } | FIELD ; %% bool one_empty_field(vector *fields) { return fields && v_length(fields) == 1 && v_at(fields, 0) && ((char*)v_at(fields, 0))[0] == '\0'; } int csverror(CSVLTYPE *locp, void *parser_state, void *scanner, const char *s) { (void)scanner; /* Use location information if available */ if (locp) { csv_parser_set_error_with_location(parser_state, s, locp->first_line, locp->first_column); } else { csv_parser_set_error_with_location(parser_state, s, 0, 0); } return 0; }