jsgf_t * jsgf_parse_string(const char *string, jsgf_t * parent) { yyscan_t yyscanner; jsgf_t *jsgf; int yyrv; YY_BUFFER_STATE buf; yylex_init(&yyscanner); buf = yy_scan_string(string, yyscanner); jsgf = jsgf_grammar_new(parent); if (!parent) jsgf_set_search_path(jsgf, NULL); yyrv = yyparse(yyscanner, jsgf); if (yyrv != 0) { E_ERROR("Failed to parse JSGF grammar from input string\n"); jsgf_grammar_free(jsgf); yy_delete_buffer(buf, yyscanner); yylex_destroy(yyscanner); return NULL; } yy_delete_buffer(buf, yyscanner); yylex_destroy(yyscanner); return jsgf; }
jsgf_t * jsgf_parse_file(const char *filename, jsgf_t *parent) { yyscan_t yyscanner; jsgf_t *jsgf; int yyrv; FILE *in = NULL; yylex_init(&yyscanner); if (filename == NULL) { yyset_in(stdin, yyscanner); } else { in = fopen(filename, "r"); if (in == NULL) { E_ERROR_SYSTEM("Failed to open %s for parsing", filename); return NULL; } yyset_in(in, yyscanner); } jsgf = jsgf_grammar_new(parent); yyrv = yyparse(yyscanner, jsgf); if (yyrv != 0) { E_ERROR("Failed to parse JSGF grammar from '%s'\n", filename ? filename : "(stdin)"); jsgf_grammar_free(jsgf); yylex_destroy(yyscanner); return NULL; } if (in) fclose(in); yylex_destroy(yyscanner); return jsgf; }
int create_graph(FILE* input) { yylex_destroy(); __click_modules = alloc_resource(sizeof(List)); __click_symbol_table = alloc_resource(sizeof(Map)); __click_edges = alloc_resource(sizeof(List)); init_list(__click_modules); init_map(__click_symbol_table); init_list(__click_edges); yyset_in(input); int ret = yyparse(); yylex_destroy(); int k; for(k=0; k<__click_modules->len; k++) { Node* node = (Node*)__click_modules->list[k]; assert(node->type == Node_MODULE); Module* module = (Module*)node->payload; printf("%s::\t", module->name->payload); ArgList* arglist = (ArgList*)module->args->payload; int j; for(j=0; j<arglist->len; j++) { Node* eacharg = (Node*)arglist->list[j]; if(eacharg == NULL) { printf("NULL "); } else if(eacharg->type == Node_VALUE_LIST) { ValueList* valuelist = (ValueList*)eacharg->payload; int i; for(i=0; i<valuelist->len; i++) { Node* eachval = (Node*)valuelist->list[i]; printf("%s ", eachval->payload); } printf(", "); } else { assert(0); } } printf("\n"); } clear_resource(); __click_modules = 0; __click_symbol_table = 0; __click_edges = 0; return ret; }
int main(int argc, char *argv[]) { /* instantiate and init calculator data */ CalcData calc_data; if (yylex_init(&calc_data.scaninfo)) { fprintf(stderr, "Fail initing scanner\n"); return(EXIT_FAILURE); } calc_data.symt = new SymbolTable; /* set up input stream */ yyset_in(stdin, calc_data.scaninfo); /* call the parser */ printf("> "); fflush(stdout); int ret; while ((ret=yyparse(&calc_data)) != 0) { printf(" = %g\n", calc_data.ast->eval()); printf("> "); fflush(stdout); } printf("\n"); /* clean up and quit */ yylex_destroy(calc_data.scaninfo); delete calc_data.ast; delete calc_data.symt; return 0; }
void fsqlf_format_bytes(fsqlf_kwmap_t kwmap, const char *bytes_in, int len, char **bytes_out ) { struct fsqlf_formatter_state f_state; FSQLF_stack_init(&f_state.lexstate_stack, sizeof(int)); FSQLF_stack_init(&f_state.sub_openings, sizeof(pair)); FSQLF_tokque_init(&f_state.tqueue); f_state.currindent = 0; f_state.left_p = 0; f_state.right_p = 0; f_state.kwall = kwmap; f_state.bout.len_used = 0; f_state.bout.len_alloc = len * 1.5; f_state.bout.buffer = malloc(f_state.bout.len_alloc); yyscan_t scanner; yylex_init(&scanner); yy_scan_bytes(bytes_in, len, scanner); yyset_extra(&f_state, scanner); yylex(scanner); *bytes_out = f_state.bout.buffer; yylex_destroy(scanner); }
/** * @function runCommand * * @abstract Parses and executes a command given a NULL terminated string. * @discussion Parses the command and adds it to the history if it is * nontrivial. * * @param cmd_buffer Pointer to NULL terminated string containing * the user input. * @result Returns nothing, but runs a command, most likely printing to * the terminal. */ void runCommand(char* cmd_buffer) { Command *command = NULL; yyscan_t scanner; YY_BUFFER_STATE state; // Check if there is anything in the line. It is useless to store // a history of empty lines. If it is not empty, then add it to our // history. Also don't want to record line rerun calls. if ((cmd_buffer[0] != 0) && (cmd_buffer[0] != '!')) { add_history(cmd_buffer); } state = yy_scan_string(cmd_buffer); if (yyparse(&command, scanner)) { printf("Error in parsing command.\n"); deleteCommand(command); return; } executeCommand(command, -1); deleteCommand(command); // Wait on all child processes while (wait(NULL) > 0) {} yy_delete_buffer(state); yylex_destroy(); return; }
int source_parse_file(source *src, const char *filename, const char *modname) { YY_BUFFER_STATE bufstate; int r; if (NULL == (yyin = fopen(filename,"r"))) { perror(filename); return -1; } yyfilename = filename; yyfileno = add_parsedfile(src,filename); ////yyfileno is the index in the src->parsedfiles yylloc.first_line = 1; bufstate = yy_create_buffer(yyin,YY_BUF_SIZE); yy_switch_to_buffer(bufstate); parse_src = src; parse_modname = modname ? modname : ""; r = yyparse(); yy_delete_buffer(bufstate); #if HAVE_YYLEX_DESTROY yylex_destroy(); #endif if (yyin) fclose(yyin); return r; }
ltl_formula * getAST (const char *input) { ltl_formula *formula; yyscan_t scanner; YY_BUFFER_STATE state; if (yylex_init (&scanner)) { // couldn't initialize return NULL; } state = yy_scan_string (input, scanner); if (yyparse (&formula, scanner)) { // error parsing return NULL; } yy_delete_buffer (state, scanner); yylex_destroy (scanner); return formula; }
int main( int argc, const char *argv[] ) { void *scanner; FILE *input; if( argc >= 2 ) input = fopen( argv[1], "r" ); else input = stdin; if( input == NULL ) { perror( "cannot open input file" ); exit( 1 ); } if( argc >= 3 ) { mkparent( argv[2] ); FILE *res = freopen( argv[2], "w", stdout ); if( res == NULL ) { perror( "cannot open output file" ); exit( 1 ); } } yylex_init( &scanner ); yyset_in( input, scanner ); yyparse( scanner ); yylex_destroy( scanner ); exit(0); }
range* do_range_expand(range_request* rr, const char* text) { yyscan_t scanner; struct range_extras extra; int result; if (text == NULL) { range* r = range_new(rr); range_request_set(rr, r); return r; } current_rr = rr; extra.rr = rr; yylex_init(&scanner); yyset_extra(&extra, scanner); yy_scan_string(text, scanner); result = yyparse(scanner); yylex_destroy(scanner); current_rr = NULL; if (result != 0) { range* r = range_new(rr); range_request_warn(rr, "parsing [%s]", text); range_request_set(rr, r); return r; } range_request_set(rr, range_evaluate(rr, extra.theast)); return range_request_results(rr); }
bool InteractiveParser::parse(FILE *file, const std::string *filename, parser::ProgramNode &programStmts, stmtFunction callback) const { yydebug = DEBUG_PARSER; yyscan_t scanner; yylex_init(&scanner); bool interactive = (stdin == file); #ifndef OS_WINDOWS interactive = interactive && (isatty(fileno(file)) == 1); #endif yyset_debug(DEBUG_LEXER, scanner); LexerContext lexerCtx(&programStmts, filename, interactive, callback); yylex_init_extra(&lexerCtx, &scanner); lexerCtx.scanner = scanner; lexerCtx.interactive = interactive; #ifndef OS_WINDOWS if(stdin == file){ rl_instream = stdin; } #endif if(interactive){ std::cout << "Welcome to the sugar interactive shell" << std::endl; std::cout << "Use 'quit' or 'exit' to quit this shell" << std::endl; } yy_push_state(lex_state_newline, scanner); yyset_in( file, scanner ); bool succeed = (yyparse(&lexerCtx) == 0); yylex_destroy(scanner); fclose(file); return succeed && (interactive || !lexerCtx.hasError); }
int read_config(const char * file) { FILE * f; f = fopen(file, "r"); if (f == NULL) { fprintf(stderr, "%s: can't open config file \"%s\": %s\n", progname, file, strerror(errno)); return -1; } lineno = 1; infile = file; yyin = f; yyparse(); #ifdef HAVE_YYLEX_DESTROY /* reset lexer and free any allocated memory */ yylex_destroy(); #endif fclose(f); return 0; }
vcx_ast_module* vcx_parser_parse_file(const char* filename) { vcx_parse_ctx ctx; int r; FILE* stream; stream = fopen(filename, "r"); if (stream == NULL) { vcx_util_errorf("could not open source file: %s", filename); return NULL; } yylex_init(&ctx.scanner); yyset_extra(&ctx, ctx.scanner); yyset_in(stream, ctx.scanner); r = yyparse(&ctx); yylex_destroy(ctx.scanner); fclose(stream); if (r == 0) { return ctx.root; } else { vcx_ast_module_delete(ctx.root); return NULL; } }
Chunk *make(char *file) { // TODO - make non-global filename = file; FILE *fp = fopen(file, "r"); if (!fp) { printf("Could not open input file '%s'.\n", file); exit(EXIT_FAILURE); } yyin = fp; yyparse(); yylex_destroy(); fclose(fp); resolve(program); if (numerrors > 0) { exit(EXIT_FAILURE); } Chunk *chunk = compile(program); free_expr(program); return chunk; }
/* Compile a string */ void compile_string(compiler_type *comp_void, char *str, bool include_baselib) { compiler_core_type *compiler = (compiler_core_type *)comp_void; ins_stream_type *baselib = 0; /* TODO: should be gc root */ char path[PATH_MAX]; /* Actually parse the input stream. */ yylex_init_extra(compiler, &(compiler->scanner)); yy_scan_string(str, compiler->scanner); /* TODO: Need a better way to handle GC than leaking */ gc_protect(compiler->gc); /* Inject include for base library */ if (include_baselib) { strcpy(path, compiler->home); strcat(path, "/lib/baselib.scm"); STREAM_NEW(baselib, string, path); setup_include(compiler, baselib); } parse_internal(compiler, compiler->scanner); gc_unprotect(compiler->gc); yylex_destroy(compiler->scanner); }
void ESDLcompiler::Process() { CriticalBlock block(m_critSect); yyInitESDLGlobals(this); Owned<IException> parseException; yyrestart (yyin); try { yyparse(); } catch(IException* e) { parseException.setown(e); } if (nCommentStartLine > -1) { char tempBuf[256]; sprintf(tempBuf, "The comment that started at line %d is not ended yet", nCommentStartLine); yyerror(tempBuf); } if(!parseException) write_esxdl(); fclose(yyin); if (gOutfile > 0) close (gOutfile); yyCleanupESDLGlobals(); yylex_destroy(); if(parseException) throw parseException.getLink(); }
static void cleanup(void) { ir_destroy(); node_destroy(root); stack_destroy(&stack); yylex_destroy(); }
int main(int argc, char const *argv[]) { if(argc!=2){ printf("USAGE: %s FILE\n", argv[0]); return EXIT_FAILURE; } FILE* f = fopen(argv[1], "r"); if(!f){ perror("Failed to open file: "); return EXIT_FAILURE; } atexit(clean_matrix); yyin = f; yyparse(); fclose(f); yylex_destroy(); build_starts(); link_matrix(); struct MATRIX *matrix = get_matrix(); int result = forkx(matrix); free(matrix); if(!result){ wait_for_children(); } return EXIT_SUCCESS; }
/* Does the action for the -test command */ int do_test (int argc, const char ** argv) { int i, val, size = 0; char * buffer; for (i=0; i<argc; i++) size += strlen(argv[i]) + 1; buffer = (char*) malloc(sizeof(char) * size); strcpy(buffer, argv[0]); for (i=1; i<argc; i++) { strcat(buffer, " "); strcat(buffer, argv[i]); } yy_scan_string(buffer); yylex(); yyparse (); if (result == NULL) return; val = eval(result); /* printf("%d\n", val); */ free_ast(result); free(buffer); yylex_destroy(); return val; }
pt_node *buildAST(const char *expr) { extern int yyparse(pt_node **, yyscan_t); FILE *file; yyscan_t scanner; pt_node *node = NULL; YY_BUFFER_STATE state; if(yylex_init(&scanner)) { fprintf(stderr, "Failed to initialize parser.\n"); exit(-1); } file = fopen(expr, "r"); if(file == NULL) { perror("fopen"); exit(-1); } state = yy_create_buffer(file, 1024, scanner); yy_switch_to_buffer(state, scanner); if(yyparse(&node, scanner)) { fprintf(stderr, "Error parsing input.\n"); exit(-1); } yy_delete_buffer(state, scanner); yylex_destroy(scanner); return node; }
int como_ast_create(const char *filename) { ast_node* statements; yyscan_t scanner; YY_BUFFER_STATE state; char* text; text = file_get_contents(filename); if(!text) { printf("file '%s' not found\n", filename); return 1; } if(yylex_init(&scanner)) { como_error_noreturn("yylex_init returned NULL"); } state = yy_scan_string(text, scanner); if(yyparse(&statements, scanner)) { como_error_noreturn("yyparse returned NULL"); } yy_delete_buffer(state, scanner); yylex_destroy(scanner); como_compile_ast(statements, filename); return 0; }
int main ( int argc, char **argv ) { outputStage = 12; options ( argc, argv ); /* In order to only scan the tokens we call yylex() directly */ if ( outputStage == 1 ) { do { } while ( yylex() ); // "Token files" exit(0); } /* The parser calls yylex(), match the rules and builds the abstract syntax tree */ // "BuildTree files" yyparse(); if ( outputStage == 2 ) { exit(0); // Exit if we are only printing this stages debug information. "BuildTree files" } /* Print the abstract syntax tree */ if ( outputStage == 3 ) { node_print ( stderr, root, 0 ); // "Tree files" exit(0); } destroy_subtree ( stderr, root ); yylex_destroy(); // Free internal data structures of the scanner. exit ( EXIT_SUCCESS ); }
ModuleNode *getAST(const char *expr) { ModuleNode *expression; yyscan_t scanner; YY_BUFFER_STATE state; if (yylex_init(&scanner)) { // couldn't initialize return NULL; } state = yy_scan_string(expr, scanner); yydebug = 0; if (yyparse(&expression, scanner)) { // error parsing return NULL; } yy_delete_buffer(state, scanner); yylex_destroy(scanner); return expression; }
void Equation::save(QXmlStreamWriter &s) { s.writeStartElement(staticTypeTag); // Reparse the equation, then write it back out in text so that we can update // any vectors or scalars that had name changes, but we don't get affected by // the optimizer if (!_equation.isEmpty()) { QMutexLocker ml(&Equations::mutex()); yylex_destroy(); yy_scan_string(parseableEquation()); ParsedEquation = 0L; int rc = yyparse(store()); Equations::Node *en = static_cast<Equations::Node*>(ParsedEquation); if (rc == 0 && en) { if (!en->takeVectors(VectorsUsed)) { Debug::self()->log(i18n("Equation [%1] failed to find its vectors when saving. Resulting Kst file may have issues.").arg(_equation), Debug::Warning); } QString etext = en->text(); s.writeAttribute("expression", etext); } delete en; ParsedEquation = 0L; } if (_xInVector) { s.writeAttribute("xvector", _xInVector->Name()); } if (_doInterp) { s.writeAttribute("interpolate", "true"); } saveNameInfo(s, VNUM|ENUM|XNUM); s.writeEndElement(); }
const QString Equation::reparsedEquation() const { QString etext; if (!_equation.isEmpty()) { if (!Equations::mutex().tryLock()) { qDebug() << "Don't reparse equation while it is being reparsed..."; return (_equation); } yylex_destroy(); yy_scan_string(parseableEquation()); ParsedEquation = 0L; int rc = yyparse(store()); Equations::Node *en = static_cast<Equations::Node*>(ParsedEquation); if (rc == 0 && en) { if (!en->takeVectors(VectorsUsed)) { Debug::self()->log(i18n("Equation [%1] failed to find its vectors when reparsing.").arg(_equation), Debug::Warning); } etext = en->text(); } delete en; ParsedEquation = 0L; Equations::mutex().unlock(); //etext.replace("atanx(", "atan2("); //etext.replace("atanxd(", "atan2d("); } return (readableEquation(etext)); }
int parse_css(FILE *fh) { // Set up the scanner yyscan_t scanner; yylex_init(&scanner); yyset_in(fh, scanner); // Set up the parser void *css_parser = ParseAlloc(malloc); int lexCode; do { lexCode = yylex(scanner); printf("Got %i\n", lexCode); Parse(css_parser, lexCode, yyget_text(scanner)); } while (lexCode > 0); // Finish the job. Parse(css_parser, 0, NULL); if (-1 == lexCode) { fprintf(stderr, "The scanner encountered an error.\n"); return 0; } // Cleanup the scanner and parser yylex_destroy(scanner); ParseFree(css_parser, free); return 1; }
void parse_context_delete (parse_context *self) { yylex_destroy (self->scanner); pt_node_unref_ornull (self->unit); mem_free (self, sizeof *self); }
int main(int argc, char **argv) { var_init(); yyparse(); var_destroy(); yylex_destroy(); return (0); }
void val_parse_forall(char *s, val_callback_t callback) { yyscan_t scanner; if (yylex_init(&scanner)) die("yylex_init error"); YY_BUFFER_STATE buf = s ? yy_scan_string(s, scanner) : 0; if (yyparse(scanner, callback)) die("parse error"); yy_delete_buffer(buf, scanner); yylex_destroy(scanner); }
int main(int ac, char** av) { instr_t* imem; int imem_size; int yyparse(); int yylex_destroy(); progname = av[0]; output = stdout; init_func(); init_instr(); init_stmt(); init_sim(); params(ac, av); if (yyin == NULL) { warning("no input file"); exit(0); } if (cfg_fp == NULL) yyparse(); fclose(yyin); yylex_destroy(); opt(); if (cfg_fp != NULL) exit(0); imem = stmt2instr(&imem_size); free_func(); deinit_symtab(); deinit_sym(); set_dmem_size(100000); set_imem(imem, imem_size); set_regs(32); run(); print_stats(); free_sim(); if (output != NULL) fclose(output); if (yyin != NULL) fclose(yyin); return 0; }