Skip to content
This repository has been archived by the owner on Jul 26, 2018. It is now read-only.

adding c++ support and adding yyinit/yydeinit to fix memory leaks #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
33 changes: 22 additions & 11 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down Expand Up @@ -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\
Expand All @@ -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\
Expand Down Expand Up @@ -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\
Expand All @@ -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\
Expand Down Expand Up @@ -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\
Expand Down Expand Up @@ -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\
Expand All @@ -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\
Expand Down
35 changes: 23 additions & 12 deletions greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion greg.g
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down
3 changes: 2 additions & 1 deletion samples/calc.leg
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}