summaryrefslogtreecommitdiff
path: root/src/runtime/c/utils
diff options
context:
space:
mode:
authorkr.angelov <kr.angelov@gmail.com>2012-12-13 14:44:33 +0000
committerkr.angelov <kr.angelov@gmail.com>2012-12-13 14:44:33 +0000
commit14e721dda9a38762695ac5435c24818265629b02 (patch)
tree9dc11a3db929482f9b4f6cb8bf59978c9288d818 /src/runtime/c/utils
parent68249a11d2daf6a7d639110e218418af84fa75d2 (diff)
a top-level API for parsing in the C runtime
Diffstat (limited to 'src/runtime/c/utils')
-rw-r--r--src/runtime/c/utils/pgf-chunk.c39
-rw-r--r--src/runtime/c/utils/pgf-translate.c41
2 files changed, 16 insertions, 64 deletions
diff --git a/src/runtime/c/utils/pgf-chunk.c b/src/runtime/c/utils/pgf-chunk.c
index d5e203368..575534cd3 100644
--- a/src/runtime/c/utils/pgf-chunk.c
+++ b/src/runtime/c/utils/pgf-chunk.c
@@ -70,14 +70,6 @@ int main(int argc, char* argv[]) {
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;
@@ -103,42 +95,15 @@ int main(int argc, char* argv[]) {
// sentence, so our memory usage doesn't increase over time.
ppool = gu_new_pool();
- // Begin parsing a sentence of the specified category
- PgfParseState* state =
- pgf_parser_init_state(from_concr, cat, 0, ppool);
- if (state == NULL) {
- fprintf(stderr, "Couldn't begin parsing\n");
- status = EXIT_FAILURE;
- break;
- }
-
GuReader *rdr =
gu_string_reader(gu_str_string(line, ppool), ppool);
PgfLexer *lexer =
pgf_new_lexer(rdr, ppool);
- // Tokenization
- GuExn* lex_err = gu_new_exn(NULL, gu_kind(type), ppool);
- PgfToken tok = pgf_lexer_next_token(lexer, lex_err, ppool);
- while (!gu_exn_is_raised(lex_err)) {
- // feed the token to get a new parse state
- state = pgf_parser_next_state(state, tok, ppool);
- if (!state) {
- gu_puts("Unexpected token: \"", wtr, err);
- gu_string_write(tok, wtr, err);
- gu_puts("\"\n", wtr, err);
- goto fail_parse;
- }
-
- tok = pgf_lexer_next_token(lexer, lex_err, ppool);
- }
-
- pgf_parse_print_chunks(state);
- continue;
- fail_parse:
+ pgf_print_chunks(from_concr, cat, lexer, ppool);
+
// Free all resources allocated during parsing and linearization
gu_pool_free(ppool);
- ppool = NULL;
}
fail_concr:
fail:
diff --git a/src/runtime/c/utils/pgf-translate.c b/src/runtime/c/utils/pgf-translate.c
index 2cf1dcfe7..03b3635f0 100644
--- a/src/runtime/c/utils/pgf-translate.c
+++ b/src/runtime/c/utils/pgf-translate.c
@@ -9,7 +9,6 @@
#include <pgf/lexer.h>
#include <pgf/literals.h>
#include <pgf/linearize.h>
-#include <pgf/expr.h>
#include <pgf/edsl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -160,40 +159,29 @@ int main(int argc, char* argv[]) {
// sentence, so our memory usage doesn't increase over time.
ppool = gu_new_pool();
- clock_t start = clock();
-
- // Begin parsing a sentence of the specified category
- PgfParseState* state =
- pgf_parser_init_state(from_concr, cat, 0, ppool);
- if (state == NULL) {
- fprintf(stderr, "Couldn't begin parsing\n");
- status = EXIT_FAILURE;
- break;
- }
-
GuReader *rdr =
gu_string_reader(gu_str_string(line, ppool), ppool);
PgfLexer *lexer =
pgf_new_lexer(rdr, ppool);
- // Tokenization
- GuExn* lex_err = gu_new_exn(NULL, gu_kind(type), ppool);
- PgfToken tok = pgf_lexer_next_token(lexer, lex_err, ppool);
- while (!gu_exn_is_raised(lex_err)) {
- // feed the token to get a new parse state
- state = pgf_parser_next_state(state, tok, ppool);
- if (!state) {
+ 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;
}
-
- tok = pgf_lexer_next_token(lexer, lex_err, ppool);
- }
- // Now begin enumerating the resulting syntax trees
- result = pgf_parse_result(state, ppool);
+ goto fail_parse;
+ }
PgfExprProb* ep = gu_next(result, PgfExprProb*, ppool);
@@ -201,8 +189,7 @@ int main(int argc, char* argv[]) {
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.
+ // The enumerator will return null at the end of the results.
if (ep == NULL) {
goto fail_parse;
}