X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;ds=sidebyside;f=parse.y;fp=parse.y;h=11eb57cd4e58b4a7de31240ad69a9ebcc0fbf38d;hb=8001a0abc5f72c5055a76c566b8b6f04ba784b1c;hp=0000000000000000000000000000000000000000;hpb=506ae3a9437ff9f4b409cd133a6c96b442b1926d;p=~andy%2Fct diff --git a/parse.y b/parse.y new file mode 100644 index 0000000..11eb57c --- /dev/null +++ b/parse.y @@ -0,0 +1,49 @@ +%{ +#define YYSTYPE char * +#include +#include +extern FILE *yyin; +int yylex(void); +void yyerror(gpointer node, char const *s); +static GList *code = NULL; +static GList *data = NULL; +%} +%error-verbose +%parse-param {gpointer root}; +%token START END DATA OUT +%% + +input : all | all input ; + +all : data | code | print ; + +data : DATA { + static int i = 0; + data = g_list_prepend(data, g_strdup_printf( + "static char data%d[] = \"%s\\n\";\n", + i, g_strescape($1, ""))); + code = g_list_prepend(code, g_strdup_printf( + "fwrite(data%d, sizeof(data%d)-1, 1, stdout);\n", + i, i)); + i++; +}; + +code : START DATA END { + code = g_list_prepend(code, g_strdup_printf("%s\n", $2)); +}; + +print : START OUT DATA END { + code = g_list_prepend(code, g_strdup_printf("printf(%s);\n", $3)); +}; + +%% +gpointer parse(FILE *input, GList **_data, GList **_code) { + yyin = input; + yyparse(NULL); + *_data = data; + *_code = code; + return NULL; +} +void yyerror(gpointer root, char const *s) { + g_error("[%s]\n", s); +}