1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
#include <pgf/data.h>
#include <stdlib.h>
typedef struct {
GuMapItor fn;
GuOut* out;
} PgfPrintFn;
void
pgf_print_flag(GuMapItor* fn, const void* key, void* value,
GuExn* err)
{
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId flag = (PgfCId) key;
PgfLiteral lit = *((PgfLiteral *) value);
GuOut *out = clo->out;
gu_puts(" flag ", out, err);
pgf_print_cid(flag, out, err);
gu_puts(" = ", out, err);
pgf_print_literal(lit, out, err);
gu_puts(";\n", out, err);
}
void
pgf_print_cat(GuMapItor* fn, const void* key, void* value,
GuExn* err)
{
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId name = (PgfCId) key;
PgfAbsCat *cat = *((PgfAbsCat **) value);
GuOut *out = clo->out;
gu_puts(" cat ", out, err);
pgf_print_cid(name, out, err);
PgfPrintContext* ctxt = NULL;
size_t n_hypos = gu_seq_length(cat->context);
for (size_t i = 0; i < n_hypos; i++) {
PgfHypo* hypo = gu_seq_index(cat->context, PgfHypo, i);
gu_putc(' ', out, err);
ctxt = pgf_print_hypo(hypo, ctxt, 4, out, err);
}
while (ctxt != NULL) {
PgfPrintContext* next = ctxt->next;
free(ctxt);
ctxt = next;
}
gu_printf(out, err, " ; -- %f\n", cat->prob);
}
void
pgf_print_absfun(GuMapItor* fn, const void* key, void* value,
GuExn* err)
{
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId name = (PgfCId) key;
PgfAbsFun *fun = *((PgfAbsFun **) value);
GuOut *out = clo->out;
gu_puts((fun->defns == NULL) ? " data " : " fun ", out, err);
pgf_print_cid(name, out, err);
gu_puts(" : ", out, err);
pgf_print_type(fun->type, NULL, 0, out, err);
gu_printf(out, err, " ; -- %f\n", fun->ep.prob);
}
static void
pgf_print_abstract(PgfAbstr* abstr, GuOut* out, GuExn* err)
{
gu_puts("abstract ", out, err);
pgf_print_cid(abstr->name, out, err);
gu_puts(" {\n", out, err);
PgfPrintFn clo1 = { { pgf_print_flag }, out };
gu_map_iter(abstr->aflags, &clo1.fn, err);
PgfPrintFn clo2 = { { pgf_print_cat }, out };
gu_map_iter(abstr->cats, &clo2.fn, err);
PgfPrintFn clo3 = { { pgf_print_absfun }, out };
gu_map_iter(abstr->funs, &clo3.fn, err);
gu_puts("}\n", out, err);
}
static void
pgf_print_productions(GuMapItor* fn, const void* key, void* value,
GuExn* err)
{
PgfPrintFn* clo = (PgfPrintFn*) fn;
int fid = *((int *) key);
PgfCCat* ccat = *((PgfCCat**) value);
GuOut *out = clo->out;
if (ccat->prods != NULL) {
size_t n_prods = gu_seq_length(ccat->prods);
for (size_t i = 0; i < n_prods; i++) {
PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i);
gu_printf(out,err," C%d -> ",fid);
GuVariantInfo i = gu_variant_open(prod);
switch (i.tag) {
case PGF_PRODUCTION_APPLY: {
PgfProductionApply* papp = i.data;
gu_printf(out,err,"F%d[",papp->fun->funid);
size_t n_args = gu_seq_length(papp->args);
for (size_t j = 0; j < n_args; j++) {
if (j > 0)
gu_putc(',',out,err);
PgfPArg arg = gu_seq_get(papp->args, PgfPArg, j);
if (arg.hypos != NULL) {
size_t n_hypos = gu_seq_length(arg.hypos);
for (size_t k = 0; k < n_hypos; k++) {
if (k > 0)
gu_putc(' ',out,err);
PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k);
gu_printf(out,err,"C%d",hypo->fid);
}
}
gu_printf(out,err,"C%d",arg.ccat->fid);
}
gu_printf(out,err,"]\n");
break;
}
case PGF_PRODUCTION_COERCE: {
PgfProductionCoerce* pcoerce = i.data;
gu_printf(out,err,"_[C%d]\n",pcoerce->coerce->fid);
break;
}
default:
gu_impossible();
}
}
}
}
static void
pgf_print_lindefs(GuMapItor* fn, const void* key, void* value,
GuExn* err)
{
PgfPrintFn* clo = (PgfPrintFn*) fn;
int fid = *((int *) key);
PgfCCat* ccat = *((PgfCCat**) value);
GuOut *out = clo->out;
if (ccat->lindefs != NULL) {
gu_printf(out,err," C%d -> ",fid);
size_t n_lindefs = gu_seq_length(ccat->lindefs);
for (size_t i = 0; i < n_lindefs; i++) {
if (i > 0) gu_putc(' ', out, err);
PgfCncFun* fun = gu_seq_get(ccat->lindefs, PgfCncFun*, i);
gu_printf(out,err,"F%d",fun->funid);
}
gu_putc('\n', out,err);
}
}
static void
pgf_print_linrefs(GuMapItor* fn, const void* key, void* value,
GuExn* err)
{
PgfPrintFn* clo = (PgfPrintFn*) fn;
int fid = *((int *) key);
PgfCCat* ccat = *((PgfCCat**) value);
GuOut *out = clo->out;
if (ccat->linrefs != NULL) {
gu_puts(" ",out,err);
size_t n_linrefs = gu_seq_length(ccat->linrefs);
for (size_t i = 0; i < n_linrefs; i++) {
if (i > 0) gu_putc(' ', out, err);
PgfCncFun* fun = gu_seq_get(ccat->linrefs, PgfCncFun*, i);
gu_printf(out,err,"F%d",fun->funid);
}
gu_printf(out,err," -> C%d\n",fid);
}
}
static void
pgf_print_cncfun(PgfCncFun *cncfun, PgfSequences* sequences,
GuOut *out, GuExn *err)
{
gu_printf(out,err," F%d := (", cncfun->funid);
for (size_t i = 0; i < cncfun->n_lins; i++) {
if (i > 0) gu_putc(',', out, err);
PgfSequence* seq = cncfun->lins[i];
gu_printf(out,err,"S%d", (seq - ((PgfSequence*) gu_seq_data(sequences))));
}
gu_puts(")", out, err);
if (cncfun->absfun != NULL) {
gu_puts(" [", out, err);
pgf_print_cid(cncfun->absfun->name, out, err);
gu_puts("]", out, err);
}
gu_puts("\n", out, err);
}
static void
pgf_print_token(PgfToken tok, GuOut *out, GuExn *err)
{
gu_putc('"', out, err);
gu_string_write(tok, out, err);
gu_putc('"', out, err);
}
static void
pgf_print_symbols(PgfSymbols* syms, GuOut *out, GuExn *err);
void
pgf_print_symbol(PgfSymbol sym, GuOut *out, GuExn *err)
{
switch (gu_variant_tag(sym)) {
case PGF_SYMBOL_CAT: {
PgfSymbolCat* scat = gu_variant_data(sym);
gu_printf(out, err, "<%d,%d>", scat->d, scat->r);
break;
}
case PGF_SYMBOL_KS: {
PgfSymbolKS* sks = gu_variant_data(sym);
pgf_print_token(sks->token, out, err);
break;
}
case PGF_SYMBOL_KP: {
PgfSymbolKP* skp = gu_variant_data(sym);
gu_puts("pre {", out, err);
pgf_print_symbols(skp->default_form, out, err);
for (size_t i = 0; i < skp->n_forms; i++) {
gu_puts("; ", out, err);
pgf_print_symbols(skp->forms[i].form, out, err);
gu_puts(" / ", out, err);
size_t n_prefixes = gu_seq_length(skp->forms[i].prefixes);
for (size_t j = 0; j < n_prefixes; j++) {
if (j > 0) gu_putc(' ', out, err);
GuString prefix = gu_seq_get(skp->forms[i].prefixes, GuString, j);
gu_putc('"', out, err);
gu_string_write(prefix, out, err);
gu_putc('"', out, err);
}
}
gu_puts("}", out, err);
break;
}
case PGF_SYMBOL_LIT: {
PgfSymbolLit *slit = gu_variant_data(sym);
gu_printf(out, err, "{%d,%d}", slit->d, slit->r);
break;
}
case PGF_SYMBOL_VAR: {
PgfSymbolVar *svar = gu_variant_data(sym);
gu_printf(out, err, "<%d,$%d>", svar->d, svar->r);
break;
}
case PGF_SYMBOL_NE: {
gu_puts("nonExist", out, err);
break;
}
case PGF_SYMBOL_BIND: {
gu_puts("BIND", out, err);
break;
}
case PGF_SYMBOL_SOFT_BIND: {
gu_puts("SOFT_BIND", out, err);
break;
}
default:
gu_impossible();
}
}
static void
pgf_print_symbols(PgfSymbols* syms, GuOut *out, GuExn *err)
{
int n_syms = gu_seq_length(syms);
for (int i = 0; i < n_syms; i++) {
if (i > 0) gu_putc(' ', out, err);
PgfSymbol sym = gu_seq_get(syms, PgfSymbol, i);
pgf_print_symbol(sym, out, err);
}
}
static void
pgf_print_cnccat(GuMapItor* fn, const void* key, void* value,
GuExn* err)
{
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId name = (PgfCId) key;
PgfCncCat* cnccat = *((PgfCncCat**) value);
GuOut *out = clo->out;
gu_puts(" ", out, err);
pgf_print_cid(name, out, err);
gu_puts(" :=\n", out, err);
PgfCCat *start = gu_seq_get(cnccat->cats, PgfCCat*, 0);
PgfCCat *end = gu_seq_get(cnccat->cats, PgfCCat*, gu_seq_length(cnccat->cats)-1);
gu_printf(out, err, " range [C%d..C%d]\n", start->fid, end->fid);
gu_puts(" labels [", out, err);
for (size_t i = 0; i < cnccat->n_lins; i++) {
if (i > 0) {
gu_puts("\n ", out, err);
}
gu_string_write(cnccat->labels[i], out, err);
}
gu_puts("]\n", out, err);
}
static void
pgf_print_concrete(PgfCId cncname, PgfConcr* concr,
GuOut* out, GuExn* err)
{
gu_puts("concrete ", out, err);
pgf_print_cid(cncname, out, err);
gu_puts(" {\n", out, err);
PgfPrintFn clo1 = { { pgf_print_flag }, out };
gu_map_iter(concr->cflags, &clo1.fn, err);
gu_puts(" productions\n", out, err);
PgfPrintFn clo2 = { { pgf_print_productions }, out };
gu_map_iter(concr->ccats, &clo2.fn, err);
gu_puts(" lindefs\n", out, err);
PgfPrintFn clo3 = { { pgf_print_lindefs }, out };
gu_map_iter(concr->ccats, &clo3.fn, err);
gu_puts(" linrefs\n", out, err);
PgfPrintFn clo4 = { { pgf_print_linrefs }, out };
gu_map_iter(concr->ccats, &clo4.fn, err);
gu_puts(" lin\n", out, err);
size_t n_funs = gu_seq_length(concr->cncfuns);
for (size_t i = 0; i < n_funs; i++) {
PgfCncFun* cncfun = gu_seq_get(concr->cncfuns, PgfCncFun*, i);
pgf_print_cncfun(cncfun, concr->sequences, out, err);
}
gu_puts(" sequences\n", out, err);
size_t n_seqs = gu_seq_length(concr->sequences);
for (size_t i = 0; i < n_seqs; i++) {
gu_printf(out,err," S%d := ", i);
PgfSymbols* syms = gu_seq_index(concr->sequences, PgfSequence, i)->syms;
pgf_print_symbols(syms, out, err);
gu_putc('\n', out, err);
}
gu_puts(" categories\n", out, err);
PgfPrintFn clo5 = { { pgf_print_cnccat }, out };
gu_map_iter(concr->cnccats, &clo5.fn, err);
gu_puts("}\n", out, err);
}
static void
pgf_print_concr_cb(GuMapItor* fn, const void* key, void* value,
GuExn* err)
{
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId cncname = (PgfCId) key;
PgfConcr *concr = *((PgfConcr **) value);
pgf_print_concrete(cncname, concr, clo->out, err);
}
void
pgf_print(PgfPGF* pgf, GuOut* out, GuExn* err)
{
pgf_print_abstract(&pgf->abstract, out, err);
PgfPrintFn clo = { { pgf_print_concr_cb }, out };
gu_map_iter(pgf->concretes, &clo.fn, err);
}
|