summaryrefslogtreecommitdiff
path: root/src/runtime/c/utils
diff options
context:
space:
mode:
authorkr.angelov <kr.angelov@gmail.com>2012-12-12 11:25:58 +0000
committerkr.angelov <kr.angelov@gmail.com>2012-12-12 11:25:58 +0000
commitaa13090b66d7ee1ffc68ad242c6419a83cf364d9 (patch)
treed36bacbc5f4e0990aae925da31ddeaf2a465a003 /src/runtime/c/utils
parent5e091d2e3dc428daa1d4b0d8df6e7b613adc22a9 (diff)
started an official API to the C runtime
Diffstat (limited to 'src/runtime/c/utils')
-rw-r--r--src/runtime/c/utils/pgf-chunk.c25
-rw-r--r--src/runtime/c/utils/pgf-print.c17
-rw-r--r--src/runtime/c/utils/pgf-translate.c30
-rw-r--r--src/runtime/c/utils/pgf2yaml.c11
4 files changed, 36 insertions, 47 deletions
diff --git a/src/runtime/c/utils/pgf-chunk.c b/src/runtime/c/utils/pgf-chunk.c
index c4d0d0b3f..d5e203368 100644
--- a/src/runtime/c/utils/pgf-chunk.c
+++ b/src/runtime/c/utils/pgf-chunk.c
@@ -8,7 +8,6 @@
#include <gu/enum.h>
#include <gu/file.h>
#include <pgf/pgf.h>
-#include <pgf/data.h>
#include <pgf/parser.h>
#include <pgf/lexer.h>
#include <pgf/literals.h>
@@ -39,38 +38,28 @@ int main(int argc, char* argv[]) {
GuString from_lang = gu_str_string(argv[3], pool);
- FILE* infile = fopen(filename, "r");
- if (infile == NULL) {
- fprintf(stderr, "couldn't open %s\n", filename);
- status = EXIT_FAILURE;
- goto fail;
- }
-
- // Create an input stream from the input file
- GuIn* in = gu_file_in(infile, 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(in, pool, err);
+ 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_read;
+ goto fail;
}
- if (!pgf_load_meta_child_probs(pgf, "../../../treebanks/PennTreebank/ParseEngAbs3.probs", pool)) {
+ pgf_load_meta_child_probs(pgf, "../../../treebanks/PennTreebank/ParseEngAbs3.probs", pool, err);
+ if (!gu_ok(err)) {
fprintf(stderr, "Loading meta child probs failed\n");
status = EXIT_FAILURE;
- goto fail_read;
+ goto fail;
}
// Look up the source and destination concrete categories
- PgfConcr* from_concr =
- gu_map_get(pgf->concretes, &from_lang, PgfConcr*);
+ PgfConcr* from_concr = pgf_get_language(pgf, from_lang);
if (!from_concr) {
fprintf(stderr, "Unknown language\n");
status = EXIT_FAILURE;
@@ -152,8 +141,6 @@ int main(int argc, char* argv[]) {
ppool = NULL;
}
fail_concr:
-fail_read:
- fclose(infile);
fail:
gu_pool_free(pool);
return status;
diff --git a/src/runtime/c/utils/pgf-print.c b/src/runtime/c/utils/pgf-print.c
index dd5789020..12d81a7c5 100644
--- a/src/runtime/c/utils/pgf-print.c
+++ b/src/runtime/c/utils/pgf-print.c
@@ -5,13 +5,24 @@
#include <gu/file.h>
#include <gu/utf8.h>
+#include <locale.h>
+#include <stdlib.h>
+
GU_DECLARE_TYPE(PgfAbstr, struct);
-int main(void) {
+int main(int argc, char* argv[]) {
+ // Set the character locale, so we can produce proper output.
+ setlocale(LC_CTYPE, "");
+
+ if (argc != 1) {
+ fprintf(stderr, "usage: %s pgf\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+ char* filename = argv[1];
+
GuPool* pool = gu_new_pool();
GuExn* err = gu_exn(NULL, type, pool);
- GuIn* in = gu_file_in(stdin, pool);
- PgfPGF* pgf = pgf_read(in, pool, err);
+ PgfPGF* pgf = pgf_read(filename, pool, err);
int status = 0;
if (!gu_ok(err)) {
fprintf(stderr, "Reading PGF failed\n");
diff --git a/src/runtime/c/utils/pgf-translate.c b/src/runtime/c/utils/pgf-translate.c
index 31127a37f..2cf1dcfe7 100644
--- a/src/runtime/c/utils/pgf-translate.c
+++ b/src/runtime/c/utils/pgf-translate.c
@@ -5,7 +5,6 @@
#include <gu/enum.h>
#include <gu/file.h>
#include <pgf/pgf.h>
-#include <pgf/data.h>
#include <pgf/parser.h>
#include <pgf/lexer.h>
#include <pgf/literals.h>
@@ -53,7 +52,7 @@ int main(int argc, char* argv[]) {
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]);
+ fprintf(stderr, "usage: %s pgf cat from_lang to_lang\n", argv[0]);
status = EXIT_FAILURE;
goto fail;
}
@@ -64,40 +63,29 @@ int main(int argc, char* argv[]) {
GuString from_lang = gu_str_string(argv[3], pool);
GuString to_lang = gu_str_string(argv[4], pool);
- FILE* infile = fopen(filename, "r");
- if (infile == NULL) {
- fprintf(stderr, "couldn't open %s\n", filename);
- status = EXIT_FAILURE;
- goto fail;
- }
-
- // Create an input stream from the input file
- GuIn* in = gu_file_in(infile, 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(in, pool, err);
+ 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_read;
+ goto fail;
}
- if (!pgf_load_meta_child_probs(pgf, "../../../treebanks/PennTreebank/ParseEngAbs3.probs", pool)) {
+ pgf_load_meta_child_probs(pgf, "../../../treebanks/PennTreebank/ParseEngAbs3.probs", pool, err);
+ if (!gu_ok(err)) {
fprintf(stderr, "Loading meta child probs failed\n");
status = EXIT_FAILURE;
- goto fail_read;
+ goto fail;
}
// Look up the source and destination concrete categories
- PgfConcr* from_concr =
- gu_map_get(pgf->concretes, &from_lang, PgfConcr*);
- PgfConcr* to_concr =
- gu_map_get(pgf->concretes, &to_lang, PgfConcr*);
+ 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;
@@ -229,8 +217,6 @@ int main(int argc, char* argv[]) {
result = NULL;
}
fail_concr:
-fail_read:
- fclose(infile);
fail:
gu_pool_free(pool);
return status;
diff --git a/src/runtime/c/utils/pgf2yaml.c b/src/runtime/c/utils/pgf2yaml.c
index 32029aa75..0d32bf3e3 100644
--- a/src/runtime/c/utils/pgf2yaml.c
+++ b/src/runtime/c/utils/pgf2yaml.c
@@ -4,11 +4,16 @@
#include <gu/file.h>
#include <gu/utf8.h>
-int main(void) {
+int main(int argc, char* argv[]) {
+ if (argc != 1) {
+ fprintf(stderr, "usage: %s pgf\n", argv[0]);
+ return 1;
+ }
+ char* filename = argv[1];
+
GuPool* pool = gu_new_pool();
GuExn* err = gu_exn(NULL, type, pool);
- GuIn* in = gu_file_in(stdin, pool);
- PgfPGF* pgf = pgf_read(in, pool, err);
+ PgfPGF* pgf = pgf_read(filename, pool, err);
int status = 0;
if (!gu_ok(err)) {
fprintf(stderr, "Reading PGF failed\n");