]>
Commit | Line | Data |
---|---|---|
122c0aac | 1 | /* Match rules with nonterminals for bison, |
9222837b | 2 | Copyright 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
122c0aac | 3 | |
cc84fd5d | 4 | This file is part of Bison, the GNU Compiler Compiler. |
122c0aac | 5 | |
cc84fd5d AD |
6 | Bison is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
122c0aac | 10 | |
cc84fd5d AD |
11 | Bison is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
122c0aac | 15 | |
cc84fd5d AD |
16 | You should have received a copy of the GNU General Public License |
17 | along with Bison; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
122c0aac RS |
20 | |
21 | ||
122c0aac | 22 | #include "system.h" |
9bfe901c | 23 | #include "getargs.h" |
ad949da9 | 24 | #include "symtab.h" |
2c5f66ed | 25 | #include "reader.h" |
122c0aac | 26 | #include "gram.h" |
cc84fd5d | 27 | #include "derives.h" |
4a120d45 | 28 | |
e1a4f3a4 AD |
29 | /* Linked list of rule numbers. */ |
30 | typedef struct rule_list_s | |
31 | { | |
32 | struct rule_list_s *next; | |
33 | rule_number_t value; | |
34 | } rule_list_t; | |
35 | ||
9222837b | 36 | rule_number_t **derives = NULL; |
cc84fd5d AD |
37 | |
38 | static void | |
39 | print_derives (void) | |
40 | { | |
d7913476 | 41 | int i; |
cc84fd5d | 42 | |
c87d4863 | 43 | fputs ("DERIVES\n", stderr); |
cc84fd5d AD |
44 | |
45 | for (i = ntokens; i < nsyms; i++) | |
46 | { | |
9222837b | 47 | rule_number_t *rp; |
ad949da9 | 48 | fprintf (stderr, "\t%s derives\n", symbols[i]->tag); |
4b3d3a8e | 49 | for (rp = derives[i]; *rp >= 0; rp++) |
720e5c1b | 50 | { |
4b3d3a8e | 51 | fprintf (stderr, "\t\t%3d ", *rp); |
e1a4f3a4 | 52 | rule_rhs_print (&rules[*rp], stderr); |
720e5c1b | 53 | } |
cc84fd5d AD |
54 | } |
55 | ||
c87d4863 | 56 | fputs ("\n\n", stderr); |
cc84fd5d AD |
57 | } |
58 | ||
122c0aac | 59 | |
122c0aac | 60 | void |
d2729d44 | 61 | set_derives (void) |
122c0aac | 62 | { |
9222837b | 63 | symbol_number_t i; |
4b3d3a8e | 64 | int r; |
9222837b | 65 | rule_number_t *q; |
122c0aac | 66 | |
e1a4f3a4 AD |
67 | /* DSET[NTERM] -- A linked list of the numbers of the rules whose |
68 | LHS is NTERM. */ | |
69 | rule_list_t **dset = XCALLOC (rule_list_t *, nvars) - ntokens; | |
70 | ||
71 | /* DELTS[RULE] -- There are NRULES rule number to attach to nterms. | |
72 | Instead of performing NRULES allocations for each, have an array | |
73 | indexed by rule numbers. */ | |
4b3d3a8e | 74 | rule_list_t *delts = XCALLOC (rule_list_t, nrules); |
122c0aac | 75 | |
4b3d3a8e | 76 | for (r = nrules - 1; r >= 0; --r) |
bba97eb2 | 77 | { |
9222837b | 78 | symbol_number_t lhs = rules[r].lhs->number; |
e1a4f3a4 AD |
79 | rule_list_t *p = &delts[r]; |
80 | /* A new LHS is found. */ | |
bba97eb2 | 81 | p->next = dset[lhs]; |
9222837b | 82 | p->value = r; |
bba97eb2 | 83 | dset[lhs] = p; |
bba97eb2 | 84 | } |
122c0aac | 85 | |
e1a4f3a4 AD |
86 | /* DSET contains what we need under the form of a linked list. Make |
87 | it a single array. */ | |
88 | ||
9222837b | 89 | derives = XCALLOC (rule_number_t *, nvars) - ntokens; |
e1a4f3a4 | 90 | q = XCALLOC (rule_number_t, nvars + int_of_rule_number (nrules)); |
122c0aac RS |
91 | |
92 | for (i = ntokens; i < nsyms; i++) | |
93 | { | |
e1a4f3a4 | 94 | rule_list_t *p = dset[i]; |
122c0aac | 95 | derives[i] = q; |
122c0aac RS |
96 | while (p) |
97 | { | |
98 | *q++ = p->value; | |
99 | p = p->next; | |
100 | } | |
101 | *q++ = -1; | |
102 | } | |
103 | ||
9bfe901c AD |
104 | if (trace_flag) |
105 | print_derives (); | |
122c0aac | 106 | |
e1a4f3a4 AD |
107 | free (dset + ntokens); |
108 | free (delts); | |
122c0aac RS |
109 | } |
110 | ||
e1a4f3a4 | 111 | |
122c0aac | 112 | void |
d2729d44 | 113 | free_derives (void) |
122c0aac | 114 | { |
d7913476 AD |
115 | XFREE (derives[ntokens]); |
116 | XFREE (derives + ntokens); | |
122c0aac | 117 | } |