diff --git a/Makefile b/Makefile index 66edd15..e492595 100644 --- a/Makefile +++ b/Makefile @@ -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 : diff --git a/compile.c b/compile.c index a04e2b0..77ca3c6 100644 --- a/compile.c +++ b/compile.c @@ -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= YY_REALLOC(G->buf, G->buflen, G->data);\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\ @@ -481,7 +480,7 @@ YY_LOCAL(int) yymatchChar(GREG *G, int c)\n\ return 0;\n\ }\n\ \n\ -YY_LOCAL(int) yymatchString(GREG *G, char *s)\n\ +YY_LOCAL(int) yymatchString(GREG *G, const char *s)\n\ {\n\ int yysav= G->pos;\n\ while (*s)\n\ @@ -518,7 +517,7 @@ YY_LOCAL(void) yyDo(GREG *G, yyaction action, int begin, int end)\n\ while (G->thunkpos >= G->thunkslen)\n\ {\n\ G->thunkslen *= 2;\n\ - G->thunks= YY_REALLOC(G->thunks, sizeof(yythunk) * G->thunkslen, G->data);\n\ + G->thunks= (yythunk*)YY_REALLOC(G->thunks, sizeof(yythunk) * G->thunkslen, G->data);\n\ }\n\ G->thunks[G->thunkpos].begin= begin;\n\ G->thunks[G->thunkpos].end= end;\n\ @@ -536,7 +535,7 @@ YY_LOCAL(int) yyText(GREG *G, int begin, int end)\n\ while (G->textlen < (yyleng - 1))\n\ {\n\ G->textlen *= 2;\n\ - G->text= YY_REALLOC(G->text, G->textlen, G->data);\n\ + G->text= (char*)YY_REALLOC(G->text, G->textlen, G->data);\n\ }\n\ memcpy(G->text, G->buf + begin, yyleng);\n\ }\n\ @@ -606,13 +605,13 @@ YY_PARSE(int) YY_NAME(parse_from)(GREG *G, yyrule yystart)\n\ if (!G->buflen)\n\ {\n\ G->buflen= YY_BUFFER_START_SIZE;\n\ - G->buf= YY_ALLOC(G->buflen, G->data);\n\ + G->buf= (char*)YY_ALLOC(G->buflen, G->data);\n\ G->textlen= YY_BUFFER_START_SIZE;\n\ - G->text= YY_ALLOC(G->textlen, G->data);\n\ + G->text= (char*)YY_ALLOC(G->textlen, G->data);\n\ G->thunkslen= YY_STACK_SIZE;\n\ - G->thunks= YY_ALLOC(sizeof(yythunk) * G->thunkslen, G->data);\n\ + G->thunks= (yythunk*)YY_ALLOC(sizeof(yythunk) * G->thunkslen, G->data);\n\ G->valslen= YY_STACK_SIZE;\n\ - G->vals= YY_ALLOC(sizeof(YYSTYPE) * G->valslen, G->data);\n\ + G->vals= (YYSTYPE*)YY_ALLOC(sizeof(YYSTYPE) * G->valslen, G->data);\n\ G->begin= G->end= G->pos= G->limit= G->thunkpos= 0;\n\ }\n\ G->pos = 0;\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\ diff --git a/greg.c b/greg.c index 2353184..dab8815 100644 --- a/greg.c +++ b/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); } diff --git a/greg.g b/greg.g index 558e9ae..02332e1 100644 --- a/greg.g +++ b/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; \ diff --git a/samples/calc.leg b/samples/calc.leg index 0dd91ed..370b288 100644 --- a/samples/calc.leg +++ b/samples/calc.leg @@ -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; }