예제 #1
0
파일: pgf-translate.c 프로젝트: k0001/GF
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);
    }
}
예제 #2
0
파일: expr.c 프로젝트: jutta526/GF
static void
pgf_expr_parser_getc(PgfExprParser* parser)
{
	parser->ch = gu_in_u8(parser->in, parser->err);
	if (!gu_ok(parser->err)) {
		gu_exn_clear(parser->err);
		parser->ch = EOF;
	}
}
예제 #3
0
static void
pgf_lexer_read_ucs(PgfLexer *lexer, GuExn* err)
{
	lexer->ucs = gu_read_ucs(lexer->rdr, err);
	if (gu_exn_is_raised(err)) {
		gu_exn_clear(err);
		lexer->ucs = ' ';
	}
}
예제 #4
0
파일: jpgf.c 프로젝트: Deseaus/GF
JNIEXPORT jobject JNICALL 
Java_org_grammaticalframework_pgf_Concr_tabularLinearize(JNIEnv* env, jobject self, jobject jexpr)
{
	jclass map_class = (*env)->FindClass(env, "java/util/HashMap");
	if (!map_class)
		return NULL;
	jmethodID constrId = (*env)->GetMethodID(env, map_class, "<init>", "()V");
	if (!constrId)
		return NULL;
	jobject table = (*env)->NewObject(env, map_class, constrId);
	if (!table)
		return NULL;

	jmethodID put_method = (*env)->GetMethodID(env, map_class, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
	if (!put_method)
		return NULL;
		
	PgfConcr* concr = get_ref(env, self);

	GuPool* tmp_pool = gu_local_pool();
	GuExn* err = gu_exn(tmp_pool);

	GuEnum* cts = 
		pgf_lzr_concretize(concr,
		                   gu_variant_from_ptr((void*) get_ref(env, jexpr)),
		                   err,
		                   tmp_pool);
	if (!gu_ok(err)) {
		if (gu_exn_caught(err, PgfExn)) {
			GuString msg = (GuString) gu_exn_caught_data(err);
			throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
		} else {
			throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be concretized");
		}
		gu_pool_free(tmp_pool);
		return NULL;
	}

	PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool);
	if (gu_variant_is_null(ctree)) {
		gu_pool_free(tmp_pool);
		return NULL;
	}

	size_t n_lins;
	GuString* labels;
	pgf_lzr_get_table(concr, ctree, &n_lins, &labels);

	for (size_t lin_idx = 0; lin_idx < n_lins; lin_idx++) {
		GuStringBuf* sbuf = gu_string_buf(tmp_pool);
		GuOut* out = gu_string_buf_out(sbuf);

		pgf_lzr_linearize_simple(concr, ctree, lin_idx, out, err, tmp_pool);

		jstring jstr = NULL;
		if (gu_ok(err)) {
			GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
			jstr = gu2j_string(env, str);
		} else {
			gu_exn_clear(err);
		}

		jstring jname = gu2j_string(env, labels[lin_idx]);

		(*env)->CallObjectMethod(env, table, put_method, jname, jstr);

		(*env)->DeleteLocalRef(env, jname);
		
		if (jstr != NULL)
			(*env)->DeleteLocalRef(env, jstr);
	}
	
	gu_pool_free(tmp_pool);

	return table;
}