1 /* Match rules with nonterminals for bison,
3 Copyright (C) 1984, 1989, 2000-2003, 2005, 2009-2015, 2018-2022 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
31 /* Linked list of rule numbers. */
32 typedef struct rule_list
34 struct rule_list
*next
;
43 fputs ("DERIVES\n", stderr
);
45 for (symbol_number i
= ntokens
; i
< nsyms
; ++i
)
47 fprintf (stderr
, " %s derives\n", symbols
[i
]->tag
);
48 for (rule
**rp
= derives
[i
- ntokens
]; *rp
; ++rp
)
50 fprintf (stderr
, " %3d ", (*rp
)->code
);
51 rule_rhs_print (*rp
, stderr
);
52 fprintf (stderr
, "\n");
56 fputs ("\n\n", stderr
);
61 derives_compute (void)
63 /* DSET[NTERM - NTOKENS] -- A linked list of the numbers of the rules
64 whose LHS is NTERM. */
65 rule_list
**dset
= xcalloc (nnterms
, sizeof *dset
);
67 /* DELTS[RULE] -- There are NRULES rule number to attach to nterms.
68 Instead of performing NRULES allocations for each, have an array
69 indexed by rule numbers. */
70 rule_list
*delts
= xnmalloc (nrules
, sizeof *delts
);
72 for (rule_number r
= nrules
- 1; r
>= 0; --r
)
74 symbol_number lhs
= rules
[r
].lhs
->number
;
75 rule_list
*p
= &delts
[r
];
76 /* A new LHS is found. */
77 p
->next
= dset
[lhs
- ntokens
];
79 dset
[lhs
- ntokens
] = p
;
82 /* DSET contains what we need under the form of a linked list. Make
85 derives
= xnmalloc (nnterms
, sizeof *derives
);
86 /* Q is the storage for DERIVES[...] (DERIVES[0] = q). */
87 rule
**q
= xnmalloc (nnterms
+ nrules
, sizeof *q
);
89 for (symbol_number i
= ntokens
; i
< nsyms
; ++i
)
91 rule_list
*p
= dset
[i
- ntokens
];
92 derives
[i
- ntokens
] = q
;
101 if (trace_flag
& trace_sets
)