1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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
;
63 RESIZE(g
->g_dfa
, dfa
, g
->g_ndfas
+ 1);
65 fatal("no mem to resize dfa in adddfa");
66 d
= &g
->g_dfa
[g
->g_ndfas
++];
73 return d
; /* Only use while fresh! */
82 RESIZE(d
->d_state
, state
, d
->d_nstates
+ 1);
83 if (d
->d_state
== NULL
)
84 fatal("no mem to resize state in addstate");
85 s
= &d
->d_state
[d
->d_nstates
++];
92 return s
- d
->d_state
;
96 addarc(d
, from
, to
, lbl
)
103 assert(0 <= from
&& from
< d
->d_nstates
);
104 assert(0 <= to
&& to
< d
->d_nstates
);
106 s
= &d
->d_state
[from
];
107 RESIZE(s
->s_arc
, arc
, s
->s_narcs
+ 1);
108 if (s
->s_arc
== NULL
)
109 fatal("no mem to resize arc list in addarc");
110 a
= &s
->s_arc
[s
->s_narcs
++];
116 addlabel(ll
, type
, str
)
124 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
125 if (ll
->ll_label
[i
].lb_type
== type
&&
126 strcmp(ll
->ll_label
[i
].lb_str
, str
) == 0)
129 RESIZE(ll
->ll_label
, label
, ll
->ll_nlabels
+ 1);
130 if (ll
->ll_label
== NULL
)
131 fatal("no mem to resize labellist in addlabel");
132 lb
= &ll
->ll_label
[ll
->ll_nlabels
++];
134 lb
->lb_str
= str
; /* XXX strdup(str) ??? */
135 return lb
- ll
->ll_label
;
138 /* Same, but rather dies than adds */
141 findlabel(ll
, type
, str
)
148 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
149 if (ll
->ll_label
[i
].lb_type
== type
/*&&
150 strcmp(ll->ll_label[i].lb_str, str) == 0*/)
153 fprintf(stderr
, "Label %d/'%s' not found\n", type
, str
);
154 fatal("grammar.c:findlabel()");
159 static void translabel
PROTO((grammar
*, label
*));
168 printf("Translating labels ...\n");
170 /* Don't translate EMPTY */
171 for (i
= EMPTY
+1; i
< g
->g_ll
.ll_nlabels
; i
++)
172 translabel(g
, &g
->g_ll
.ll_label
[i
]);
183 printf("Translating label %s ...\n", labelrepr(lb
));
185 if (lb
->lb_type
== NAME
) {
186 for (i
= 0; i
< g
->g_ndfas
; i
++) {
187 if (strcmp(lb
->lb_str
, g
->g_dfa
[i
].d_name
) == 0) {
189 printf("Label %s is non-terminal %d.\n",
192 lb
->lb_type
= g
->g_dfa
[i
].d_type
;
197 for (i
= 0; i
< (int)N_TOKENS
; i
++) {
198 if (strcmp(lb
->lb_str
, tok_name
[i
]) == 0) {
200 printf("Label %s is terminal %d.\n",
207 printf("Can't translate NAME label '%s'\n", lb
->lb_str
);
211 if (lb
->lb_type
== STRING
) {
212 if (isalpha(lb
->lb_str
[1])) {
215 printf("Label %s is a keyword\n", lb
->lb_str
);
218 p
= strchr(lb
->lb_str
, '\'');
222 else if (lb
->lb_str
[2] == lb
->lb_str
[0]) {
223 int type
= (int) tok_1char(lb
->lb_str
[1]);
229 printf("Unknown OP label %s\n",
232 else if (lb
->lb_str
[2] && lb
->lb_str
[3] == lb
->lb_str
[0]) {
233 int type
= (int) tok_2char(lb
->lb_str
[1],
240 printf("Unknown OP label %s\n",
244 printf("Can't translate STRING label %s\n",
248 printf("Can't translate label '%s'\n", labelrepr(lb
));