forked from GitHub/gf-core
minor optimization/refactoring in the grammar reader
This commit is contained in:
@@ -35,60 +35,34 @@ pgf_lzr_add_overl_entry(PgfCncOverloadMap* overl_table,
|
|||||||
gu_buf_push(entries, void*, entry);
|
gu_buf_push(entries, void*, entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
GuMapItor fn;
|
|
||||||
PgfConcr* concr;
|
|
||||||
GuPool* pool;
|
|
||||||
} PgfLzrIndexFn;
|
|
||||||
|
|
||||||
static void
|
|
||||||
pgf_lzr_index_itor(GuMapItor* fn, const void* key, void* value, GuExn* err)
|
|
||||||
{
|
|
||||||
(void) (key && err);
|
|
||||||
|
|
||||||
PgfLzrIndexFn* clo = (PgfLzrIndexFn*) fn;
|
|
||||||
PgfCCat* ccat = *((PgfCCat**) value);
|
|
||||||
PgfConcr *concr = clo->concr;
|
|
||||||
GuPool *pool = clo->pool;
|
|
||||||
|
|
||||||
if (gu_seq_is_null(ccat->prods))
|
|
||||||
return;
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
void* data = gu_variant_data(prod);
|
|
||||||
switch (gu_variant_tag(prod)) {
|
|
||||||
case PGF_PRODUCTION_APPLY: {
|
|
||||||
PgfProductionApply* papply = data;
|
|
||||||
PgfCncOverloadMap* overl_table =
|
|
||||||
gu_map_get(concr->fun_indices, &papply->fun->absfun->name,
|
|
||||||
PgfCncOverloadMap*);
|
|
||||||
if (!overl_table) {
|
|
||||||
overl_table = gu_map_type_new(PgfCncOverloadMap, pool);
|
|
||||||
gu_map_put(concr->fun_indices,
|
|
||||||
&papply->fun->absfun->name, PgfCncOverloadMap*, overl_table);
|
|
||||||
}
|
|
||||||
pgf_lzr_add_overl_entry(overl_table, ccat, papply, pool);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PGF_PRODUCTION_COERCE: {
|
|
||||||
PgfProductionCoerce* pcoerce = data;
|
|
||||||
pgf_lzr_add_overl_entry(concr->coerce_idx, ccat, pcoerce, pool);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
gu_impossible();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
pgf_lzr_index(PgfConcr* concr, GuPool *pool)
|
pgf_lzr_index(PgfConcr* concr,
|
||||||
|
PgfCCat* ccat, PgfProduction prod,
|
||||||
|
GuPool *pool)
|
||||||
{
|
{
|
||||||
PgfLzrIndexFn clo = { { pgf_lzr_index_itor }, concr, pool };
|
void* data = gu_variant_data(prod);
|
||||||
gu_map_iter(concr->ccats, &clo.fn, NULL);
|
switch (gu_variant_tag(prod)) {
|
||||||
|
case PGF_PRODUCTION_APPLY: {
|
||||||
|
PgfProductionApply* papply = data;
|
||||||
|
PgfCncOverloadMap* overl_table =
|
||||||
|
gu_map_get(concr->fun_indices, &papply->fun->absfun->name,
|
||||||
|
PgfCncOverloadMap*);
|
||||||
|
if (!overl_table) {
|
||||||
|
overl_table = gu_map_type_new(PgfCncOverloadMap, pool);
|
||||||
|
gu_map_put(concr->fun_indices,
|
||||||
|
&papply->fun->absfun->name, PgfCncOverloadMap*, overl_table);
|
||||||
|
}
|
||||||
|
pgf_lzr_add_overl_entry(overl_table, ccat, papply, pool);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PGF_PRODUCTION_COERCE: {
|
||||||
|
PgfProductionCoerce* pcoerce = data;
|
||||||
|
pgf_lzr_add_overl_entry(concr->coerce_idx, ccat, pcoerce, pool);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
gu_impossible();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct PgfLzn PgfLzn;
|
typedef struct PgfLzn PgfLzn;
|
||||||
|
|||||||
@@ -2561,96 +2561,71 @@ pgf_parser_index_epsilon(PgfConcr* concr,
|
|||||||
gu_buf_push(gu_seq_buf(prods), PgfProduction, prod);
|
gu_buf_push(gu_seq_buf(prods), PgfProduction, prod);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
void
|
||||||
GuMapItor fn;
|
pgf_parser_index(PgfConcr* concr,
|
||||||
PgfConcr* concr;
|
PgfCCat* ccat, PgfProduction prod,
|
||||||
GuPool* pool;
|
GuPool *pool)
|
||||||
} PgfLeftcornerFn;
|
|
||||||
|
|
||||||
static void
|
|
||||||
pgf_parser_index_cats(GuMapItor* fn, const void* key, void* value, GuExn* err)
|
|
||||||
{
|
{
|
||||||
(void) (key && err);
|
|
||||||
|
|
||||||
PgfLeftcornerFn* clo = (PgfLeftcornerFn*) fn;
|
|
||||||
PgfCCat* ccat = *((PgfCCat**) value);
|
|
||||||
|
|
||||||
if (gu_seq_is_null(ccat->prods))
|
|
||||||
return;
|
|
||||||
|
|
||||||
for (size_t lin_idx = 0; lin_idx < ccat->cnccat->n_lins; lin_idx++) {
|
for (size_t lin_idx = 0; lin_idx < ccat->cnccat->n_lins; lin_idx++) {
|
||||||
size_t n_prods = gu_seq_length(ccat->prods);
|
GuVariantInfo i = gu_variant_open(prod);
|
||||||
for (size_t i = 0; i < n_prods; i++) {
|
switch (i.tag) {
|
||||||
PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i);
|
case PGF_PRODUCTION_APPLY: {
|
||||||
|
PgfProductionApply* papp = i.data;
|
||||||
|
|
||||||
GuVariantInfo i = gu_variant_open(prod);
|
if (gu_seq_length(papp->args) > 0)
|
||||||
switch (i.tag) {
|
|
||||||
case PGF_PRODUCTION_APPLY: {
|
|
||||||
PgfProductionApply* papp = i.data;
|
|
||||||
|
|
||||||
if (gu_seq_length(papp->args) > 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
PgfSequence seq = papp->fun->lins[lin_idx];
|
|
||||||
if (gu_seq_length(seq) > 0) {
|
|
||||||
GuVariantInfo i = gu_variant_open(gu_seq_get(seq, PgfSymbol, 0));
|
|
||||||
switch (i.tag) {
|
|
||||||
case PGF_SYMBOL_KS: {
|
|
||||||
PgfSymbolKS* sks = i.data;
|
|
||||||
pgf_parser_index_token(clo->concr,
|
|
||||||
sks->tokens,
|
|
||||||
ccat, lin_idx, prod,
|
|
||||||
clo->pool);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PGF_SYMBOL_KP: {
|
|
||||||
PgfSymbolKP* skp = i.data;
|
|
||||||
pgf_parser_index_token(clo->concr,
|
|
||||||
skp->default_form,
|
|
||||||
ccat, lin_idx, prod,
|
|
||||||
clo->pool);
|
|
||||||
for (size_t i = 0; i < skp->n_forms; i++) {
|
|
||||||
pgf_parser_index_token(clo->concr,
|
|
||||||
skp->forms[i].form,
|
|
||||||
ccat, lin_idx, prod,
|
|
||||||
clo->pool);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PGF_SYMBOL_CAT:
|
|
||||||
case PGF_SYMBOL_LIT:
|
|
||||||
case PGF_SYMBOL_NE:
|
|
||||||
case PGF_SYMBOL_VAR:
|
|
||||||
// Nothing to be done here
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
gu_impossible();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
pgf_parser_index_epsilon(clo->concr,
|
|
||||||
ccat, lin_idx, prod,
|
|
||||||
clo->pool);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PGF_PRODUCTION_COERCE:
|
|
||||||
// Nothing to be done here
|
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
gu_impossible();
|
PgfSequence seq = papp->fun->lins[lin_idx];
|
||||||
|
if (gu_seq_length(seq) > 0) {
|
||||||
|
GuVariantInfo i = gu_variant_open(gu_seq_get(seq, PgfSymbol, 0));
|
||||||
|
switch (i.tag) {
|
||||||
|
case PGF_SYMBOL_KS: {
|
||||||
|
PgfSymbolKS* sks = i.data;
|
||||||
|
pgf_parser_index_token(concr,
|
||||||
|
sks->tokens,
|
||||||
|
ccat, lin_idx, prod,
|
||||||
|
pool);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PGF_SYMBOL_KP: {
|
||||||
|
PgfSymbolKP* skp = i.data;
|
||||||
|
pgf_parser_index_token(concr,
|
||||||
|
skp->default_form,
|
||||||
|
ccat, lin_idx, prod,
|
||||||
|
pool);
|
||||||
|
for (size_t i = 0; i < skp->n_forms; i++) {
|
||||||
|
pgf_parser_index_token(concr,
|
||||||
|
skp->forms[i].form,
|
||||||
|
ccat, lin_idx, prod,
|
||||||
|
pool);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PGF_SYMBOL_CAT:
|
||||||
|
case PGF_SYMBOL_LIT:
|
||||||
|
case PGF_SYMBOL_NE:
|
||||||
|
case PGF_SYMBOL_VAR:
|
||||||
|
// Nothing to be done here
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
gu_impossible();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
pgf_parser_index_epsilon(concr,
|
||||||
|
ccat, lin_idx, prod,
|
||||||
|
pool);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case PGF_PRODUCTION_COERCE:
|
||||||
|
// Nothing to be done here
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
gu_impossible();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
pgf_parser_index(PgfConcr* concr, GuPool *pool)
|
|
||||||
{
|
|
||||||
PgfLeftcornerFn clo1 = { { pgf_parser_index_cats },
|
|
||||||
concr, pool };
|
|
||||||
gu_map_iter(concr->ccats, &clo1.fn, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
prob_t
|
prob_t
|
||||||
pgf_ccat_set_viterbi_prob(PgfCCat* ccat) {
|
pgf_ccat_set_viterbi_prob(PgfCCat* ccat) {
|
||||||
if (ccat->fid < 0)
|
if (ccat->fid < 0)
|
||||||
|
|||||||
@@ -1024,65 +1024,80 @@ pgf_read_cnccats(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr)
|
|||||||
return cnccats;
|
return cnccats;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
static void
|
||||||
GuMapItor fn;
|
pgf_ccat_set_cnccat(PgfCCat* ccat, PgfProduction prod)
|
||||||
PgfReader* rdr;
|
|
||||||
} PgfIndexFn;
|
|
||||||
|
|
||||||
static PgfCncCat*
|
|
||||||
pgf_ccat_set_cnccat(PgfCCat* ccat)
|
|
||||||
{
|
{
|
||||||
if (!ccat->cnccat) {
|
GuVariantInfo i = gu_variant_open(prod);
|
||||||
size_t n_prods = gu_seq_length(ccat->prods);
|
switch (i.tag) {
|
||||||
for (size_t i = 0; i < n_prods; i++) {
|
case PGF_PRODUCTION_COERCE: {
|
||||||
PgfProduction prod =
|
PgfProductionCoerce* pcoerce = i.data;
|
||||||
gu_seq_get(ccat->prods, PgfProduction, i);
|
PgfCncCat* cnccat = pcoerce->coerce->cnccat;
|
||||||
GuVariantInfo i = gu_variant_open(prod);
|
if (!ccat->cnccat) {
|
||||||
switch (i.tag) {
|
ccat->cnccat = cnccat;
|
||||||
case PGF_PRODUCTION_COERCE: {
|
} else if (ccat->cnccat != cnccat) {
|
||||||
PgfProductionCoerce* pcoerce = i.data;
|
// XXX: real error
|
||||||
PgfCncCat* cnccat =
|
gu_impossible();
|
||||||
pgf_ccat_set_cnccat(pcoerce->coerce);
|
|
||||||
if (!ccat->cnccat) {
|
|
||||||
ccat->cnccat = cnccat;
|
|
||||||
} else if (ccat->cnccat != cnccat) {
|
|
||||||
// XXX: real error
|
|
||||||
gu_impossible();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PGF_PRODUCTION_APPLY:
|
|
||||||
// Shouldn't happen with current PGF.
|
|
||||||
// XXX: real error
|
|
||||||
gu_impossible();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
gu_impossible();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PGF_PRODUCTION_APPLY:
|
||||||
|
// Shouldn't happen with current PGF.
|
||||||
|
// XXX: real error
|
||||||
|
gu_impossible();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
gu_impossible();
|
||||||
}
|
}
|
||||||
return ccat->cnccat;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern float
|
extern prob_t
|
||||||
pgf_ccat_set_viterbi_prob(PgfCCat* ccat);
|
pgf_ccat_set_viterbi_prob(PgfCCat* ccat);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
GuMapItor fn;
|
||||||
|
PgfConcr* concr;
|
||||||
|
GuPool* pool;
|
||||||
|
} PgfIndexFn;
|
||||||
|
|
||||||
|
extern void
|
||||||
|
pgf_parser_index(PgfConcr* concr,
|
||||||
|
PgfCCat* ccat, PgfProduction prod,
|
||||||
|
GuPool *pool);
|
||||||
|
|
||||||
|
void
|
||||||
|
pgf_lzr_index(PgfConcr* concr,
|
||||||
|
PgfCCat* ccat, PgfProduction prod,
|
||||||
|
GuPool *pool);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pgf_read_ccat_cb(GuMapItor* fn, const void* key, void* value, GuExn* err)
|
pgf_read_ccat_cb(GuMapItor* fn, const void* key, void* value, GuExn* err)
|
||||||
{
|
{
|
||||||
(void) (key && err);
|
(void) (key && err);
|
||||||
PgfCCat* ccat = *((PgfCCat**) value);
|
|
||||||
|
PgfIndexFn* clo = (PgfIndexFn*) fn;
|
||||||
|
PgfCCat* ccat = *((PgfCCat**) value);
|
||||||
|
PgfConcr *concr = clo->concr;
|
||||||
|
GuPool *pool = clo->pool;
|
||||||
|
|
||||||
|
if (gu_seq_is_null(ccat->prods))
|
||||||
|
return;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (!ccat->cnccat) {
|
||||||
|
pgf_ccat_set_cnccat(ccat, prod);
|
||||||
|
}
|
||||||
|
|
||||||
|
pgf_parser_index(concr, ccat, prod, pool);
|
||||||
|
pgf_lzr_index(concr, ccat, prod, pool);
|
||||||
|
}
|
||||||
|
|
||||||
pgf_ccat_set_cnccat(ccat);
|
|
||||||
// pgf_ccat_set_viterbi_prob(ccat);
|
// pgf_ccat_set_viterbi_prob(ccat);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
pgf_parser_index(PgfConcr* concr, GuPool *pool);
|
|
||||||
|
|
||||||
void
|
|
||||||
pgf_lzr_index(PgfConcr* concr, GuPool *pool);
|
|
||||||
|
|
||||||
static PgfConcr*
|
static PgfConcr*
|
||||||
pgf_read_concrete(PgfReader* rdr, PgfAbstr* abstr)
|
pgf_read_concrete(PgfReader* rdr, PgfAbstr* abstr)
|
||||||
{
|
{
|
||||||
@@ -1121,12 +1136,9 @@ pgf_read_concrete(PgfReader* rdr, PgfAbstr* abstr)
|
|||||||
concr->callbacks = pgf_new_callbacks_map(concr, rdr->opool);
|
concr->callbacks = pgf_new_callbacks_map(concr, rdr->opool);
|
||||||
concr->total_cats = pgf_read_int(rdr);
|
concr->total_cats = pgf_read_int(rdr);
|
||||||
|
|
||||||
PgfIndexFn clo1 = { { pgf_read_ccat_cb }, rdr };
|
PgfIndexFn clo1 = { { pgf_read_ccat_cb }, concr, rdr->opool };
|
||||||
gu_map_iter(concr->ccats, &clo1.fn, NULL);
|
gu_map_iter(concr->ccats, &clo1.fn, NULL);
|
||||||
|
|
||||||
pgf_parser_index(concr, rdr->opool);
|
|
||||||
pgf_lzr_index(concr, rdr->opool);
|
|
||||||
|
|
||||||
return concr;
|
return concr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user