forked from GitHub/gf-core
libpgf: now all concrete functions and categories are explicitly linked to their abstract counter parts
This commit is contained in:
@@ -97,7 +97,7 @@ GU_DEFINE_TYPE(
|
|||||||
|
|
||||||
GU_DEFINE_TYPE(
|
GU_DEFINE_TYPE(
|
||||||
PgfCncCat, struct,
|
PgfCncCat, struct,
|
||||||
GU_MEMBER(PgfCncCat, cid, PgfCId),
|
GU_MEMBER(PgfCncCat, abscat, PgfCat),
|
||||||
GU_MEMBER_P(PgfCncCat, cats, PgfCCatIds),
|
GU_MEMBER_P(PgfCncCat, cats, PgfCCatIds),
|
||||||
GU_MEMBER(PgfCncCat, n_lins, size_t),
|
GU_MEMBER(PgfCncCat, n_lins, size_t),
|
||||||
GU_FLEX_MEMBER(PgfCncCat, labels, GuString));
|
GU_FLEX_MEMBER(PgfCncCat, labels, GuString));
|
||||||
@@ -118,7 +118,7 @@ GU_DEFINE_TYPE(PgfSeqId, typedef, gu_type(PgfSequence));
|
|||||||
|
|
||||||
GU_DEFINE_TYPE(
|
GU_DEFINE_TYPE(
|
||||||
PgfCncFun, struct,
|
PgfCncFun, struct,
|
||||||
GU_MEMBER(PgfCncFun, fun, PgfCId),
|
GU_MEMBER(PgfCncFun, name, PgfCId),
|
||||||
GU_MEMBER(PgfCncFun, n_lins, GuLength),
|
GU_MEMBER(PgfCncFun, n_lins, GuLength),
|
||||||
GU_FLEX_MEMBER(PgfCncFun, lins, PgfSeqId));
|
GU_FLEX_MEMBER(PgfCncFun, lins, PgfSeqId));
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ typedef GuSeq PgfCCatSeq;
|
|||||||
extern GU_DECLARE_TYPE(PgfCCatSeq, GuSeq);
|
extern GU_DECLARE_TYPE(PgfCCatSeq, GuSeq);
|
||||||
|
|
||||||
typedef struct PgfAbstr PgfAbstr;
|
typedef struct PgfAbstr PgfAbstr;
|
||||||
|
extern GU_DECLARE_TYPE(PgfAbstr, struct);
|
||||||
|
|
||||||
typedef struct PgfFunDecl PgfFunDecl;
|
typedef struct PgfFunDecl PgfFunDecl;
|
||||||
typedef struct PgfConcr PgfConcr;
|
typedef struct PgfConcr PgfConcr;
|
||||||
|
|
||||||
@@ -89,7 +91,9 @@ typedef struct PgfEquation PgfEquation;
|
|||||||
typedef GuSeq PgfEquations;
|
typedef GuSeq PgfEquations;
|
||||||
typedef PgfEquations PgfEquationsM; // can be null
|
typedef PgfEquations PgfEquationsM; // can be null
|
||||||
extern GU_DECLARE_TYPE(PgfEquationsM, GuSeq);
|
extern GU_DECLARE_TYPE(PgfEquationsM, GuSeq);
|
||||||
|
|
||||||
typedef struct PgfCat PgfCat;
|
typedef struct PgfCat PgfCat;
|
||||||
|
extern GU_DECLARE_TYPE(PgfCat, struct);
|
||||||
|
|
||||||
typedef PgfSequence PgfSeqId; // shared reference
|
typedef PgfSequence PgfSeqId; // shared reference
|
||||||
|
|
||||||
@@ -141,9 +145,9 @@ struct PgfCat {
|
|||||||
|
|
||||||
|
|
||||||
struct PgfCncCat {
|
struct PgfCncCat {
|
||||||
PgfCId cid;
|
PgfCat *abscat;
|
||||||
PgfCCatIds* cats;
|
PgfCCatIds* cats;
|
||||||
|
|
||||||
size_t n_lins;
|
size_t n_lins;
|
||||||
GuString labels[];
|
GuString labels[];
|
||||||
/**< Labels for tuples. All nested tuples, records and tables
|
/**< Labels for tuples. All nested tuples, records and tables
|
||||||
@@ -155,7 +159,8 @@ struct PgfCncCat {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct PgfCncFun {
|
struct PgfCncFun {
|
||||||
PgfCId fun; // XXX: resolve to PgfFunDecl*?
|
PgfCId name;
|
||||||
|
PgfFunDecl *absfun;
|
||||||
int funid;
|
int funid;
|
||||||
GuLength n_lins;
|
GuLength n_lins;
|
||||||
PgfSeqId lins[];
|
PgfSeqId lins[];
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ pgf_lzr_add_infer_entry(
|
|||||||
n_args > 0 ? gu_list_index(arg_cats, 0)->fid : -1,
|
n_args > 0 ? gu_list_index(arg_cats, 0)->fid : -1,
|
||||||
n_args > 1 ? gu_list_index(arg_cats, 1)->fid : -1,
|
n_args > 1 ? gu_list_index(arg_cats, 1)->fid : -1,
|
||||||
n_args > 2 ? gu_list_index(arg_cats, 2)->fid : -1,
|
n_args > 2 ? gu_list_index(arg_cats, 2)->fid : -1,
|
||||||
cat->fid, papply->fun->fun);
|
cat->fid, papply->fun->name);
|
||||||
PgfLinInfers* entries =
|
PgfLinInfers* entries =
|
||||||
gu_map_get(infer_table, &arg_cats, PgfLinInfers*);
|
gu_map_get(infer_table, &arg_cats, PgfLinInfers*);
|
||||||
if (!entries) {
|
if (!entries) {
|
||||||
@@ -161,13 +161,13 @@ pgf_lzr_index(PgfConcr* concr, PgfCCat* ccat, PgfProduction prod,
|
|||||||
case PGF_PRODUCTION_APPLY: {
|
case PGF_PRODUCTION_APPLY: {
|
||||||
PgfProductionApply* papply = data;
|
PgfProductionApply* papply = data;
|
||||||
PgfInferMap* infer =
|
PgfInferMap* infer =
|
||||||
gu_map_get(concr->fun_indices, &papply->fun->fun,
|
gu_map_get(concr->fun_indices, &papply->fun->name,
|
||||||
PgfInferMap*);
|
PgfInferMap*);
|
||||||
gu_debug("index: %s -> %d", papply->fun->fun, ccat->fid);
|
gu_debug("index: %s -> %d", papply->fun->name, ccat->fid);
|
||||||
if (!infer) {
|
if (!infer) {
|
||||||
infer = gu_map_type_new(PgfInferMap, pool);
|
infer = gu_map_type_new(PgfInferMap, pool);
|
||||||
gu_map_put(concr->fun_indices,
|
gu_map_put(concr->fun_indices,
|
||||||
&papply->fun->fun, PgfInferMap*, infer);
|
&papply->fun->name, PgfInferMap*, infer);
|
||||||
}
|
}
|
||||||
pgf_lzr_add_infer_entry(infer, ccat, papply, pool);
|
pgf_lzr_add_infer_entry(infer, ccat, papply, pool);
|
||||||
break;
|
break;
|
||||||
@@ -452,7 +452,7 @@ pgf_lzr_linearize(PgfConcr* concr, PgfCncTree ctree, size_t lin_idx, PgfLinFuncs
|
|||||||
PgfCncTreeApp* fapp = cti.data;
|
PgfCncTreeApp* fapp = cti.data;
|
||||||
PgfCncFun* fun = fapp->fun;
|
PgfCncFun* fun = fapp->fun;
|
||||||
if (fns->expr_apply) {
|
if (fns->expr_apply) {
|
||||||
fns->expr_apply(fnsp, fun->fun, fapp->n_args);
|
fns->expr_apply(fnsp, fun->name, fapp->n_args);
|
||||||
}
|
}
|
||||||
gu_require(lin_idx < fun->n_lins);
|
gu_require(lin_idx < fun->n_lins);
|
||||||
PgfSequence seq = fun->lins[lin_idx];
|
PgfSequence seq = fun->lins[lin_idx];
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ typedef GuList(PgfItemBuf*) PgfItemBufs;
|
|||||||
typedef GuBuf PgfCCatBuf;
|
typedef GuBuf PgfCCatBuf;
|
||||||
|
|
||||||
struct PgfParse {
|
struct PgfParse {
|
||||||
PgfAbstr* abstr;
|
|
||||||
PgfConcr* concr;
|
PgfConcr* concr;
|
||||||
PgfItemBuf* agenda;
|
PgfItemBuf* agenda;
|
||||||
int max_fid;
|
int max_fid;
|
||||||
@@ -39,7 +38,6 @@ struct PgfExprState {
|
|||||||
typedef struct PgfParseResult PgfParseResult;
|
typedef struct PgfParseResult PgfParseResult;
|
||||||
|
|
||||||
struct PgfParseResult {
|
struct PgfParseResult {
|
||||||
PgfAbstr* abstr;
|
|
||||||
PgfConcr* concr;
|
PgfConcr* concr;
|
||||||
GuPool *tmp_pool;
|
GuPool *tmp_pool;
|
||||||
GuBuf *pqueue;
|
GuBuf *pqueue;
|
||||||
@@ -852,11 +850,9 @@ pgf_new_parsing(PgfConcr* concr, PgfLexCallback* callback, int max_fid,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static PgfParse*
|
static PgfParse*
|
||||||
pgf_new_parse(PgfAbstr* abstr, PgfConcr* concr,
|
pgf_new_parse(PgfConcr* concr, int max_fid, GuPool* pool)
|
||||||
int max_fid, GuPool* pool)
|
|
||||||
{
|
{
|
||||||
PgfParse* parse = gu_new(PgfParse, pool);
|
PgfParse* parse = gu_new(PgfParse, pool);
|
||||||
parse->abstr = abstr;
|
|
||||||
parse->concr = concr;
|
parse->concr = concr;
|
||||||
parse->agenda = NULL;
|
parse->agenda = NULL;
|
||||||
parse->max_fid = max_fid;
|
parse->max_fid = max_fid;
|
||||||
@@ -969,7 +965,7 @@ pgf_parse_token(PgfParse* parse, PgfToken tok, bool robust, GuPool* pool)
|
|||||||
|
|
||||||
PgfParse* next_parse = NULL;
|
PgfParse* next_parse = NULL;
|
||||||
if (gu_buf_length(agenda) > 0) {
|
if (gu_buf_length(agenda) > 0) {
|
||||||
next_parse = pgf_new_parse(parse->abstr, parse->concr, parse->max_fid, pool);
|
next_parse = pgf_new_parse(parse->concr, parse->max_fid, pool);
|
||||||
next_parse->agenda = agenda;
|
next_parse->agenda = agenda;
|
||||||
next_parse->max_fid= parsing->max_fid;
|
next_parse->max_fid= parsing->max_fid;
|
||||||
}
|
}
|
||||||
@@ -1009,11 +1005,9 @@ pgf_parse_result_from_ccat(PgfParseResult *result, PgfCCat *ccat,
|
|||||||
case PGF_PRODUCTION_APPLY: {
|
case PGF_PRODUCTION_APPLY: {
|
||||||
PgfProductionApply* papp = pi.data;
|
PgfProductionApply* papp = pi.data;
|
||||||
|
|
||||||
PgfFunDecl *fun_decl =
|
gu_assert(papp->fun->absfun != NULL);
|
||||||
gu_map_get(result->abstr->funs, &papp->fun->fun, PgfFunDecl*);
|
|
||||||
gu_assert(fun_decl != NULL);
|
|
||||||
|
|
||||||
double prob = ps->prob - log(fun_decl->prob);
|
double prob = ps->prob - log(papp->fun->absfun->prob);
|
||||||
|
|
||||||
PgfExprState *state = gu_new(PgfExprState, result->tmp_pool);
|
PgfExprState *state = gu_new(PgfExprState, result->tmp_pool);
|
||||||
state->prev = ps->state;
|
state->prev = ps->state;
|
||||||
@@ -1021,7 +1015,7 @@ pgf_parse_result_from_ccat(PgfParseResult *result, PgfCCat *ccat,
|
|||||||
gu_new_variant(PGF_EXPR_FUN,
|
gu_new_variant(PGF_EXPR_FUN,
|
||||||
PgfExprFun,
|
PgfExprFun,
|
||||||
&state->expr, pool);
|
&state->expr, pool);
|
||||||
expr_fun->fun = papp->fun->fun;
|
expr_fun->fun = papp->fun->name;
|
||||||
state->args = papp->args;
|
state->args = papp->args;
|
||||||
state->arg_idx = 0;
|
state->arg_idx = 0;
|
||||||
|
|
||||||
@@ -1148,7 +1142,6 @@ pgf_parse_result(PgfParse* parse, GuPool* pool)
|
|||||||
GuPool *states_pool = gu_new_pool();
|
GuPool *states_pool = gu_new_pool();
|
||||||
PgfParseResult *result =
|
PgfParseResult *result =
|
||||||
gu_new_i(pool, PgfParseResult,
|
gu_new_i(pool, PgfParseResult,
|
||||||
.abstr = parse->abstr,
|
|
||||||
.concr = parse->concr,
|
.concr = parse->concr,
|
||||||
.tmp_pool = states_pool,
|
.tmp_pool = states_pool,
|
||||||
.pqueue = gu_new_buf(PgfExprPState, states_pool),
|
.pqueue = gu_new_buf(PgfExprPState, states_pool),
|
||||||
@@ -1168,7 +1161,7 @@ pgf_parse_result(PgfParse* parse, GuPool* pool)
|
|||||||
|
|
||||||
// TODO: s/CId/Cat, add the cid to Cat, make Cat the key to CncCat
|
// TODO: s/CId/Cat, add the cid to Cat, make Cat the key to CncCat
|
||||||
PgfParse*
|
PgfParse*
|
||||||
pgf_parser_parse(PgfAbstr* abstr, PgfConcr* concr, PgfCId cat, size_t lin_idx, GuPool* pool)
|
pgf_parser_parse(PgfConcr* concr, PgfCId cat, size_t lin_idx, GuPool* pool)
|
||||||
{
|
{
|
||||||
PgfCncCat* cnccat =
|
PgfCncCat* cnccat =
|
||||||
gu_map_get(concr->cnccats, &cat, PgfCncCat*);
|
gu_map_get(concr->cnccats, &cat, PgfCncCat*);
|
||||||
@@ -1178,7 +1171,7 @@ pgf_parser_parse(PgfAbstr* abstr, PgfConcr* concr, PgfCId cat, size_t lin_idx, G
|
|||||||
}
|
}
|
||||||
gu_assert(lin_idx < cnccat->n_lins);
|
gu_assert(lin_idx < cnccat->n_lins);
|
||||||
|
|
||||||
PgfParse* parse = pgf_new_parse(abstr, concr, concr->max_fid, pool);
|
PgfParse* parse = pgf_new_parse(concr, concr->max_fid, pool);
|
||||||
parse->agenda = gu_new_buf(PgfItem*, pool);
|
parse->agenda = gu_new_buf(PgfItem*, pool);
|
||||||
|
|
||||||
PgfItemBuf* conts = gu_new_buf(PgfItem*, pool);
|
PgfItemBuf* conts = gu_new_buf(PgfItem*, pool);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ typedef struct PgfParse PgfParse;
|
|||||||
|
|
||||||
/// Begin parsing
|
/// Begin parsing
|
||||||
PgfParse*
|
PgfParse*
|
||||||
pgf_parser_parse(PgfAbstr* abstr, PgfConcr* concr, PgfCId cat, size_t lin_idx, GuPool* pool);
|
pgf_parser_parse(PgfConcr* concr, PgfCId cat, size_t lin_idx, GuPool* pool);
|
||||||
/**<
|
/**<
|
||||||
* @param parser The parser to use
|
* @param parser The parser to use
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ pgf_print_cncfun(PgfCncFun *cncfun, PgfSequences *sequences,
|
|||||||
}
|
}
|
||||||
|
|
||||||
gu_puts(") [", wtr, err);
|
gu_puts(") [", wtr, err);
|
||||||
gu_string_write(cncfun->fun, wtr, err);
|
gu_string_write(cncfun->name, wtr, err);
|
||||||
gu_puts("]\n", wtr, err);
|
gu_puts("]\n", wtr, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ struct PgfReader {
|
|||||||
GuPool* opool;
|
GuPool* opool;
|
||||||
GuPool* tmp_pool;
|
GuPool* tmp_pool;
|
||||||
GuSymTable* symtab;
|
GuSymTable* symtab;
|
||||||
|
PgfAbstr* curr_abstr;
|
||||||
PgfConcr* curr_concr;
|
PgfConcr* curr_concr;
|
||||||
GuMap* curr_lindefs;
|
GuMap* curr_lindefs;
|
||||||
PgfContsMap* curr_conts_map; // used temporary for building the bu index for the parser
|
PgfContsMap* curr_conts_map; // used temporary for building the bu index for the parser
|
||||||
@@ -619,6 +620,13 @@ pgf_read_to_PgfFunId(GuType* type, PgfReader* rdr, void* to)
|
|||||||
*(PgfFunId*) to = gu_list_elems(rdr->curr_concr->cncfuns)[id];
|
*(PgfFunId*) to = gu_list_elems(rdr->curr_concr->cncfuns)[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
pgf_read_to_PgfAbstr(GuType* type, PgfReader* rdr, void* to)
|
||||||
|
{
|
||||||
|
rdr->curr_abstr = to;
|
||||||
|
pgf_read_to_struct(type, rdr, to);
|
||||||
|
}
|
||||||
|
|
||||||
static GU_DEFINE_TYPE(PgfLinDefs, GuIntMap, gu_ptr_type(PgfFunIds),
|
static GU_DEFINE_TYPE(PgfLinDefs, GuIntMap, gu_ptr_type(PgfFunIds),
|
||||||
&gu_null_struct);
|
&gu_null_struct);
|
||||||
typedef PgfCCat PgfCCatData;
|
typedef PgfCCat PgfCCatData;
|
||||||
@@ -755,6 +763,8 @@ pgf_read_new_PgfConcr(GuType* type, PgfReader* rdr, GuPool* pool,
|
|||||||
for (int funid = 0; funid < n_funs; funid++) {
|
for (int funid = 0; funid < n_funs; funid++) {
|
||||||
PgfCncFun* cncfun = gu_list_index(concr->cncfuns, funid);
|
PgfCncFun* cncfun = gu_list_index(concr->cncfuns, funid);
|
||||||
cncfun->funid = funid;
|
cncfun->funid = funid;
|
||||||
|
cncfun->absfun =
|
||||||
|
gu_map_get(rdr->curr_abstr->funs, &cncfun->name, PgfFunDecl*);
|
||||||
}
|
}
|
||||||
|
|
||||||
return concr;
|
return concr;
|
||||||
@@ -764,8 +774,7 @@ static void*
|
|||||||
pgf_read_new_PgfCncCat(GuType* type, PgfReader* rdr, GuPool* pool,
|
pgf_read_new_PgfCncCat(GuType* type, PgfReader* rdr, GuPool* pool,
|
||||||
size_t* size_out)
|
size_t* size_out)
|
||||||
{
|
{
|
||||||
PgfCId cid = *(PgfCId*) rdr->curr_key;
|
gu_enter("->");
|
||||||
gu_enter("-> cid");
|
|
||||||
(void) (type && size_out);
|
(void) (type && size_out);
|
||||||
|
|
||||||
int first = pgf_read_int(rdr);
|
int first = pgf_read_int(rdr);
|
||||||
@@ -774,7 +783,10 @@ pgf_read_new_PgfCncCat(GuType* type, PgfReader* rdr, GuPool* pool,
|
|||||||
|
|
||||||
PgfCncCat* cnccat =
|
PgfCncCat* cnccat =
|
||||||
gu_malloc(pool, sizeof(PgfCncCat)+n_lins*sizeof(GuString));
|
gu_malloc(pool, sizeof(PgfCncCat)+n_lins*sizeof(GuString));
|
||||||
cnccat->cid = cid;
|
|
||||||
|
cnccat->abscat =
|
||||||
|
gu_map_get(rdr->curr_abstr->cats, rdr->curr_key, PgfCat*);
|
||||||
|
gu_assert(cnccat->abscat != NULL);
|
||||||
|
|
||||||
int len = last + 1 - first;
|
int len = last + 1 - first;
|
||||||
cnccat->cats = gu_new_list(PgfCCatIds, pool, len);
|
cnccat->cats = gu_new_list(PgfCCatIds, pool, len);
|
||||||
@@ -834,6 +846,7 @@ pgf_read_to_table = GU_TYPETABLE(
|
|||||||
PGF_READ_TO(PgfCCat),
|
PGF_READ_TO(PgfCCat),
|
||||||
PGF_READ_TO(PgfSeqId),
|
PGF_READ_TO(PgfSeqId),
|
||||||
PGF_READ_TO(PgfFunId),
|
PGF_READ_TO(PgfFunId),
|
||||||
|
PGF_READ_TO(PgfAbstr),
|
||||||
PGF_READ_TO(alias));
|
PGF_READ_TO(alias));
|
||||||
|
|
||||||
#define PGF_READ_NEW_FN(k_, fn_) \
|
#define PGF_READ_NEW_FN(k_, fn_) \
|
||||||
@@ -863,6 +876,7 @@ pgf_new_reader(GuIn* in, GuPool* opool, GuPool* tmp_pool, GuExn* err)
|
|||||||
rdr->symtab = gu_new_symtable(opool, tmp_pool);
|
rdr->symtab = gu_new_symtable(opool, tmp_pool);
|
||||||
rdr->err = err;
|
rdr->err = err;
|
||||||
rdr->in = in;
|
rdr->in = in;
|
||||||
|
rdr->curr_abstr = NULL;
|
||||||
rdr->curr_concr = NULL;
|
rdr->curr_concr = NULL;
|
||||||
rdr->curr_lindefs = NULL;
|
rdr->curr_lindefs = NULL;
|
||||||
rdr->curr_conts_map = NULL;
|
rdr->curr_conts_map = NULL;
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ int main(int argc, char* argv[]) {
|
|||||||
GuString cat;
|
GuString cat;
|
||||||
bool robust_mode;
|
bool robust_mode;
|
||||||
if (argv[2][0] == '.') {
|
if (argv[2][0] == '.') {
|
||||||
printf("%s\n", argv[2]+1);
|
|
||||||
cat = gu_str_string(argv[2]+1, pool);
|
cat = gu_str_string(argv[2]+1, pool);
|
||||||
robust_mode = true;
|
robust_mode = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -116,7 +115,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
// Begin parsing a sentence of the specified category
|
// Begin parsing a sentence of the specified category
|
||||||
PgfParse* parse =
|
PgfParse* parse =
|
||||||
pgf_parser_parse(&pgf->abstract, from_concr, cat, lin_idx, pool);
|
pgf_parser_parse(from_concr, cat, lin_idx, pool);
|
||||||
if (parse == NULL) {
|
if (parse == NULL) {
|
||||||
fprintf(stderr, "Couldn't begin parsing\n");
|
fprintf(stderr, "Couldn't begin parsing\n");
|
||||||
status = EXIT_FAILURE;
|
status = EXIT_FAILURE;
|
||||||
|
|||||||
Reference in New Issue
Block a user