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; } }
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); }
lexer::lexer (char const *name) : impl (new pimpl (name)) { yylex_init (&yyscanner); yyset_extra (this, yyscanner); yyset_in (NULL, yyscanner); }
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); }
parse_context * parse_context_new (void) { parse_context *self = mem_alloc (sizeof *self); yylex_init (&self->scanner); yyset_extra (self, self->scanner); self->unit = NULL; return self; }
static PyObject * _psp_module_parsestring(PyObject *self, PyObject *argv) { PyObject *code; PyObject *str; PyObject *latin = NULL; char *c_str = NULL; yyscan_t scanner; psp_parser_t *parser; YY_BUFFER_STATE bs; if (!PyArg_ParseTuple(argv, "S", &str)) { return NULL; } Py_BEGIN_ALLOW_THREADS parser = psp_parser_init(); yylex_init(&scanner); yyset_extra(parser, scanner); if (PyUnicode_Check(str)) { latin = PyUnicode_AsLatin1String(str); if (latin) c_str = PyBytes_AsString(latin); } else if (PyBytes_Check(str)) c_str = PyBytes_AsString(str); if (!c_str) c_str = "UNICODE ERROR"; bs = yy_scan_string(c_str, scanner); yylex(scanner); Py_XDECREF(latin); /* yy_delete_buffer(bs, scanner); */ yylex_destroy(scanner); psp_string_0(&parser->pycode); Py_END_ALLOW_THREADS if (parser->pycode.blob) { code = MpBytesOrUnicode_FromString(parser->pycode.blob); } else { code = MpBytesOrUnicode_FromString(""); } psp_parser_cleanup(parser); return code; }
unsigned char parse_and_report(const char *filename) { yyscan_t scanner; FILE *f; unsigned char err = 0; if(filename == NULL || strcmp(filename, "-") == 0) { f = stdin; filename = "stdin"; } else { f = fopen(filename, "r"); if(!f) { perror(filename); return 1; } } yylex_init(&scanner); yyset_in(f, scanner); yyset_extra((void *) filename, scanner); #ifdef DEBUG yyset_debug(1, scanner); #endif if(xml_output) printf("\t<file name=\"%s\">\n", filename); switch(yyparse(scanner)) { case 0: if(!xml_output) printf("%s: valid JSON\n", filename); break; case 1: if(!xml_output) printf("%s: parsing failed\n", filename); err = 1; break; case 2: fprintf(stderr, "Out of memory\n"); exit(1); break; } if(xml_output) printf("\t</file>\n"); yylex_destroy(scanner); if(f != stdin) fclose(f); return err; }
int main(int argc, char *argv[]) { FILE *fp; yyscan_t yyscanner; M1_compiler comp; if (argc <= 1) { fprintf(stderr, "Usage: m1 <file>\n"); exit(EXIT_FAILURE); } fp = fopen(argv[1], "r"); if (fp == NULL) { fprintf(stderr, "Could not open file\n"); exit(EXIT_FAILURE); } /* set up compiler */ init_compiler(&comp); /* set up lexer and parser */ yylex_init(&yyscanner); yyset_extra(&comp, yyscanner); yyset_in(fp, yyscanner); comp.yyscanner = yyscanner; /* yyscanner has a pointer to comp, and vice versa. */ yyparse(yyscanner, &comp); fprintf(stderr, "parsing done\n"); if (comp.errors == 0) { assert(intstack_isempty(comp.breakstack) != 0); assert(intstack_isempty(comp.continuestack) != 0); check(&comp, comp.ast); /* need to finish */ //if (comp.errors == 0) { fprintf(stderr, "generating code...\n"); gencode(&comp, comp.ast); } } fclose(fp); fprintf(stderr, "compilation done\n"); return 0; }
void fsqlf_format_file(struct fsqlf_kw_conf *kwall, FILE *fin, FILE *fout) { 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 = kwall; f_state.bout = (struct FSQLF_out_buffer){NULL, 0, 0}; yyscan_t scanner; yylex_init(&scanner); yyset_in(fin, scanner); yyset_out(fout, scanner); yyset_extra(&f_state, scanner); yylex(scanner); yylex_destroy(scanner); }
ParserStatus FidlParser::parse(const std::string& file, bool debug) { clear(); //std::cout << "Opening file '" << file << "' for parsing\n"; bool ok = open_and_buffer_file(&open_files_, file); if (!ok) { return first_error_; } assert(open_files_.size() == 1); //std::cout << "Initializing lexer.\n"; yylex_init(&lexer_); if (debug) { yyset_debug(1, lexer_); yydebug = 1; } yyset_extra(this, lexer_); yyset_in(current_file_ptr(), lexer_); //std::cout << "Starting parsing of '" << current_file() << "'.\n"; root_node_ = new ast::Root(); yyparse(this, lexer_); //std::cout << "Done parsing '" << current_file() << "'.\n"; yylex_destroy(lexer_); return first_error_; }
vcx_ast_module* vcx_parser_parse_string(const char* source) { vcx_parse_ctx ctx; int r; memset(&ctx, 0, sizeof(ctx)); yylex_init(&ctx.scanner); yyset_extra(&ctx, ctx.scanner); // yyset_in(stdin, ctx.scanner); yy_scan_string(source, ctx.scanner); r = yyparse(&ctx); yylex_destroy(ctx.scanner); if (r == 0) { return ctx.root; } else { vcx_ast_module_delete(ctx.root); return NULL; } }
int main(int argc, char *argv[]) { struct ritual_instance scheme; struct parse_context my; struct rflo_filehandle *flo_stdout; ritual_global_initialize(); ritual_select_instance( &scheme ); // Life's easy when you're single-threaded.. int failure = ritual_initialize_instance( &scheme ); if( failure ) { fprintf(stderr, "fatal error: unable to initialize Scheme instance\n" ); return 1; } if( RITUAL_TOP_LEVEL_ERROR( &scheme ) ) { fprintf(stderr, "fatal error: %s\n", scheme.error->reason ); ritual_deinitialize_instance( &scheme ); return 1; } flo_stdout = rflo_filehandle_create( &scheme, stdout ); ritual_define_native_proc( &scheme, scheme.root, "eq?", rnp_eqp ); ritual_define_native_proc( &scheme, scheme.root, "ritual-print-diagnostics", rnp_ritual_print_diagnostics ); ritual_define_envproc( &scheme, scheme.root, renvp_ritual_typename, "ritual-typename", "object", 0 ); ritual_define_envproc( &scheme, scheme.root, renvp_ritual_typenames, "ritual-typenames", RSENT_REST, "rest", 0 ); ritual_define_native_proc( &scheme, scheme.root, "ritual-trace", rnp_ritual_trace ); pctx_init( &my, &scheme ); yylex_init( &my.scanner ); yyset_extra( &my, my.scanner ); while( 1 ) { char data[1024]; fputs( ">>> ", stdout ); fflush( stdout ); void* fgrv = fgets( data, sizeof data, stdin ); if( !fgrv ) { puts(""); break; } int len = strlen( data ); if( data[len-1] != '\n' ) { puts( "error: line too long" ); } else { data[len-1] = '\0'; if( !strlen(data) ) { continue; } if( RITUAL_INTERACTIVE_LEVEL_ERROR( &scheme ) ) { fprintf( stderr, "error: %s\n", scheme.error->reason ); } else { YY_BUFFER_STATE buffer = yy_scan_string( data, my.scanner ); yyparse( &my ); yy_delete_buffer( buffer, my.scanner ); while( pctx_has_more( &my ) ) { ritual_object_t * object = pctx_pop( &my ); object = ritual_eval( &scheme, scheme.root, object ); fputs( "-> ", stdout ); ritual_print( &scheme, &flo_stdout->flo, object ); puts( "" ); } } } } rflo_filehandle_destroy( &scheme, flo_stdout ); yylex_destroy( my.scanner ); pctx_destroy( &my ); ritual_deinitialize_instance( &scheme ); ritual_global_deinitialize(); return 0; }
int main(int argc, char *argv[]) { struct ritual_instance scheme; struct parse_context my; struct rflo_filehandle *flo_stdout; ritual_global_initialize(); ritual_select_instance( &scheme ); // Life's easy when you're single-threaded.. int failure = ritual_initialize_instance( &scheme ); if( failure ) { fprintf(stderr, "fatal error: unable to initialize Scheme instance\n" ); return 1; } if( RITUAL_TOP_LEVEL_ERROR( &scheme ) ) { fprintf(stderr, "fatal error: %s\n", scheme.error->reason ); ritual_deinitialize_instance( &scheme ); return 1; } flo_stdout = rflo_filehandle_create( &scheme, stdout ); pctx_init( &my, &scheme ); yylex_init( &my.scanner ); yyset_extra( &my, my.scanner ); struct rl3_global_context gctx_s; struct rl3_global_context *gctx = &gctx_s; rl3_initialize(gctx ); struct rl3_context rl3ctx; rl3_context_init( &rl3ctx, gctx, &scheme ); struct rl3_instr *program = 0; struct rl3_instr *jpt = program = rl3_mkinstr( &scheme, gctx->IS_NULL, 0, program ); program = rl3_mkinstr( &scheme, gctx->BRANCH, 0, program ); program = rl3_mkinstr( &scheme, gctx->SPLIT_PAIR, 0, program ); program = rl3_mkinstr( &scheme, gctx->PRINT, 0, program ); program = rl3_mkinstr( &scheme, gctx->JUMP, &jpt->header, program ); program = rl3_reverse( program ); struct rl3_instr *print_forwards = program; struct rl3_instr *dsc = rl3_mkinstr( &scheme, gctx->DISCARD, 0, 0); /* program = 0; program = rl3_mkinstr( &scheme, gctx->STORE, 0, program ); jpt = program = rl3_mkinstr( &scheme, gctx->SWAP, 0, program ); program = rl3_mkinstr( &scheme, gctx->IS_NULL, 0, program ); program = rl3_mkinstr( &scheme, gctx->BRANCH, &dsc->header, program ); program = rl3_mkinstr( &scheme, gctx->SPLIT_PAIR, 0, program ); program = rl3_mkinstr( &scheme, gctx->ROTATE, 0, program ); program = rl3_mkinstr( &scheme, gctx->SWAP, 0, program ); program = rl3_mkinstr( &scheme, gctx->CONS, 0, program ); program = rl3_mkinstr( &scheme, gctx->JUMP, &jpt->header, program ); program = rl3_reverse( program ); */ program = 0; struct rl3_instr **writep = &program; writep = rl3_seqinstr( &scheme, gctx->STORE, 0, writep, 0 ); writep = rl3_seqinstr( &scheme, gctx->SWAP, 0, writep, &jpt ); writep = rl3_seqinstr( &scheme, gctx->IS_NULL, 0, writep, 0 ); writep = rl3_seqinstr( &scheme, gctx->BRANCH, &dsc->header, writep, 0 ); writep = rl3_seqinstr( &scheme, gctx->SPLIT_PAIR, 0, writep, 0 ); writep = rl3_seqinstr( &scheme, gctx->ROTATE, 0, writep, 0 ); writep = rl3_seqinstr( &scheme, gctx->SWAP, 0, writep, 0 ); writep = rl3_seqinstr( &scheme, gctx->CONS, 0, writep, 0 ); writep = rl3_seqinstr( &scheme, gctx->JUMP, &jpt->header, writep, 0 ); while( 1 ) { char data[1024]; fputs( ">>> ", stdout ); fflush( stdout ); void* fgrv = fgets( data, sizeof data, stdin ); if( !fgrv ) { puts(""); break; } int len = strlen( data ); if( data[len-1] != '\n' ) { puts( "error: line too long" ); } else { data[len-1] = '\0'; if( !strlen(data) ) { continue; } if( RITUAL_INTERACTIVE_LEVEL_ERROR( &scheme ) ) { fprintf( stderr, "error: %s\n", scheme.error->reason ); } else { YY_BUFFER_STATE buffer = yy_scan_string( data, my.scanner ); yyparse( &my ); yy_delete_buffer( buffer, my.scanner ); while( pctx_has_more( &my ) ) { ritual_object_t * object = pctx_pop( &my ); ritual_list_push( &scheme, &rl3ctx.values, object ); ritual_list_push( &scheme, &rl3ctx.sequences, (void*) program ); fprintf(stderr, "\n" ); while( rl3_running( &rl3ctx ) ) { fprintf(stderr, "." ); rl3_run_one( &rl3ctx ); } fprintf(stderr, "\n" ); object = ritual_list_next( &scheme, &rl3ctx.values ); fputs( "-> ", stdout ); ritual_print( &scheme, &flo_stdout->flo, object ); puts( "" ); } } } } rflo_filehandle_destroy( &scheme, flo_stdout ); yylex_destroy( my.scanner ); pctx_destroy( &my ); rl3_deinitialize( gctx ); ritual_deinitialize_instance( &scheme ); ritual_global_deinitialize(); return 0; }
static PyObject * _psp_module_parse(PyObject *self, PyObject *argv) { PyObject *code; char *filename; char *dir = NULL; char *path; psp_parser_t *parser; yyscan_t scanner; FILE *f; if (!PyArg_ParseTuple(argv, "s|s", &filename, &dir)) { return NULL; } if (dir) { path = malloc(strlen(filename)+strlen(dir)+1); if (!path) return PyErr_NoMemory(); strcpy(path, dir); strcat(path, filename); } else { path = filename; } Py_BEGIN_ALLOW_THREADS f = fopen(path, "rb"); Py_END_ALLOW_THREADS if (f == NULL) { PyErr_SetFromErrnoWithFilename(PyExc_IOError, path); if (dir) free(path); return NULL; } if (dir) free(path); parser = psp_parser_init(); if (dir) parser->dir = dir; yylex_init(&scanner); yyset_in(f, scanner); yyset_extra(parser, scanner); yylex(scanner); yylex_destroy(scanner); fclose(f); psp_string_0(&parser->pycode); if (PyErr_Occurred()) { psp_parser_cleanup(parser); return NULL; } if (parser->pycode.blob) { code = MpBytesOrUnicode_FromString(parser->pycode.blob); } else { code = MpBytesOrUnicode_FromString(""); } psp_parser_cleanup(parser); return code; }