26 |
#include "getargs.h" |
#include "getargs.h" |
27 |
#include "files.h" |
#include "files.h" |
28 |
#include "symtab.h" |
#include "symtab.h" |
29 |
|
#include "symlist.h" |
30 |
#include "options.h" |
#include "options.h" |
31 |
#include "gram.h" |
#include "gram.h" |
32 |
#include "complain.h" |
#include "complain.h" |
36 |
#include "muscle_tab.h" |
#include "muscle_tab.h" |
37 |
|
|
38 |
int lineno; |
int lineno; |
39 |
static symbol_list *grammar = NULL; |
static symbol_list_t *grammar = NULL; |
40 |
static int start_flag = 0; |
static int start_flag = 0; |
41 |
|
|
42 |
/* Nonzero if %union has been seen. */ |
/* Nonzero if %union has been seen. */ |
43 |
int typed = 0; |
int typed = 0; |
|
|
|
|
symbol_list * |
|
|
symbol_list_new (symbol_t *sym, location_t location) |
|
|
{ |
|
|
symbol_list *res = XMALLOC (symbol_list, 1); |
|
|
res->next = NULL; |
|
|
res->sym = sym; |
|
|
res->location = location; |
|
|
res->action = NULL; |
|
|
res->ruleprec = NULL; |
|
|
return res; |
|
|
} |
|
|
|
|
|
symbol_list * |
|
|
symbol_list_prepend (symbol_list *list, symbol_t *symbol, location_t location) |
|
|
{ |
|
|
symbol_list *res = symbol_list_new (symbol, location); |
|
|
res->next = list; |
|
|
return res; |
|
|
} |
|
|
|
|
44 |
|
|
|
/*--------------------------------------------------------------. |
|
|
| Get the data type (alternative in the union) of the value for | |
|
|
| symbol N in rule RULE. | |
|
|
`--------------------------------------------------------------*/ |
|
|
|
|
|
char * |
|
|
get_type_name (int n, symbol_list *rule) |
|
|
{ |
|
|
int i; |
|
|
symbol_list *rp; |
|
|
|
|
|
if (n < 0) |
|
|
{ |
|
|
complain (_("invalid $ value")); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
rp = rule; |
|
|
i = 0; |
|
|
|
|
|
while (i < n) |
|
|
{ |
|
|
rp = rp->next; |
|
|
if (rp == NULL || rp->sym == NULL) |
|
|
{ |
|
|
complain (_("invalid $ value")); |
|
|
return NULL; |
|
|
} |
|
|
++i; |
|
|
} |
|
|
|
|
|
return rp->sym->type_name; |
|
|
} |
|
|
|
|
|
|
|
45 |
/*-----------------------. |
/*-----------------------. |
46 |
| Set the start symbol. | |
| Set the start symbol. | |
47 |
`-----------------------*/ |
`-----------------------*/ |
128 |
} |
} |
129 |
|
|
130 |
/*-------------------------------------------------------------------. |
/*-------------------------------------------------------------------. |
131 |
| Parse the input grammar into a one symbol_list structure. Each | |
| Parse the input grammar into a one symbol_list_t structure. Each | |
132 |
| rule is represented by a sequence of symbols: the left hand side | |
| rule is represented by a sequence of symbols: the left hand side | |
133 |
| followed by the contents of the right hand side, followed by a | |
| followed by the contents of the right hand side, followed by a | |
134 |
| null pointer instead of a symbol to terminate the rule. The next | |
| null pointer instead of a symbol to terminate the rule. The next | |
146 |
`-------------------------------------------------------------------*/ |
`-------------------------------------------------------------------*/ |
147 |
|
|
148 |
/* The (currently) last symbol of GRAMMAR. */ |
/* The (currently) last symbol of GRAMMAR. */ |
149 |
symbol_list *grammar_end = NULL; |
symbol_list_t *grammar_end = NULL; |
150 |
|
|
151 |
/* Append S to the GRAMMAR. */ |
/* Append S to the GRAMMAR. */ |
152 |
void |
void |
153 |
grammar_symbol_append (symbol_t *symbol, location_t location) |
grammar_symbol_append (symbol_t *symbol, location_t location) |
154 |
{ |
{ |
155 |
symbol_list *p = symbol_list_new (symbol, location); |
symbol_list_t *p = symbol_list_new (symbol, location); |
156 |
|
|
157 |
if (grammar_end) |
if (grammar_end) |
158 |
grammar_end->next = p; |
grammar_end->next = p; |
165 |
/* The rule currently being defined, and the previous rule. |
/* The rule currently being defined, and the previous rule. |
166 |
CURRENT_RULE points to the first LHS of the current rule, while |
CURRENT_RULE points to the first LHS of the current rule, while |
167 |
PREVIOUS_RULE_END points to the *end* of the previous rule (NULL). */ |
PREVIOUS_RULE_END points to the *end* of the previous rule (NULL). */ |
168 |
symbol_list *current_rule = NULL; |
symbol_list_t *current_rule = NULL; |
169 |
symbol_list *previous_rule_end = NULL; |
symbol_list_t *previous_rule_end = NULL; |
170 |
|
|
171 |
|
|
172 |
/*----------------------------------------------. |
/*----------------------------------------------. |
268 |
action. Create the MIDRULE. */ |
action. Create the MIDRULE. */ |
269 |
location_t dummy_location = current_rule->action_location; |
location_t dummy_location = current_rule->action_location; |
270 |
symbol_t *dummy = gensym (dummy_location); |
symbol_t *dummy = gensym (dummy_location); |
271 |
symbol_list *midrule = symbol_list_new (dummy, dummy_location); |
symbol_list_t *midrule = symbol_list_new (dummy, dummy_location); |
272 |
|
|
273 |
/* Make a new rule, whose body is empty, before the current one, so |
/* Make a new rule, whose body is empty, before the current one, so |
274 |
that the action just read can belong to it. */ |
that the action just read can belong to it. */ |
342 |
{ |
{ |
343 |
unsigned int itemno; |
unsigned int itemno; |
344 |
int ruleno; |
int ruleno; |
345 |
symbol_list *p; |
symbol_list_t *p; |
346 |
|
|
347 |
ritem = XCALLOC (item_number_t, nritems); |
ritem = XCALLOC (item_number_t, nritems); |
348 |
rules = XCALLOC (rule_t, nrules) - 1; |
rules = XCALLOC (rule_t, nrules) - 1; |
463 |
|
|
464 |
axiom: %start EOF. */ |
axiom: %start EOF. */ |
465 |
{ |
{ |
466 |
symbol_list *p = symbol_list_new (axiom, empty_location); |
symbol_list_t *p = symbol_list_new (axiom, empty_location); |
467 |
p->location = grammar->location; |
p->location = grammar->location; |
468 |
p->next = symbol_list_new (startsymbol, empty_location); |
p->next = symbol_list_new (startsymbol, empty_location); |
469 |
p->next->next = symbol_list_new (eoftoken, empty_location); |
p->next->next = symbol_list_new (eoftoken, empty_location); |
489 |
/* Convert the grammar into the format described in gram.h. */ |
/* Convert the grammar into the format described in gram.h. */ |
490 |
packgram (); |
packgram (); |
491 |
|
|
492 |
/* The grammar as a symbol_list is no longer needed. */ |
/* The grammar as a symbol_list_t is no longer needed. */ |
493 |
LIST_FREE (symbol_list, grammar); |
LIST_FREE (symbol_list_t, grammar); |
494 |
} |
} |