a top-level API for parsing in the C runtime

This commit is contained in:
kr.angelov
2012-12-13 14:44:33 +00:00
parent 60942c440a
commit 2ba632dc9f
10 changed files with 138 additions and 91 deletions

View File

@@ -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:

View File

@@ -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;
}