1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Grammar implementation */
27 #include "pgenheaders.h"
45 fatal("no mem for new grammar");
49 g
->g_ll
.ll_nlabels
= 0;
50 g
->g_ll
.ll_label
= NULL
;
62 RESIZE(g
->g_dfa
, dfa
, g
->g_ndfas
+ 1);
64 fatal("no mem to resize dfa in adddfa");
65 d
= &g
->g_dfa
[g
->g_ndfas
++];
72 return d
; /* Only use while fresh! */
81 RESIZE(d
->d_state
, state
, d
->d_nstates
+ 1);
82 if (d
->d_state
== NULL
)
83 fatal("no mem to resize state in addstate");
84 s
= &d
->d_state
[d
->d_nstates
++];
87 return s
- d
->d_state
;
91 addarc(d
, from
, to
, lbl
)
98 assert(0 <= from
&& from
< d
->d_nstates
);
99 assert(0 <= to
&& to
< d
->d_nstates
);
101 s
= &d
->d_state
[from
];
102 RESIZE(s
->s_arc
, arc
, s
->s_narcs
+ 1);
103 if (s
->s_arc
== NULL
)
104 fatal("no mem to resize arc list in addarc");
105 a
= &s
->s_arc
[s
->s_narcs
++];
111 addlabel(ll
, type
, str
)
119 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
120 if (ll
->ll_label
[i
].lb_type
== type
&&
121 strcmp(ll
->ll_label
[i
].lb_str
, str
) == 0)
124 RESIZE(ll
->ll_label
, label
, ll
->ll_nlabels
+ 1);
125 if (ll
->ll_label
== NULL
)
126 fatal("no mem to resize labellist in addlabel");
127 lb
= &ll
->ll_label
[ll
->ll_nlabels
++];
129 lb
->lb_str
= str
; /* XXX strdup(str) ??? */
130 return lb
- ll
->ll_label
;
133 /* Same, but rather dies than adds */
136 findlabel(ll
, type
, str
)
144 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
145 if (ll
->ll_label
[i
].lb_type
== type
/*&&
146 strcmp(ll->ll_label[i].lb_str, str) == 0*/)
149 fprintf(stderr
, "Label %d/'%s' not found\n", type
, str
);
154 static void translabel
PROTO((grammar
*, label
*));
162 printf("Translating labels ...\n");
163 /* Don't translate EMPTY */
164 for (i
= EMPTY
+1; i
< g
->g_ll
.ll_nlabels
; i
++)
165 translabel(g
, &g
->g_ll
.ll_label
[i
]);
176 printf("Translating label %s ...\n", labelrepr(lb
));
178 if (lb
->lb_type
== NAME
) {
179 for (i
= 0; i
< g
->g_ndfas
; i
++) {
180 if (strcmp(lb
->lb_str
, g
->g_dfa
[i
].d_name
) == 0) {
182 printf("Label %s is non-terminal %d.\n",
185 lb
->lb_type
= g
->g_dfa
[i
].d_type
;
190 for (i
= 0; i
< (int)N_TOKENS
; i
++) {
191 if (strcmp(lb
->lb_str
, tok_name
[i
]) == 0) {
193 printf("Label %s is terminal %d.\n",
200 printf("Can't translate NAME label '%s'\n", lb
->lb_str
);
204 if (lb
->lb_type
== STRING
) {
205 if (isalpha(lb
->lb_str
[1])) {
208 printf("Label %s is a keyword\n", lb
->lb_str
);
211 p
= strchr(lb
->lb_str
, '\'');
215 else if (lb
->lb_str
[2] == lb
->lb_str
[0]) {
216 int type
= (int) tok_1char(lb
->lb_str
[1]);
222 printf("Unknown OP label %s\n",
225 else if (lb
->lb_str
[2] && lb
->lb_str
[3] == lb
->lb_str
[0]) {
226 int type
= (int) tok_2char(lb
->lb_str
[1],
233 printf("Unknown OP label %s\n",
237 printf("Can't translate STRING label %s\n",
241 printf("Can't translate label '%s'\n", labelrepr(lb
));