예제 #1
0
파일: printer.c 프로젝트: McMbuvi/GF
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);
    GuWriter *wtr = clo->wtr;
    
    gu_puts("    ", wtr, err);
    gu_string_write(name, wtr, err);
    gu_puts(" :=\n", wtr, err);
    
    PgfCCat *start = gu_list_index(cnccat->cats, 0);
    PgfCCat *end   = gu_list_index(cnccat->cats, gu_list_length(cnccat->cats)-1);
    
    gu_printf(wtr, err, "       range  [C%d..C%d]\n", start->fid, end->fid);
    
    gu_puts("       labels [", wtr, err);
    for (size_t i = 0; i < cnccat->n_lins; i++) {
		if (i > 0) {
			gu_puts("\n               ", wtr, err);
		}

		gu_string_write(cnccat->labels[i], wtr, err);
	}
    gu_puts("]\n", wtr, err);
}
예제 #2
0
파일: printer.c 프로젝트: McMbuvi/GF
void
pgf_print_symbol(PgfSymbol sym, GuWriter *wtr, GuExn *err)
{
	switch (gu_variant_tag(sym)) {
	case PGF_SYMBOL_CAT: {
		PgfSymbolCat* scat = gu_variant_data(sym);
		gu_printf(wtr, err, "<%d,%d>", scat->d, scat->r);
		break;
	}
	case PGF_SYMBOL_KS: {
		PgfSymbolKS* sks = gu_variant_data(sym);
		pgf_print_tokens(sks->tokens, wtr, err);
		break;
	}
	case PGF_SYMBOL_KP: {
		PgfSymbolKP* skp = gu_variant_data(sym);

		gu_puts("pre {", wtr, err);
		pgf_print_tokens(skp->default_form, wtr, err);
		
		for (size_t i = 0; i < skp->n_forms; i++) {
			gu_puts("; ", wtr, err);
            pgf_print_tokens(skp->forms[i].form, wtr, err);
            gu_puts(" / ", wtr, err);
            
            size_t n_prefixes = gu_list_length(skp->forms[i].prefixes);
            for (size_t j = 0; j < n_prefixes; j++) {
				if (j > 0) gu_putc(' ', wtr, err);
				
				GuString prefix = gu_list_index(skp->forms[i].prefixes, j);
				gu_putc('"', wtr, err);
				gu_string_write(prefix, wtr, err);
				gu_putc('"', wtr, err);
			}
		}
		
		gu_puts("}", wtr, err);
		break;
	}
	case PGF_SYMBOL_LIT: {
		PgfSymbolLit *slit = gu_variant_data(sym);
		gu_printf(wtr, err, "{%d,%d}", slit->d, slit->r);
		break;
	}
	case PGF_SYMBOL_VAR: {
		PgfSymbolVar *svar = gu_variant_data(sym);
		gu_printf(wtr, err, "<%d,$%d>", svar->d, svar->r);
		break;
	}
	default:
		gu_impossible();
	}
}
예제 #3
0
파일: printer.c 프로젝트: 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);
}
예제 #4
0
파일: printer.c 프로젝트: McMbuvi/GF
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);
    GuWriter *wtr = clo->wtr;
    
    gu_puts("  flag ", wtr, err);
    gu_string_write(flag, wtr, err);
    gu_puts(" = ", wtr, err);
    pgf_print_literal(lit, wtr, err);
    gu_puts(";\n", wtr, err);
}
예제 #5
0
파일: printer.c 프로젝트: McMbuvi/GF
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);
    GuWriter *wtr = clo->wtr;
    
    gu_puts(gu_seq_is_null(fun->defns) ? "  data " : "  fun ", wtr, err);
    gu_string_write(name, wtr, err);
    gu_puts(" : ", wtr, err);
    pgf_print_type(fun->type, 0, wtr, err);
    gu_printf(wtr, err, " ;   -- %f\n", fun->ep.prob);
}
예제 #6
0
파일: printer.c 프로젝트: McMbuvi/GF
static void
pgf_print_abstract(PgfAbstr* abstr, GuWriter* wtr, GuExn* err)
{
	gu_puts("abstract ", wtr, err);
	gu_string_write(abstr->name, wtr, err);
	gu_puts(" {\n", wtr, err);
	
	PgfPrintFn clo1 = { { pgf_print_flag }, wtr };
	gu_map_iter(abstr->aflags, &clo1.fn, err);

	PgfPrintFn clo2 = { { pgf_print_cat }, wtr };
	gu_map_iter(abstr->cats, &clo2.fn, err);

	PgfPrintFn clo3 = { { pgf_print_absfun }, wtr };
	gu_map_iter(abstr->funs, &clo3.fn, err);
	
	gu_puts("}\n", wtr, err);
}
예제 #7
0
파일: printer.c 프로젝트: 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);
}
예제 #8
0
파일: printer.c 프로젝트: McMbuvi/GF
static void
pgf_print_concrete(PgfCId cncname, PgfConcr* concr, 
                   GuWriter* wtr, GuExn* err)
{
	gu_puts("concrete ", wtr, err);
	gu_string_write(cncname, wtr, err);
	gu_puts(" {\n", wtr, err);

	PgfPrintFn clo1 = { { pgf_print_flag }, wtr };
	gu_map_iter(concr->cflags, &clo1.fn, err);

	gu_puts("  productions\n", wtr, err);
	PgfPrintFn clo2 = { { pgf_print_productions }, wtr };
	gu_map_iter(concr->ccats, &clo2.fn, err);

	gu_puts("  lindefs\n", wtr, err);
	PgfPrintFn clo3 = { { pgf_print_lindefs }, wtr };
	gu_map_iter(concr->ccats, &clo3.fn, err);

	gu_puts("  lin\n", wtr, err);
	size_t n_funs = gu_list_length(concr->cncfuns);
	for (size_t i = 0; i < n_funs; i++) {
		PgfCncFun* cncfun = gu_list_index(concr->cncfuns, i);
		pgf_print_cncfun(cncfun, concr->sequences, wtr, err);
	}

	gu_puts("  sequences\n", wtr, err);
	size_t n_seqs = gu_list_length(concr->sequences);
	for (size_t i = 0; i < n_seqs; i++) {
		PgfSequence seq = gu_list_index(concr->sequences, i);
		pgf_print_sequence(i, seq, wtr, err);
	}
	
	gu_puts("  categories\n", wtr, err);
	PgfPrintFn clo4 = { { pgf_print_cnccat }, wtr };
	gu_map_iter(concr->cnccats, &clo4.fn, err);
	
	gu_puts("}\n", wtr, err);
}
예제 #9
0
파일: printer.c 프로젝트: McMbuvi/GF
static void
pgf_print_cncfun(PgfCncFun *cncfun, PgfSequences *sequences, 
					GuWriter *wtr, GuExn *err)
{
	gu_printf(wtr,err,"    F%d := (", cncfun->funid);
	
	size_t n_seqs = gu_list_length(sequences);
	
	for (size_t i = 0; i < cncfun->n_lins; i++) {
		if (i > 0) gu_putc(',', wtr, err);
		PgfSequence seq = cncfun->lins[i];

		for (size_t seqid = 0; seqid < n_seqs; seqid++) {
			if (gu_seq_data(gu_list_index(sequences, seqid)) == gu_seq_data(seq)) {
				gu_printf(wtr,err,"S%d", seqid);
				break;
			}
		}
	}
	
	gu_puts(") [", wtr, err);
	gu_string_write(cncfun->name, wtr, err);
	gu_puts("]\n", wtr, err);
}
예제 #10
0
int main(int argc, char* argv[]) {
	// Set the character locale, so we can produce proper output.
	setlocale(LC_CTYPE, "");

	// Create the pool that is used to allocate everything
	GuPool* pool = gu_new_pool();
	int status = EXIT_SUCCESS;
	if (argc < 5 || argc > 6) {
		fprintf(stderr, "usage: %s pgf cat from-lang to-lang [probs-file]\n", argv[0]);
		status = EXIT_FAILURE;
		goto fail;
	}
	char* filename = argv[1];

	GuString cat = gu_str_string(argv[2], pool);

	GuString from_lang = gu_str_string(argv[3], pool);
	GuString to_lang = gu_str_string(argv[4], pool);
	
	// Create an exception frame that catches all errors.
	GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);

	// Read the PGF grammar.
	PgfPGF* pgf = pgf_read(filename, pool, err);

	// If an error occured, it shows in the exception frame
	if (!gu_ok(err)) {
		fprintf(stderr, "Reading PGF failed\n");
		status = EXIT_FAILURE;
		goto fail;
	}

	if (argc == 6) {
		char* meta_probs_filename = argv[5];
		pgf_load_meta_child_probs(pgf, meta_probs_filename, pool, err);
		if (!gu_ok(err)) {
			fprintf(stderr, "Loading meta child probs failed\n");
			status = EXIT_FAILURE;
			goto fail;
		}
	}

	// Look up the source and destination concrete categories
	PgfConcr* from_concr = pgf_get_language(pgf, from_lang);
	PgfConcr* to_concr = pgf_get_language(pgf, to_lang);
	if (!from_concr || !to_concr) {
		fprintf(stderr, "Unknown language\n");
		status = EXIT_FAILURE;
		goto fail_concr;
	}
	
	// Register a callback for the literal category Symbol
	pgf_parser_add_literal(from_concr, gu_str_string("Symb", pool),
	                       &pgf_nerc_literal_callback);

	// Create an output stream for stdout
	GuOut* out = gu_file_out(stdout, pool);

	// Locale-encoding writers are currently unsupported
	// GuWriter* wtr = gu_locale_writer(out, pool);
	// Use a writer with hard-coded utf-8 encoding for now.
	GuWriter* wtr = gu_new_utf8_writer(out, pool);

	// We will keep the latest results in the 'ppool' and
	// we will iterate over them by using 'result'.
	GuPool* ppool = NULL;
	GuEnum* result = NULL;

	// The interactive translation loop.
	// XXX: This currently reads stdin directly, so it doesn't support
	// encodings properly. TODO: use a locale reader for input
	while (true) {
		fprintf(stdout, "> ");
		fflush(stdout);
		char buf[4096];
		char* line = fgets(buf, sizeof(buf), stdin);
		if (line == NULL) {
			if (ferror(stdin)) {
				fprintf(stderr, "Input error\n");
				status = EXIT_FAILURE;
			}
			break;
		} else if (strcmp(line, "") == 0) {
			// End nicely on empty input
			break;
		} else if (strcmp(line, "\n") == 0) {
			// Empty line -> show the next tree for the last sentence

			if (result != NULL) {
				clock_t start = clock();

				PgfExprProb* ep = gu_next(result, PgfExprProb*, ppool);

				clock_t end = clock();
				double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
				printf("%.2f sec\n", cpu_time_used);

				// The enumerator will return a null variant at the
				// end of the results.
				if (ep == NULL) {
					goto fail_parse;
				}
				
				print_result(ep, to_concr, wtr, err, ppool);
			}
			continue;
		}

		// We release the last results
		if (ppool != NULL) {
			gu_pool_free(ppool);
			ppool  = NULL;
			result = NULL;
		}
		
		// We create a temporary pool for translating a single
		// sentence, so our memory usage doesn't increase over time.
		ppool = gu_new_pool();

		GuReader *rdr =
			gu_string_reader(gu_str_string(line, ppool), ppool);
		PgfLexer *lexer =
			pgf_new_lexer(rdr, ppool);

		clock_t start = clock();

		GuEnum* result =
			pgf_parse(from_concr, cat, lexer, ppool);
		if (result == NULL) {
			PgfToken tok =
				pgf_lexer_current_token(lexer);

			if (gu_string_eq(tok, gu_empty_string))
				gu_puts("Couldn't begin parsing", wtr, err);
			else {
				gu_puts("Unexpected token: \"", wtr, err);
				gu_string_write(tok, wtr, err);
				gu_puts("\"\n", wtr, err);
			}

			goto fail_parse;
		}

		PgfExprProb* ep = gu_next(result, PgfExprProb*, ppool);

		clock_t end = clock();
		double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
		printf("%.2f sec\n", cpu_time_used);

		// The enumerator will return null at the end of the results.
		if (ep == NULL) {
			goto fail_parse;
		}
		
		print_result(ep, to_concr, wtr, err, ppool);

		continue;
	fail_parse:
		// Free all resources allocated during parsing and linearization
		gu_pool_free(ppool);
		ppool = NULL;
		result = NULL;
	}
예제 #11
0
파일: pgf-translate.c 프로젝트: k0001/GF
int main(int argc, char* argv[]) {
    // Set the character locale, so we can produce proper output.
    setlocale(LC_CTYPE, "");

    // Create the pool that is used to allocate everything
    GuPool* pool = gu_new_pool();
    int status = EXIT_SUCCESS;
    if (argc < 5) {
        fprintf(stderr, "usage: %s pgf cat from-lang to-lang\n", argv[0]);
        status = EXIT_FAILURE;
        goto fail;
    }

    GuString filename = argv[1];
    GuString cat = argv[2];
    GuString from_lang = argv[3];
    GuString to_lang = argv[4];

    // Create an exception frame that catches all errors.
    GuExn* err = gu_new_exn(pool);

    // Read the PGF grammar.
    PgfPGF* pgf = pgf_read(filename, pool, err);

    // If an error occured, it shows in the exception frame
    if (!gu_ok(err)) {
        fprintf(stderr, "Reading PGF failed\n");
        status = EXIT_FAILURE;
        goto fail;
    }

    // Look up the source and destination concrete categories
    PgfConcr* from_concr = pgf_get_language(pgf, from_lang);
    PgfConcr* to_concr = pgf_get_language(pgf, to_lang);
    if (!from_concr || !to_concr) {
        fprintf(stderr, "Unknown language\n");
        status = EXIT_FAILURE;
        goto fail_concr;
    }

    // Register a callback for the literal category Symbol
    PgfCallbacksMap* callbacks =
        pgf_new_callbacks_map(from_concr, pool);
    pgf_callbacks_map_add_literal(from_concr, callbacks,
                                  "PN", &pgf_nerc_literal_callback);
    pgf_callbacks_map_add_literal(from_concr, callbacks,
                                  "Symb", &pgf_unknown_literal_callback);

    // Create an output stream for stdout
    GuOut* out = gu_file_out(stdout, pool);

    // We will keep the latest results in the 'ppool' and
    // we will iterate over them by using 'result'.
    GuPool* ppool = NULL;
    GuEnum* result = NULL;

    // The interactive translation loop.
    // XXX: This currently reads stdin directly, so it doesn't support
    // encodings properly. TODO: use a locale reader for input
    while (true) {
        fprintf(stdout, "> ");
        fflush(stdout);
        char buf[4096];
        char* line = fgets(buf, sizeof(buf), stdin);
        if (line == NULL) {
            if (ferror(stdin)) {
                fprintf(stderr, "Input error\n");
                status = EXIT_FAILURE;
            }
            break;
        } else if (strcmp(line, "") == 0) {
            // End nicely on empty input
            break;
        } else if (strcmp(line, "\n") == 0) {
            // Empty line -> show the next tree for the last sentence

            if (result != NULL) {
                clock_t start = clock();

                PgfExprProb* ep = gu_next(result, PgfExprProb*, ppool);

                clock_t end = clock();
                double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
                printf("%.2f sec\n", cpu_time_used);

                // The enumerator will return a null variant at the
                // end of the results.
                if (ep == NULL) {
                    goto fail_parse;
                }

                print_result(ep, to_concr, out, err, ppool);
            }
            continue;
        }

        // We release the last results
        if (ppool != NULL) {
            gu_pool_free(ppool);
            ppool  = NULL;
            result = NULL;
        }

        // We create a temporary pool for translating a single
        // sentence, so our memory usage doesn't increase over time.
        ppool = gu_new_pool();

        clock_t start = clock();

        GuExn* parse_err = gu_new_exn(ppool);
        result =
            pgf_parse_with_heuristics(from_concr, cat, line,
                                      -1, callbacks,
                                      parse_err, ppool, ppool);
        if (!gu_ok(parse_err)) {
            if (gu_exn_caught(parse_err, PgfExn)) {
                GuString msg = gu_exn_caught_data(parse_err);
                gu_string_write(msg, out, err);
                gu_putc('\n', out, err);
            } else if (gu_exn_caught(parse_err, PgfParseError)) {
                gu_puts("Unexpected token: \"", out, err);
                GuString tok = gu_exn_caught_data(parse_err);
                gu_string_write(tok, out, err);
                gu_puts("\"\n", out, err);
            }

            goto fail_parse;
        }

        PgfExprProb* ep = gu_next(result, PgfExprProb*, ppool);

        clock_t end = clock();
        double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
        printf("%.2f sec\n", cpu_time_used);

        // The enumerator will return null at the end of the results.
        if (ep == NULL) {
            goto fail_parse;
        }

        print_result(ep, to_concr, out, err, ppool);

        continue;
fail_parse:
        // Free all resources allocated during parsing and linearization
        gu_pool_free(ppool);
        ppool = NULL;
        result = NULL;
    }