]> git.saurik.com Git - bison.git/blame_incremental - src/derives.c
#
[bison.git] / src / derives.c
... / ...
CommitLineData
1/* Match rules with nonterminals for bison,
2 Copyright (C) 1984, 1989 Free Software Foundation, Inc.
3
4This file is part of Bison, the GNU Compiler Compiler.
5
6Bison is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11Bison is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Bison; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21/* set_derives finds, for each variable (nonterminal), which rules can derive it.
22 It sets up the value of derives so that
23 derives[i - ntokens] points to a vector of rule numbers,
24 terminated with -1. */
25
26#include <stdio.h>
27#include "system.h"
28#include "alloc.h"
29#include "types.h"
30#include "gram.h"
31
32
33short **derives;
34
35void
36set_derives()
37{
38 register int i;
39 register int lhs;
40 register shorts *p;
41 register short *q;
42 register shorts **dset;
43 register shorts *delts;
44
45 dset = NEW2(nvars, shorts *) - ntokens;
46 delts = NEW2(nrules + 1, shorts);
47
48 p = delts;
49 for (i = nrules; i > 0; i--)
50 {
51 lhs = rlhs[i];
52 if (lhs >= 0)
53 {
54 p->next = dset[lhs];
55 p->value = i;
56 dset[lhs] = p;
57 p++;
58 }
59 }
60
61 derives = NEW2(nvars, short *) - ntokens;
62 q = NEW2(nvars + nrules, short);
63
64 for (i = ntokens; i < nsyms; i++)
65 {
66 derives[i] = q;
67 p = dset[i];
68 while (p)
69 {
70 *q++ = p->value;
71 p = p->next;
72 }
73 *q++ = -1;
74 }
75
76#ifdef DEBUG
77 print_derives();
78#endif
79
80 FREE(dset + ntokens);
81 FREE(delts);
82}
83
84void
85free_derives()
86{
87 FREE(derives[ntokens]);
88 FREE(derives + ntokens);
89}
90
91
92
93#ifdef DEBUG
94
95print_derives()
96{
97 register int i;
98 register short *sp;
99
100 extern char **tags;
101
102 printf(_("\n\n\nDERIVES\n\n"));
103
104 for (i = ntokens; i < nsyms; i++)
105 {
106 printf(_("%s derives"), tags[i]);
107 for (sp = derives[i]; *sp > 0; sp++)
108 {
109 printf(" %d", *sp);
110 }
111 putchar('\n');
112 }
113
114 putchar('\n');
115}
116
117#endif
118