3 * monoburg.y: yacc input grammer
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2001 Ximian, Inc.
21 static int yylineno = 0;
22 static int yylinepos = 0;
41 %token <ivalue> INTEGER
48 %type <rule_list> rule_list
53 | START IDENT { start_nonterm ($2); } decls
55 | TERMPREFIX plist decls
56 | rule_list optcost optcode optcfunc {
58 for (tmp = $1; tmp; tmp = tmp->next) {
59 rule_add (tmp->data, $3, $2, $4);
65 rule : IDENT ':' tree { $$ = make_rule ($1, $3); }
68 rule_list : rule { $$ = g_list_append (NULL, $1); }
69 | rule ',' rule_list { $$ = g_list_prepend ($3, $1); }
72 optcode : /* empty */ { $$ = NULL; }
77 | plist IDENT { create_term_prefix ($2);}
81 | tlist IDENT { create_term ($2, -1);}
82 | tlist IDENT '=' INTEGER { create_term ($2, $4); }
85 tree : IDENT { $$ = create_tree ($1, NULL, NULL); }
86 | IDENT '(' tree ')' { $$ = create_tree ($1, $3, NULL); }
87 | IDENT '(' tree ',' tree ')' { $$ = create_tree ($1, $3, $5); }
90 optcost : /* empty */ {$$ = NULL; }
92 | INTEGER { $$ = g_strdup_printf ("%d", $1); }
95 optcfunc : /*empty */ { $$ = NULL; }
96 | COST CODE { $$ = $2; }
100 static char input[2048];
101 static char *next = input;
104 yyerror (char *fmt, ...)
110 fprintf (stderr, "line %d(%d): ", yylineno, yylinepos);
111 vfprintf (stderr, fmt, ap);
112 fprintf(stderr, "\n");
119 static int state = 0;
132 static struct pplist *pp = NULL;
135 getvar (const char *input)
137 char *var = g_strchug (g_strdup (input));
140 for (ptr = var; *ptr && *ptr != '\n'; ++ptr) {
141 if (g_ascii_isspace (*ptr)) {
151 push_if (char *input, gboolean flip)
153 struct pplist *new_pp = g_new (struct pplist, 1);
154 char *var = getvar (input);
156 new_pp->ignore = (g_hash_table_lookup (definedvars, var) == NULL) ^ flip;
159 new_pp->ignore |= (pp ? pp->ignore : 0);
168 yyerror ("%%else without %%if");
170 pp->ignore = !pp->ignore | (pp->next ? pp->next->ignore : 0);
176 struct pplist *prev_pp = pp;
179 yyerror ("%%endif without %%if");
195 if (!fgets (input, sizeof (input), inputfd))
198 ll = (input [0] == '%' && input [1] == '%');
202 if (!ll && input [0] == '%') {
203 if (!strncmp (&input [1], "ifdef", 5)) {
204 push_if (&input [6], FALSE);
208 else if (!strncmp (&input [1], "ifndef", 6)) {
209 push_if (&input [7], TRUE);
213 else if (!strncmp (&input [1], "else", 4)) {
218 else if (!strncmp (&input [1], "endif", 5)) {
224 if (pp && pp->ignore) {
235 fputs (input, outputfd);
246 ll = state != 1 || input[0] == '#';
249 } while (next_state == 2 || ll);
258 fputs (input, outputfd);
259 while (fgets (input, sizeof (input), inputfd))
260 fputs (input, outputfd);
270 if (!(c = nextchar ()))
273 yylinepos = next - input + 1;
279 if (!strncmp (next, "start", 5) && isspace (next[5])) {
284 if (!strncmp (next, "termprefix", 10) && isspace (next[10])) {
289 if (!strncmp (next, "term", 4) && isspace (next[4])) {
300 num = 10*num + (c - '0');
301 } while (isdigit (c = (*next++)));
311 if (!strncmp (next - 1, "cost", 4) && isspace (next[3])) {
316 while (isalpha (*n) || isdigit (*n) || *n == '_')
320 yylval.text = g_strndup (next - 1, l);
327 static char buf [100000];
329 while ((c = *next++) != '"' && c)
333 yylval.text = g_strdup (buf);
340 static char buf [100000];
342 while (d && (c = nextchar ())) {
344 assert (i < sizeof (buf));
347 case '{': d++; break;
348 case '}': d--; break;
354 yylval.text = g_strdup (buf);