]> Pileus Git - ~andy/ct/blob - parse.y
Update knots
[~andy/ct] / parse.y
1 %{
2 #define YYSTYPE char *
3 #include <stdio.h>
4 #include <glib.h>
5 extern FILE *yyin;
6 int yylex(void);
7 void yyerror(gpointer node, char const *s);
8 static GList *code = NULL;
9 static GList *data = NULL;
10 %}
11 %error-verbose
12 %parse-param {gpointer root};
13 %token START END DATA OUT
14 %%
15
16 input : all | all input ;
17
18 all   : data | code | print ;
19
20 data  : DATA {
21         static int i = 0;
22         data = g_list_prepend(data, g_strdup_printf(
23                 "static char data%d[] = \"%s\";\n",
24                 i, g_strescape($1, "")));
25         code = g_list_prepend(code, g_strdup_printf(
26                 "fwrite(data%d, sizeof(data%d)-1, 1, stdout);\n",
27                 i, i));
28         i++;
29 };
30
31 code  : START DATA END {
32         code = g_list_prepend(code, g_strdup_printf("%s\n", $2));
33 };
34
35 print : START OUT DATA END {
36         code = g_list_prepend(code, g_strdup_printf("printf(%s);\n", $3));
37 };
38
39 %%
40 gpointer parse(FILE *input, GList **_data, GList **_code) {
41         yyin = input;
42         yyparse(NULL);
43         *_data = data;
44         *_code = code;
45         return NULL;
46 }
47 void yyerror(gpointer root, char const *s) {
48         g_error("[%s]\n", s);
49 }