]> git.saurik.com Git - bison.git/blob - src/derives.c
d3443602c4107829920f6439f164b7442be736bf
[bison.git] / src / derives.c
1 /* Match rules with nonterminals for bison,
2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21
22 #include "system.h"
23 #include "getargs.h"
24 #include "symtab.h"
25 #include "types.h"
26 #include "reader.h"
27 #include "gram.h"
28 #include "derives.h"
29
30 short **derives = NULL;
31
32 static void
33 print_derives (void)
34 {
35 int i;
36
37 fputs ("DERIVES\n", stderr);
38
39 for (i = ntokens; i < nsyms; i++)
40 {
41 short *sp;
42 fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
43 for (sp = derives[i]; *sp > 0; sp++)
44 {
45 item_number_t *rhsp;
46 fprintf (stderr, "\t\t%d:", *sp);
47 for (rhsp = rules[*sp].rhs; *rhsp >= 0; ++rhsp)
48 fprintf (stderr, " %s", symbols[*rhsp]->tag);
49 fprintf (stderr, " (rule %d)\n", -*rhsp - 1);
50 }
51 }
52
53 fputs ("\n\n", stderr);
54 }
55
56
57 void
58 set_derives (void)
59 {
60 int i;
61 shorts *p;
62 short *q;
63 shorts **dset;
64 shorts *delts;
65
66 dset = XCALLOC (shorts *, nvars) - ntokens;
67 delts = XCALLOC (shorts, nrules + 1);
68
69 p = delts;
70 for (i = nrules; i > 0; i--)
71 {
72 token_number_t lhs = rules[i].lhs->number;
73 p->next = dset[lhs];
74 p->value = i;
75 dset[lhs] = p;
76 p++;
77 }
78
79 derives = XCALLOC (short *, nvars) - ntokens;
80 q = XCALLOC (short, nvars + nrules);
81
82 for (i = ntokens; i < nsyms; i++)
83 {
84 derives[i] = q;
85 p = dset[i];
86 while (p)
87 {
88 *q++ = p->value;
89 p = p->next;
90 }
91 *q++ = -1;
92 }
93
94 if (trace_flag)
95 print_derives ();
96
97 XFREE (dset + ntokens);
98 XFREE (delts);
99 }
100
101 void
102 free_derives (void)
103 {
104 XFREE (derives[ntokens]);
105 XFREE (derives + ntokens);
106 }