From bb0bb0e18626f66c6b9c8164bdb43d3eeb55a879 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 23 Oct 2013 03:13:10 +0900 Subject: [PATCH] reentrant parser (but not for scanner) --- src/parse.y | 7 +++++-- src/scan.l | 11 ++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/parse.y b/src/parse.y index 885d055d..c2214071 100644 --- a/src/parse.y +++ b/src/parse.y @@ -11,15 +11,16 @@ struct parser_control { pic_state *pic; pic_value value; bool incomp; + int yynerrs; }; void init_scanner(const char *); void final_scanner(void); void yyerror(struct parser_control *, const char *); -int yylex(struct parser_control *); %} +%pure_parser %parse-param {struct parser_control *p} %lex-param {struct parser_control *p} @@ -118,6 +119,7 @@ void yyerror(struct parser_control *p, const char *msg) { puts(msg); + p->yynerrs++; } bool @@ -127,12 +129,13 @@ pic_parse(pic_state *pic, const char *str, pic_value *v) p.pic = pic; p.incomp = false; + p.yynerrs = 0; init_scanner(str); yyparse(&p); final_scanner(); - if (yynerrs > 0) { + if (p.yynerrs > 0) { p.value = pic_undef_value(); } diff --git a/src/scan.l b/src/scan.l index c9b55f74..3881b3f4 100644 --- a/src/scan.l +++ b/src/scan.l @@ -9,11 +9,12 @@ struct parser_control { pic_state *pic; pic_value value; bool incomp; + int yynerrs; }; static pic_value new_escaped_string(pic_state *, const char *); -#define YY_DECL int yylex (struct parser_control *p) +#define YY_DECL int yylex (YYSTYPE *yylvalp, struct parser_control *p) %} %option noinput @@ -43,11 +44,11 @@ string_elem [^\"\\]|"\\\""|"\\\\" "(" return tLPAREN; ")" return tRPAREN; "'" return tQUOTE; -{boolean} { yylval.datum = pic_bool_value(strcmp(yytext, "#t") == 0 || strcmp(yytext, "#true") == 0); return tBOOLEAN; } -{real} { yylval.datum = pic_float_value(atof(yytext)); return tNUMBER; } -{identifier} { yylval.datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; } +{boolean} { yylvalp->datum = pic_bool_value(strcmp(yytext, "#t") == 0 || strcmp(yytext, "#true") == 0); return tBOOLEAN; } +{real} { yylvalp->datum = pic_float_value(atof(yytext)); return tNUMBER; } +{identifier} { yylvalp->datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; } {string} { - yylval.datum = new_escaped_string(p->pic, yytext); + yylvalp->datum = new_escaped_string(p->pic, yytext); return tSTRING; }