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
|
#ifndef PGF_DATA_H_
#define PGF_DATA_H_
#include <gu/variant.h>
#include <gu/map.h>
#include <gu/string.h>
#include <gu/seq.h>
#include <pgf/pgf.h>
typedef struct PgfCCat PgfCCat;
typedef GuSeq PgfCCats;
#define PgfCIdMap GuStringMap
typedef struct {
PgfCId name;
PgfLiteral value;
} PgfFlag;
typedef GuSeq PgfFlags;
extern GuOrder pgf_flag_order[1];
// PgfPatt
typedef GuVariant PgfPatt;
typedef enum {
PGF_PATT_APP,
PGF_PATT_VAR,
PGF_PATT_AS,
PGF_PATT_WILD,
PGF_PATT_LIT,
PGF_PATT_IMPL_ARG,
PGF_PATT_TILDE,
PGF_PATT_NUM_TAGS
} PgfPattTag;
typedef struct {
PgfCId ctor;
size_t n_args;
PgfPatt args[];
} PgfPattApp;
typedef struct {
PgfLiteral lit;
} PgfPattLit;
typedef struct {
PgfCId var;
} PgfPattVar;
typedef struct {
PgfCId var;
PgfPatt patt;
} PgfPattAs;
typedef void PgfPattWild;
typedef struct {
PgfPatt patt;
} PgfPattImplArg;
typedef struct {
PgfExpr expr;
} PgfPattTilde;
typedef struct {
PgfExpr body;
size_t n_patts;
PgfPatt patts[];
} PgfEquation;
typedef GuSeq PgfEquations;
typedef void *PgfFunction;
typedef struct {
PgfCId name;
PgfType* type;
int arity;
PgfEquations* defns; // maybe null
PgfExprProb ep;
void* predicate;
struct {
PgfFunction code;
union {
size_t caf_offset;
PgfFunction* con;
};
} closure;
} PgfAbsFun;
typedef GuSeq PgfAbsFuns;
extern GuOrder pgf_absfun_order[1];
typedef GuMap PgfMetaChildMap;
typedef struct {
PgfCId name;
PgfHypos* context;
prob_t prob;
void* predicate;
} PgfAbsCat;
typedef GuSeq PgfAbsCats;
extern GuOrder pgf_abscat_order[1];
typedef struct PgfEvalGates PgfEvalGates;
typedef struct {
PgfCId name;
PgfFlags* aflags;
PgfAbsFuns* funs;
PgfAbsCats* cats;
PgfAbsFun* abs_lin_fun;
PgfEvalGates* eval_gates;
} PgfAbstr;
typedef enum {
PGF_INSTR_CHECK_ARGS = 0,
PGF_INSTR_CASE = 1,
PGF_INSTR_CASE_LIT = 2,
PGF_INSTR_SAVE = 3,
PGF_INSTR_ALLOC = 4,
PGF_INSTR_PUT_CONSTR = 5,
PGF_INSTR_PUT_CLOSURE = 6,
PGF_INSTR_PUT_LIT = 7,
PGF_INSTR_SET = 8,
PGF_INSTR_SET_PAD = 9,
PGF_INSTR_PUSH_FRAME = 10,
PGF_INSTR_PUSH = 11,
PGF_INSTR_TUCK = 12,
PGF_INSTR_EVAL = 13,
PGF_INSTR_DROP = 16,
PGF_INSTR_JUMP = 17,
PGF_INSTR_FAIL = 18,
PGF_INSTR_PUSH_ACCUM = 19,
PGF_INSTR_POP_ACCUM = 20,
PGF_INSTR_ADD = 21,
} PgfInstruction;
typedef GuSeq PgfConcrs;
extern GuOrder pgf_concr_order[1];
struct PgfPGF {
uint16_t major_version;
uint16_t minor_version;
PgfFlags* gflags;
PgfAbstr abstract;
PgfConcrs* concretes;
GuPool* pool; // the pool in which the grammar is allocated
};
typedef struct {
PgfAbsCat *abscat;
PgfCCats* cats;
size_t n_lins;
GuString labels[];
/**< Labels for tuples. All nested tuples, records and tables
* in the GF linearization types are flattened into a single
* tuple in the corresponding PGF concrete category. This
* field holds the labels that indicate which GF field or
* parameter (or their combination) each tuple element
* represents. */
} PgfCncCat;
typedef GuSeq PgfTokens;
bool
pgf_tokens_equal(PgfTokens* t1, PgfTokens* t2);
typedef GuSeq PgfSymbols;
typedef struct {
PgfSymbols* form;
/**< The form of this variant as a list of tokens. */
GuStrings* prefixes;
/**< The prefixes of the following symbol that trigger this
* form. */
} PgfAlternative;
typedef struct PgfItemConts PgfItemConts;
typedef PgfCIdMap PgfPrintNames;
typedef GuStringMap PgfCncFunOverloadMap;
typedef GuMap PgfCncOverloadMap;
typedef struct PgfItem PgfItem;
typedef GuVariant PgfSymbol;
typedef enum {
PGF_SYMBOL_CAT,
PGF_SYMBOL_LIT,
PGF_SYMBOL_VAR,
PGF_SYMBOL_KS,
PGF_SYMBOL_KP,
PGF_SYMBOL_BIND,
PGF_SYMBOL_SOFT_BIND,
PGF_SYMBOL_NE,
PGF_SYMBOL_SOFT_SPACE,
PGF_SYMBOL_CAPIT,
PGF_SYMBOL_ALL_CAPIT,
} PgfSymbolTag;
typedef struct {
int d;
int r;
} PgfSymbolIdx;
typedef PgfSymbolIdx PgfSymbolCat, PgfSymbolLit, PgfSymbolVar;
typedef struct {
char token[0]; // a flexible array that contains the token
} PgfSymbolKS;
typedef struct PgfSymbolKP
/** A prefix-dependent symbol. The form that this symbol takes
* depends on the form of a prefix of the following symbol. */
{
PgfSymbols* default_form;
/**< Default form that this symbol takes if none of of the
* variant forms is triggered. */
size_t n_forms;
PgfAlternative forms[];
/**< Variant forms whose choise depends on the following
* symbol. */
} PgfSymbolKP;
typedef struct {
} PgfSymbolNE;
typedef struct {
} PgfSymbolBIND;
typedef struct {
} PgfSymbolCAPIT;
typedef GuBuf PgfProductionIdx;
typedef struct {
PgfSymbols* syms; // -> PgfSymbol
PgfProductionIdx* idx;
} PgfSequence;
typedef GuSeq PgfSequences;
typedef struct {
PgfAbsFun* absfun;
PgfExprProb *ep;
int funid;
size_t n_lins;
PgfSequence* lins[];
} PgfCncFun;
typedef GuSeq PgfCncFuns;
struct PgfConcr {
PgfCId name;
PgfAbstr* abstr;
PgfFlags* cflags;
PgfPrintNames* printnames;
GuMap* ccats;
PgfCncFunOverloadMap* fun_indices;
PgfCncOverloadMap* coerce_idx;
PgfCncFuns* cncfuns;
PgfSequences* sequences;
PgfCIdMap* cnccats;
int total_cats;
GuPool* pool; // if the language is loaded separately then this is the pool
GuFinalizer fin; // and this is the finalizer in the pool of the whole grammar
};
// PgfProduction
typedef GuVariant PgfProduction;
typedef enum {
PGF_PRODUCTION_APPLY,
PGF_PRODUCTION_COERCE,
PGF_PRODUCTION_EXTERN
} PgfProductionTag;
typedef struct {
PgfCCat* ccat;
PgfCCats* hypos;
} PgfPArg;
typedef GuSeq PgfPArgs;
typedef struct {
PgfCncFun* fun;
PgfPArgs* args;
} PgfProductionApply;
typedef struct PgfProductionCoerce
/** A coercion. This production is a logical union of the coercions of
* another FId. This allows common subsets of productions to be
* shared. */
{
PgfCCat* coerce;
} PgfProductionCoerce;
typedef struct {
PgfExprProb *ep;
GuSeq* lins;
} PgfProductionExtern;
typedef struct {
PgfExprProb *ep;
PgfPArgs* args;
} PgfProductionMeta;
typedef GuSeq PgfProductionSeq;
typedef struct {
PgfCCat* ccat;
size_t lin_idx;
PgfProductionApply* papp;
} PgfProductionIdxEntry;
struct PgfCCat {
PgfCncCat* cnccat;
PgfCncFuns* lindefs;
PgfCncFuns* linrefs;
size_t n_synprods;
PgfProductionSeq* prods;
float viterbi_prob;
int fid;
PgfItemConts* conts;
struct PgfAnswers* answers;
GuFinalizer fin[0];
};
#endif
|