]> git.saurik.com Git - bison.git/blame - src/derives.c
* src/LR0.c, src/conflicts.c, src/lalr.c, src/lalr.h, src/output.c
[bison.git] / src / derives.c
CommitLineData
122c0aac 1/* Match rules with nonterminals for bison,
2c5f66ed 2 Copyright 1984, 1989, 2000, 2001 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"
122c0aac 25#include "types.h"
2c5f66ed 26#include "reader.h"
122c0aac 27#include "gram.h"
cc84fd5d 28#include "derives.h"
4a120d45 29
80a69750 30short **derives = NULL;
cc84fd5d
AD
31
32static void
33print_derives (void)
34{
d7913476 35 int i;
cc84fd5d 36
c87d4863 37 fputs ("DERIVES\n", stderr);
cc84fd5d
AD
38
39 for (i = ntokens; i < nsyms; i++)
40 {
720e5c1b 41 short *sp;
ad949da9 42 fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
cc84fd5d 43 for (sp = derives[i]; *sp > 0; sp++)
720e5c1b
AD
44 {
45 short *rhsp;
46 fprintf (stderr, "\t\t%d:", *sp);
1a2b5d37 47 for (rhsp = &ritem[rules[*sp].rhs]; *rhsp >= 0; ++rhsp)
ad949da9 48 fprintf (stderr, " %s", symbols[*rhsp]->tag);
3d4daee3 49 fprintf (stderr, " (rule %d)\n", -*rhsp - 1);
720e5c1b 50 }
cc84fd5d
AD
51 }
52
c87d4863 53 fputs ("\n\n", stderr);
cc84fd5d
AD
54}
55
122c0aac 56
122c0aac 57void
d2729d44 58set_derives (void)
122c0aac 59{
d7913476 60 int i;
d7913476
AD
61 shorts *p;
62 short *q;
63 shorts **dset;
64 shorts *delts;
122c0aac 65
d7913476
AD
66 dset = XCALLOC (shorts *, nvars) - ntokens;
67 delts = XCALLOC (shorts, nrules + 1);
122c0aac
RS
68
69 p = delts;
70 for (i = nrules; i > 0; i--)
1a2b5d37 71 if (rules[i].useful)
68f1e3ed 72 {
1a2b5d37 73 int lhs = rules[i].lhs;
68f1e3ed
AD
74 p->next = dset[lhs];
75 p->value = i;
76 dset[lhs] = p;
77 p++;
78 }
122c0aac 79
d7913476
AD
80 derives = XCALLOC (short *, nvars) - ntokens;
81 q = XCALLOC (short, nvars + nrules);
122c0aac
RS
82
83 for (i = ntokens; i < nsyms; i++)
84 {
85 derives[i] = q;
86 p = dset[i];
87 while (p)
88 {
89 *q++ = p->value;
90 p = p->next;
91 }
92 *q++ = -1;
93 }
94
9bfe901c
AD
95 if (trace_flag)
96 print_derives ();
122c0aac 97
d7913476
AD
98 XFREE (dset + ntokens);
99 XFREE (delts);
122c0aac
RS
100}
101
102void
d2729d44 103free_derives (void)
122c0aac 104{
d7913476
AD
105 XFREE (derives[ntokens]);
106 XFREE (derives + ntokens);
122c0aac 107}