Permalink
Browse files

add yyinit yydeinit and pass YY_INPUT G->data

fixes memory leaks
add make test to build sample and fix sample using new init/deinit
updated greg.g and greg.c
  • Loading branch information...
onnlucky authored and Amos Wenger committed Mar 29, 2011
1 parent c4ddf86 commit bd695b01673dac27e00c076db74786a0de7a26c1
Showing with 50 additions and 18 deletions.
  1. +10 −1 Makefile
  2. +14 −3 compile.c
  3. +23 −12 greg.c
  4. +1 −1 greg.g
  5. +2 −1 samples/calc.leg
View
@@ -29,9 +29,18 @@ grammar : .FORCE
./greg -o greg.c greg.g
clean : .FORCE
- rm -f *~ *.o *.greg.[cd]
+ rm -rf *~ *.o *.greg.[cd] greg samples/*.o samples/calc samples/calc.dSYM
spotless : clean .FORCE
rm -f greg
+samples/calc.c: samples/calc.leg greg
+ ./greg -o $@ $<
+
+samples/calc: samples/calc.c
+ $(CC) $(CFLAGS) -o $@ $<
+
+test: samples/calc
+ echo '21 * 2 + 0' | ./samples/calc | grep 42
+
.FORCE :
View
@@ -382,7 +382,7 @@ static char *preamble= "\
#define YY_NAME(N) yy##N\n\
#endif\n\
#ifndef YY_INPUT\n\
-#define YY_INPUT(buf, result, max_size) \\\n\
+#define YY_INPUT(buf, result, max_size, D) \\\n\
{ \\\n\
int yyc= getchar(); \\\n\
result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \\\n\
@@ -449,13 +449,12 @@ typedef struct _GREG {\n\
YY_LOCAL(int) yyrefill(GREG *G)\n\
{\n\
int yyn;\n\
- YY_XTYPE YY_XVAR = (YY_XTYPE) G->data;\n\
while (G->buflen - G->pos < 512)\n\
{\n\
G->buflen *= 2;\n\
G->buf= (char*)YY_REALLOC(G->buf, G->buflen, G->data);\n\
}\n\
- YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos));\n\
+ YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos), G->data);\n\
if (!yyn) return 0;\n\
G->limit += yyn;\n\
return 1;\n\
@@ -643,6 +642,17 @@ YY_PARSE(int) YY_NAME(parse)(GREG *G)\n\
return YY_NAME(parse_from)(G, yy_%s);\n\
}\n\
\n\
+YY_PARSE(void) YY_NAME(init)(GREG *G)\n\
+{\n\
+ memset(G, 0, sizeof(GREG));\n\
+}\n\
+YY_PARSE(void) YY_NAME(deinit)(GREG *G)\n\
+{\n\
+ if (G->buf) YY_FREE(G->buf);\n\
+ if (G->text) YY_FREE(G->text);\n\
+ if (G->thunks) YY_FREE(G->thunks);\n\
+ if (G->vals) YY_FREE(G->vals);\n\
+}\n\
YY_PARSE(GREG *) YY_NAME(parse_new)(YY_XTYPE data)\n\
{\n\
GREG *G = (GREG *)YY_CALLOC(1, sizeof(GREG), G->data);\n\
@@ -652,6 +662,7 @@ YY_PARSE(GREG *) YY_NAME(parse_new)(YY_XTYPE data)\n\
\n\
YY_PARSE(void) YY_NAME(parse_free)(GREG *G)\n\
{\n\
+ YY_NAME(deinit)(G);\n\
YY_FREE(G);\n\
}\n\
\n\
View
35 greg.c
@@ -36,7 +36,7 @@ struct _GREG;
void yyerror(struct _GREG *, char *message);
-# define YY_INPUT(buf, result, max) \
+# define YY_INPUT(buf, result, max, D) \
{ \
int c= getc(input); \
if ('\n' == c || '\r' == c) ++lineNumber; \
@@ -74,7 +74,7 @@ struct _GREG;
#define YY_NAME(N) yy##N
#endif
#ifndef YY_INPUT
-#define YY_INPUT(buf, result, max_size) \
+#define YY_INPUT(buf, result, max_size, D) \
{ \
int yyc= getchar(); \
result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \
@@ -141,13 +141,12 @@ typedef struct _GREG {
YY_LOCAL(int) yyrefill(GREG *G)
{
int yyn;
- YY_XTYPE YY_XVAR = (YY_XTYPE) G->data;
while (G->buflen - G->pos < 512)
{
G->buflen *= 2;
- G->buf= YY_REALLOC(G->buf, G->buflen, G->data);
+ G->buf= (char*)YY_REALLOC(G->buf, G->buflen, G->data);
}
- YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos));
+ YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos), G->data);
if (!yyn) return 0;
G->limit += yyn;
return 1;
@@ -173,7 +172,7 @@ YY_LOCAL(int) yymatchChar(GREG *G, int c)
return 0;
}
-YY_LOCAL(int) yymatchString(GREG *G, char *s)
+YY_LOCAL(int) yymatchString(GREG *G, const char *s)
{
int yysav= G->pos;
while (*s)
@@ -210,7 +209,7 @@ YY_LOCAL(void) yyDo(GREG *G, yyaction action, int begin, int end)
while (G->thunkpos >= G->thunkslen)
{
G->thunkslen *= 2;
- G->thunks= YY_REALLOC(G->thunks, sizeof(yythunk) * G->thunkslen, G->data);
+ G->thunks= (yythunk*)YY_REALLOC(G->thunks, sizeof(yythunk) * G->thunkslen, G->data);
}
G->thunks[G->thunkpos].begin= begin;
G->thunks[G->thunkpos].end= end;
@@ -228,7 +227,7 @@ YY_LOCAL(int) yyText(GREG *G, int begin, int end)
while (G->textlen < (yyleng - 1))
{
G->textlen *= 2;
- G->text= YY_REALLOC(G->text, G->textlen, G->data);
+ G->text= (char*)YY_REALLOC(G->text, G->textlen, G->data);
}
memcpy(G->text, G->buf + begin, yyleng);
}
@@ -946,13 +945,13 @@ YY_PARSE(int) YY_NAME(parse_from)(GREG *G, yyrule yystart)
if (!G->buflen)
{
G->buflen= YY_BUFFER_START_SIZE;
- G->buf= YY_ALLOC(G->buflen, G->data);
+ G->buf= (char*)YY_ALLOC(G->buflen, G->data);
G->textlen= YY_BUFFER_START_SIZE;
- G->text= YY_ALLOC(G->textlen, G->data);
+ G->text= (char*)YY_ALLOC(G->textlen, G->data);
G->thunkslen= YY_STACK_SIZE;
- G->thunks= YY_ALLOC(sizeof(yythunk) * G->thunkslen, G->data);
+ G->thunks= (yythunk*)YY_ALLOC(sizeof(yythunk) * G->thunkslen, G->data);
G->valslen= YY_STACK_SIZE;
- G->vals= YY_ALLOC(sizeof(YYSTYPE) * G->valslen, G->data);
+ G->vals= (YYSTYPE*)YY_ALLOC(sizeof(YYSTYPE) * G->valslen, G->data);
G->begin= G->end= G->pos= G->limit= G->thunkpos= 0;
}
G->pos = 0;
@@ -983,6 +982,17 @@ YY_PARSE(int) YY_NAME(parse)(GREG *G)
return YY_NAME(parse_from)(G, yy_grammar);
}
+YY_PARSE(void) YY_NAME(init)(GREG *G)
+{
+ memset(G, 0, sizeof(GREG));
+}
+YY_PARSE(void) YY_NAME(deinit)(GREG *G)
+{
+ if (G->buf) YY_FREE(G->buf);
+ if (G->text) YY_FREE(G->text);
+ if (G->thunks) YY_FREE(G->thunks);
+ if (G->vals) YY_FREE(G->vals);
+}
YY_PARSE(GREG *) YY_NAME(parse_new)(YY_XTYPE data)
{
GREG *G = (GREG *)YY_CALLOC(1, sizeof(GREG), G->data);
@@ -992,6 +1002,7 @@ YY_PARSE(GREG *) YY_NAME(parse_new)(YY_XTYPE data)
YY_PARSE(void) YY_NAME(parse_free)(GREG *G)
{
+ YY_NAME(deinit)(G);
YY_FREE(G);
}
View
2 greg.g
@@ -48,7 +48,7 @@
void yyerror(struct _GREG *, char *message);
-# define YY_INPUT(buf, result, max) \
+# define YY_INPUT(buf, result, max, D) \
{ \
int c= getc(input); \
if ('\n' == c || '\r' == c) ++lineNumber; \
View
@@ -41,8 +41,9 @@ EOL = '\n' | '\r\n' | '\r' | ';'
int main()
{
GREG g;
- memset(&g, 0, sizeof(GREG));
+ yyinit(&g);
while (yyparse(&g));
+ yydeinit(&g);
return 0;
}

0 comments on commit bd695b0

Please sign in to comment.