static file_contents get_contents(char *filename) { FILE* file = fopen(filename, "r"); char *contents = read_file(file); fclose(file); parse_source source = start_parse(contents, filename); file_contents parsed = parse(source); return parsed; }
/* ------------------------------------------------------------------------ @NAME : bt_parse_entry_s() @INPUT : entry_text - string containing the entire entry to parse, or NULL meaning we're done, please cleanup options - standard btparse options bitmap line - current line number (if that makes any sense) -- passed to the parser to set zzline, so that lexical and syntax errors are properly localized @OUTPUT : *top - newly-allocated AST for the entry (or NULL if entry_text was NULL, ie. at EOF) @RETURNS : 1 with *top set to AST for entry on successful read/parse 1 with *top==NULL if entry_text was NULL, ie. at EOF 0 if any serious errors seen in input (*top is still set to the AST, but only for as much of the input as we were able to parse) (A "serious" error is a lexical or syntax error; "trivial" errors such as warnings and notifications count as "success" for the purposes of this function's return value.) @DESCRIPTION: Parses a BibTeX entry contained in a string. @GLOBALS : @CALLS : ANTLR @CREATED : 1997/01/18, GPW (from code in bt_parse_entry()) @MODIFIED : -------------------------------------------------------------------------- */ AST * bt_parse_entry_s (char * entry_text, char * filename, int line, ushort options, boolean * status) { AST * entry_ast = NULL; static int * err_counts = NULL; if (options & BTO_STRINGMASK) /* any string options set? */ { usage_error ("bt_parse_entry_s: illegal options " "(string options not allowed"); } InputFilename = filename; err_counts = bt_get_error_counts (err_counts); if (entry_text == NULL) /* signal to clean up */ { finish_parse (&err_counts); if (status) *status = TRUE; return NULL; } zzast_sp = ZZAST_STACKSIZE; /* workaround apparent pccts bug */ start_parse (NULL, entry_text, line); entry (&entry_ast); /* enter the parser */ ++zzasp; /* why is this done? */ if (entry_ast == NULL) /* can happen with very bad input */ { if (status) *status = FALSE; return entry_ast; } #if DEBUG dump_ast ("bt_parse_entry_s: single entry, after parsing:\n", entry_ast); #endif bt_postprocess_entry (entry_ast, StringOptions[entry_ast->metatype] | options); #if DEBUG dump_ast ("bt_parse_entry_s: single entry, after post-processing:\n", entry_ast); #endif if (status) *status = parse_status (err_counts); return entry_ast; } /* bt_parse_entry_s () */
int main(int argc, char **argv) { SAX_parser *p; p = SAX_init(stdin); reg_characters_handler(p, chars); reg_start_document_handler(p, begin); reg_end_document_handler(p, end); reg_start_element_handler(p, start_elem); reg_end_element_handler(p, end_elem); reg_pi_handler(p, proc_inst); reg_error_handler(p, error); start_parse(p); fclose(p->source); saxfree(p); return EXIT_SUCCESS; }
/** * Hlavna funkcia programu * @param argc int pocet argumentov pri volani programu * @param argv[] char* pole s argumentami programu * @return int kod chyby * */ int main(int argc, char* argv[]) { FILE *source_file; // kontrola vstupnych argumentov if(argc!=2) { fprintf(stderr, "Neni korektne zadany vstupni soubor jako jedinny argument programu."); return EXIT_FAILURE; } // otevreni zdrojoveho souboru if((source_file = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Subor se nepodarilo otevrit"); return EXIT_FAILURE; } int error = ERR_CODE_OK; // inicializacia spravy pamate memory_init(); // zoznam instrukcii TInst_list inst_list; inst_init_list(&inst_list); error = start_parse(source_file, &inst_list); // zavreni souboru, uvolneni pameti fclose(source_file); memory_free_all(); return error; }
int repl() { #ifdef HAVE_READLINE history_truncate_file(history_filename(), 1000); read_history(history_filename()); #endif // HAVE_READLINE // Eingabeschleife als REPL(read-eval-print loop) -> Eingabe, Verarbeitung, Ausgabe do { char *user_input = NULL; ////////////////////// // read ////////////////////// #ifdef HAVE_READLINE user_input = readline("?: "); if (!user_input) break; strim(user_input); if (*user_input) add_history(user_input); #else // Eingabepuffer als "String" mit Maximallänge user_input = calloc(LINE_BUFFER_SIZE, sizeof(char)); printf("?: "); // Bei end-of-file oder Lesefehler gibt fgets NULL zurück // und das Programm soll beendet werden (analog zur Shell) // // break ist eigentlich nicht schön (=nicht im Struktogramm // abbildbar ;) ), dient aber hier der Klarheit, da einige // if entfallen können if (!fgets(user_input, LINE_BUFFER_SIZE, stdin)) break; strim(user_input); #endif // HAVE_READLINE ////////////////////// // eval ////////////////////// // Abbruch durch User? if (strcmp(user_input, "\\quit") == 0) break; // 1. Parsen parse_context *p_ctx = start_parse(user_input); // 2. Baum traversieren if (parse(p_ctx) == 0) { eval_context *e_ctx = eval(p_ctx->ast_root); if (e_ctx == NULL) fprintf(stderr, "Fatal evaluation error.\n"); if (e_ctx->success) { ////////////////////// // print ////////////////////// printf("-> %.50Lg\n", e_ctx->result); } else { print_eval_errors(e_ctx); } end_eval(e_ctx); } else { print_parse_errors(p_ctx); } // Aufräumen end_parse(p_ctx); free(user_input); } while (1); // loop.... #ifdef HAVE_READLINE write_history(history_filename()); #endif // HAVE_READLINE printf("End.\n"); return 0; }
/* ------------------------------------------------------------------------ @NAME : bt_parse_entry() @INPUT : infile - file to read next entry from options - standard btparse options bitmap @OUTPUT : *top - AST for the entry, or NULL if no entries left in file @RETURNS : same as bt_parse_entry_s() @DESCRIPTION: Starts (or continues) parsing from a file. @GLOBALS : @CALLS : @CREATED : Jan 1997, GPW @MODIFIED : -------------------------------------------------------------------------- */ AST * bt_parse_entry (FILE * infile, char * filename, ushort options, boolean * status) { AST * entry_ast = NULL; static int * err_counts = NULL; static FILE * prev_file = NULL; if (prev_file != NULL && infile != prev_file) { usage_error ("bt_parse_entry: you can't interleave calls " "across different files"); } if (options & BTO_STRINGMASK) /* any string options set? */ { usage_error ("bt_parse_entry: illegal options " "(string options not allowed)"); } InputFilename = filename; err_counts = bt_get_error_counts (err_counts); if (feof (infile)) { if (prev_file != NULL) /* haven't already done the cleanup */ { prev_file = NULL; finish_parse (&err_counts); } else { usage_warning ("bt_parse_entry: second attempt to read past eof"); } if (status) *status = TRUE; return NULL; } /* * Here we do some nasty poking about the innards of PCCTS in order to * enter the parser multiple times on the same input stream. This code * comes from expanding the macro invokation: * * ANTLR (entry (top), infile); * * When LL_K, ZZINF_LOOK, and DEMAND_LOOK are all undefined, this * ultimately expands to * * zzbufsize = ZZLEXBUFSIZE; * { * static char zztoktext[ZZLEXBUFSIZE]; * zzlextext = zztoktext; * zzrdstream (f); * zzgettok(); * } * entry (top); * ++zzasp; * * (I'm expanding hte zzenterANTLR, zzleaveANTLR, and zzPrimateLookAhead * macros, but leaving ZZLEXBUFSIZE -- a simple constant -- alone.) * * There are two problems with this: 1) zztoktext is a statically * allocated buffer, and when it overflows we just ignore further * characters that should belong to that lexeme; and 2) zzrdstream() and * zzgettok() are called every time we enter the parser, which means the * token left over from the previous entry will be discarded when we * parse entries 2 .. N. * * I handle the static buffer problem with alloc_lex_buffer() and * realloc_lex_buffer() (in lex_auxiliary.c), and by rewriting the ZZCOPY * macro to call realloc_lex_buffer() when overflow is detected. * * I handle the extra token-read by hanging on to a static file * pointer, prev_file, between calls to bt_parse_entry() -- when * the program starts it is NULL, and we reset it to NULL on * finishing a file. Thus, any call that is the first on a given * file will allocate the lexical buffer and read the first token; * thereafter, we skip those steps, and free the buffer on reaching * end-of-file. Currently, this method precludes interleaving * calls to bt_parse_entry() on different files -- perhaps I could * fix this with the zz{save,restore}_{antlr,dlg}_state() * functions? */ zzast_sp = ZZAST_STACKSIZE; /* workaround apparent pccts bug */ #if defined(LL_K) || defined(ZZINF_LOOK) || defined(DEMAND_LOOK) # error One of LL_K, ZZINF_LOOK, or DEMAND_LOOK was defined #endif if (prev_file == NULL) /* only read from input stream if */ { /* starting afresh with a file */ start_parse (infile, NULL, 0); prev_file = infile; } assert (prev_file == infile); entry (&entry_ast); /* enter the parser */ ++zzasp; /* why is this done? */ if (entry_ast == NULL) /* can happen with very bad input */ { if (status) *status = FALSE; return entry_ast; } #if DEBUG dump_ast ("bt_parse_entry(): single entry, after parsing:\n", entry_ast); #endif bt_postprocess_entry (entry_ast, StringOptions[entry_ast->metatype] | options); #if DEBUG dump_ast ("bt_parse_entry(): single entry, after post-processing:\n", entry_ast); #endif if (status) *status = parse_status (err_counts); return entry_ast; } /* bt_parse_entry() */