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(); } }
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); }
static void print_result(PgfExprProb* ep, PgfConcr* to_concr, GuWriter* wtr, GuExn* err, GuPool* ppool) { // Write out the abstract syntax tree gu_printf(wtr, err, " [%f] ", ep->prob); pgf_print_expr(ep->expr, 0, wtr, err); gu_putc('\n', wtr, err); // Enumerate the concrete syntax trees corresponding // to the abstract tree. GuEnum* cts = pgf_lzr_concretize(to_concr, ep->expr, ppool); while (true) { PgfCncTree ctree = gu_next(cts, PgfCncTree, ppool); if (gu_variant_is_null(ctree)) { break; } gu_putc(' ', wtr, err); // Linearize the concrete tree as a simple // sequence of strings. pgf_lzr_linearize_simple(to_concr , ctree, 0, wtr, err); gu_putc('\n', wtr, err); gu_writer_flush(wtr, err); } }
static void print_result(PgfExprProb* ep, PgfConcr* to_concr, GuOut* out, GuExn* err, GuPool* ppool) { // Write out the abstract syntax tree gu_printf(out, err, " [%f] ", ep->prob); pgf_print_expr(ep->expr, NULL, 0, out, err); gu_putc('\n', out, err); // Enumerate the concrete syntax trees corresponding // to the abstract tree. GuEnum* cts = pgf_lzr_concretize(to_concr, ep->expr, err, ppool); while (true) { PgfCncTree ctree = gu_next(cts, PgfCncTree, ppool); if (gu_variant_is_null(ctree)) { break; } gu_putc(' ', out, err); // Linearize the concrete tree as a simple // sequence of strings. pgf_lzr_linearize_simple(to_concr, ctree, 0, out, err, ppool); if (gu_exn_caught(err, PgfLinNonExist)) { // encountered nonExist. Unfortunately there // might be some output printed already. The // right solution should be to use GuStringBuf. gu_exn_clear(err); } gu_putc('\n', out, err); gu_out_flush(out, err); } }
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); }
static void pgf_print_lindefs(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 (ccat->lindefs != NULL) { gu_printf(wtr,err," C%d -> ",fid); size_t n_lindefs = gu_list_length(ccat->lindefs); for (size_t i = 0; i < n_lindefs; i++) { if (i > 0) gu_putc(' ', wtr, err); PgfCncFun* fun = gu_list_index(ccat->lindefs, i); gu_printf(wtr,err,"F%d",fun->funid); } gu_putc('\n', wtr,err); } }
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); }
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); }
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); }
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(); } } } }
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); }
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 < 4 || argc > 5) { fprintf(stderr, "usage: %s pgf-file start-cat cnc-lang [heuristics]\n(0.0 <= heuristics < 1.0, default: 0.95)\n", argv[0]); status = EXIT_FAILURE; goto fail; } char* filename = argv[1]; GuString cat = argv[2]; GuString lang = argv[3]; double heuristics = 0.95; if (argc == 5) { heuristics = atof(argv[4]); } // Create an exception frame that catches all errors. GuExn* err = gu_new_exn(pool); clock_t start = clock(); // 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* concr = pgf_get_language(pgf, lang); if (!concr) { fprintf(stderr, "Unknown language\n"); status = EXIT_FAILURE; goto fail; } clock_t end = clock(); double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; fprintf(stderr, "(%.0f ms) Ready to parse [heuristics=%.2f]!\n", 1000.0 * cpu_time_used, heuristics); // 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; // The interactive PARSING loop. // XXX: This currently reads stdin directly, so it doesn't support // encodings properly. TODO: use a locale reader for input for (int ctr = 0; true; ctr++) { // We release the last results if (ppool != NULL) { gu_pool_free(ppool); ppool = NULL; } /* 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 -> skip continue; } // 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); PgfCallbacksMap* callbacks = pgf_new_callbacks_map(concr, ppool); GuEnum* result = pgf_parse_with_heuristics(concr, cat, line, heuristics, callbacks, parse_err, ppool, ppool); PgfExprProb* ep = NULL; if (gu_ok(parse_err)) ep = gu_next(result, PgfExprProb*, ppool); clock_t end = clock(); double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; gu_printf(out, err, "%d (%.0f ms): ", ctr, 1000.0 * cpu_time_used); if (ep != NULL) { gu_printf(out, err, "[%.4f] (", ep->prob); pgf_print_expr(ep->expr, NULL, 0, out, err); gu_printf(out, err, ")\n"); } else { gu_printf(out, err, "---\n"); } gu_out_flush(out, err); } fail: gu_pool_free(pool); return status; }