OSDN Git Service

Make the lexer reentrant (to avoid "still reachable" memory).
authorCarl Worth <cworth@cworth.org>
Mon, 10 May 2010 18:52:29 +0000 (11:52 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 10 May 2010 18:52:29 +0000 (11:52 -0700)
This allows the final program to be 100% "valgrind clean", (freeing
all memory that it allocates). This will make it much easier to ensure
that any allocation that parser actions perform are also cleaned up.

glcpp-lex.l
glcpp-parse.y
glcpp.c

index 9779f2b..276f50d 100644 (file)
@@ -28,7 +28,7 @@
 #include "glcpp-parse.h"
 %}
 
-%option noyywrap
+%option reentrant noyywrap
 
 %%
 
index 739b293..9acd549 100644 (file)
 #define YYSTYPE int
 
 void
-yyerror (const char *error);
+yyerror (const char *error, void *scanner);
 
 %}
 
+%parse-param {void *scanner}
+%lex-param {void *scanner}
+
 %token TOKEN
 
 %%
@@ -51,7 +54,7 @@ token:                TOKEN
 %%
 
 void
-yyerror (const char *error)
+yyerror (const char *error, void *scanner)
 {
        fprintf (stderr, "Parse error: %s\n", error);
 }
diff --git a/glcpp.c b/glcpp.c
index 09641ce..90a0e89 100644 (file)
--- a/glcpp.c
+++ b/glcpp.c
 int
 main (void)
 {
-       return yyparse ();
+       int ret;
+       void *scanner;
+
+       yylex_init (&scanner);
+       ret = yyparse (scanner);
+       yylex_destroy (scanner);
+
+       return ret;
 }