Example #1
0
File: printer.c Project: McMbuvi/GF
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);
    GuWriter *wtr = clo->wtr;

	if (!gu_seq_is_null(ccat->prods)) {
		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(wtr,err,"    C%d -> ",fid);

			GuVariantInfo i = gu_variant_open(prod);
			switch (i.tag) {
			case PGF_PRODUCTION_APPLY: {
				PgfProductionApply* papp = i.data;
				gu_printf(wtr,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(',',wtr,err);
                    
					PgfPArg arg = gu_seq_get(papp->args, PgfPArg, j);

					if (arg.hypos != NULL) {
						size_t n_hypos = gu_list_length(arg.hypos);
						for (size_t k = 0; k < n_hypos; k++) {
							if (k > 0)
								gu_putc(' ',wtr,err);
							PgfCCat *hypo = gu_list_index(arg.hypos, k);
							gu_printf(wtr,err,"C%d",hypo->fid);
						}
					}
            
					gu_printf(wtr,err,"C%d",arg.ccat->fid);
				}
				gu_printf(wtr,err,"]\n");
				break;
			}
			case PGF_PRODUCTION_COERCE: {
				PgfProductionCoerce* pcoerce = i.data;
				gu_printf(wtr,err,"_[C%d]\n",pcoerce->coerce->fid);
				break;
			}
			default:
				gu_impossible();
			}
		}
	}
}
Example #2
0
File: reader.c Project: k0001/GF
static PgfCncFun*
pgf_read_cncfun(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr, int funid)
{
	PgfCId name = pgf_read_cid(rdr, rdr->tmp_pool);
	gu_return_on_exn(rdr->err, NULL);

	size_t len = pgf_read_len(rdr);
	gu_return_on_exn(rdr->err, NULL);

	PgfAbsFun* absfun =
		gu_seq_binsearch(abstr->funs, pgf_absfun_order, PgfAbsFun, name);

	PgfCncFun* cncfun = gu_new_flex(rdr->opool, PgfCncFun, lins, len);
	cncfun->absfun = absfun;
	cncfun->ep = (absfun == NULL) ? NULL : &absfun->ep;
	cncfun->funid = funid;
	cncfun->n_lins = len;

	for (size_t i = 0; i < len; i++) {
		size_t seqid = pgf_read_int(rdr);
		gu_return_on_exn(rdr->err, NULL);

		if (seqid >= gu_seq_length(concr->sequences)) {
			gu_raise(rdr->err, PgfReadExn);
			return NULL;
		}
		
		cncfun->lins[i] = gu_seq_index(concr->sequences, PgfSequence, seqid);
	}

	return cncfun;
}
Example #3
0
bool 
pgf_tokens_equal(PgfTokens t1, PgfTokens t2)
{
	size_t len1 = gu_seq_length(t1);
	size_t len2 = gu_seq_length(t2);
	if (len1 != len2) {
		return false;
	}
	for (size_t i = 0; i < len1; i++) {
		GuString s1 = gu_seq_get(t1, PgfToken, i);
		GuString s2 = gu_seq_get(t2, PgfToken, i);
		if (!gu_string_eq(s1, s2)) {
			return false;
		}
	}
	return true;
}
Example #4
0
static void
pgf_print_abs_production(PgfMetaId id,
                         PgfAbsProduction* prod,
                         GuOut* out, GuExn* err)
{
	gu_printf(out,err,"?%d = %s",id,prod->fun->name);
	size_t n_hypos = gu_seq_length(prod->fun->type->hypos);
	for (size_t i = 0; i < n_hypos; i++) {
		gu_printf(out,err," ?%d", prod->args[i]);
	}
	gu_printf(out,err,"  <%d>\n",prod->count);
}
Example #5
0
File: printer.c Project: McMbuvi/GF
static void
pgf_print_tokens(PgfTokens tokens, GuWriter *wtr, GuExn *err)
{
	gu_putc('"', wtr, err);
	size_t n_toks = gu_seq_length(tokens);
	for (size_t i = 0; i < n_toks; i++) {
		if (i > 0) gu_putc(' ', wtr, err);
			
		PgfToken tok = gu_seq_get(tokens, PgfToken, i);
		gu_string_write(tok, wtr, err);
	}
	gu_putc('"', wtr, err);
}
Example #6
0
static void
pgf_metrics_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens toks)
{
	PgfMetricsLznState* state = gu_container(funcs, PgfMetricsLznState, funcs);
	
	size_t len = gu_seq_length(toks);
	for (size_t i = 0; i < len; i++) {
		PgfToken tok = gu_seq_get(toks, PgfToken, i);
		
		if (state->ps != NULL)
			state->ps = pgf_parser_next_state(state->ps, tok, state->pool);

		state->pos++;
	}
}
Example #7
0
File: printer.c Project: McMbuvi/GF
static void
pgf_print_sequence(size_t seqid, PgfSequence seq, GuWriter *wtr, GuExn *err)
{
	gu_printf(wtr,err,"    S%d := ", seqid);

	int n_syms = gu_seq_length(seq);
	for (int i = 0; i < n_syms; i++) {
		if (i > 0) gu_putc(' ', wtr, err);
			
		PgfSymbol sym = gu_seq_get(seq, PgfSymbol, i);
		pgf_print_symbol(sym, wtr, err);
	}

	gu_putc('\n', wtr, err);
}
Example #8
0
File: printer.c Project: McMbuvi/GF
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);
    GuWriter *wtr = clo->wtr;

    gu_puts("  cat ", wtr, err);
    gu_string_write(name, wtr, err);

    size_t n_hypos = gu_seq_length(cat->context);
    for (size_t i = 0; i < n_hypos; i++) {
		PgfHypo* hypo = gu_seq_get(cat->context, PgfHypo*, i);
		gu_putc(' ', wtr, err);
		pgf_print_hypo(hypo, 4, wtr, err);
	}

    gu_printf(wtr, err, " ;   -- %f\n",cat->meta_prob);
}
Example #9
0
static void
pgf_lookup_index_syms(GuMap* lexicon_idx, PgfSymbols* syms, PgfAbsFun* absfun, GuPool* pool) {
	size_t n_syms = gu_seq_length(syms);
	for (size_t j = 0; j < n_syms; j++) {
		PgfSymbol sym = gu_seq_get(syms, PgfSymbol, j);
		GuVariantInfo i = gu_variant_open(sym);
		switch (i.tag) {
		case PGF_SYMBOL_KP: {
			PgfSymbolKP* skp = (PgfSymbolKP*) i.data;
			pgf_lookup_index_syms(lexicon_idx, skp->default_form, absfun, pool);
			for (size_t k = 0; k < skp->n_forms; k++) {
				pgf_lookup_index_syms(lexicon_idx, skp->forms[k].form, absfun, pool);
			}
			break;
		}
		case PGF_SYMBOL_KS: {
			PgfSymbolKS* sks = (PgfSymbolKS*) i.data;
			GuBuf* funs = gu_map_get(lexicon_idx, sks->token, GuBuf*);
			if (funs == NULL) {
				funs = gu_new_buf(PgfAbsFun*, pool);
				gu_map_put(lexicon_idx, sks->token, GuBuf*, funs);
			}

			bool found = false;
			size_t n_funs = gu_buf_length(funs);
			for (size_t l = 0; l < n_funs; l++) {
				PgfAbsFun* absfun1 = gu_buf_get(funs, PgfAbsFun*, l);
				if (absfun1 == absfun) {
					found = true;
					break;
				}
			}
			if (!found)
				gu_buf_push(funs, PgfAbsFun*, absfun);
			break;
		}
		}
	}