Example #1
0
File: parser.c Project: danfis/SVT
static int svtParserParseInit(svt_parser_t *parser)
{
    if (yylex_init_extra(&parser->yylval, &parser->scanner) != 0){
        perror("Can't initialize scanner");
        return -1;
    }
    yyset_in(parser->input, parser->scanner);

    parser->cur_tok = -1;
    parser->cur_obj = NULL;

    return 0;
}
Example #2
0
oconfig_item_t *oconfig_parse_fh (FILE *fh)
{
  int status;
  oconfig_item_t *ret;

  char file[10];

  yyset_in (fh);

  if (NULL == c_file) {
    int status;

    status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));

    if ((status < 0) || (status >= sizeof (file))) {
      c_file = "<unknown>";
    }
    else {
      file[sizeof (file) - 1] = '\0';
      c_file = file;
    }
  }

  status = yyparse ();
  if (status != 0)
  {
    fprintf (stderr, "yyparse returned error #%i\n", status);
    return (NULL);
  }

  c_file = NULL;

  ret = ci_root;
  ci_root = NULL;
  yyset_in ((FILE *) 0);

  return (ret);
} /* oconfig_item_t *oconfig_parse_fh */
Example #3
0
int Compiler_compileFile(Context* context, FILE* file) {
    void* scanner;
    Node* root = 0;

    yylex_init(&scanner);
    yyset_in(file, scanner);

    int res = yyparse(scanner, &root);
    yylex_destroy(scanner);
    if (res) return -1;

    res = Compiler_compileRootNode(context, root);
    if (root) Node_delete(root);
    return res;
}
Example #4
0
int parser_parse(FILE* in, FILE* out) {
    int result = 0;
    yyscan_t scanner;
    if (yylex_init(&scanner)) {
        result = 1;
    }
    else {
        yyset_in(in, scanner);
        yyset_out(out, scanner);
    
        result = yyparse(scanner, yyparse_callback);
        yylex_destroy(scanner);
    }

    return (result);
}
Example #5
0
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;
}
Example #6
0
File: main.c Project: crab2313/m1
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;
}
Example #7
0
/* Parse a file */
object_type *parse(interp_core_type *interp, FILE *in) {
    reset(interp);
    
    if(!interp->scanner) {
	create_parser(interp);
    }
    
    yyset_in(in, peek_scanner);
    
    if(parse_internal(interp, peek_scanner)) {
	return 0;
    }

    TRACE("\n")
    
    return interp->added;
}
Example #8
0
void iiInit(FILE* file)
{
	yyset_in(file);

	gint i;
	for(i=0; i < NUM_TRAC_STATEMENTS; i++) {
		statementsSucceed[i] = 0;
		statementsFail[i] = 0;
	}
	
	fileList = NULL;
	dirList = NULL;
	
	timing_init();
	ast_init();
	var_init();
	//groups_init();
}
Example #9
0
std::unique_ptr<AstTranslationUnit> ParserDriver::parse(const std::string& filename, FILE* in,
        SymbolTable& symbolTable, ErrorReport& errorReport, DebugReport& debugReport) {
    translationUnit = std::make_unique<AstTranslationUnit>(
            std::unique_ptr<AstProgram>(new AstProgram()), symbolTable, errorReport, debugReport);
    yyscan_t scanner;
    scanner_data data;
    data.yyfilename = filename.c_str();
    yylex_init_extra(&data, &scanner);
    yyset_in(in, scanner);

    yy::parser parser(*this, scanner);
    parser.set_debug_level(trace_parsing);
    parser.parse();

    yylex_destroy(scanner);

    translationUnit->getProgram()->finishParsing();

    return std::move(translationUnit);
}
Example #10
0
__attribute__ ((constructor)) void colorit_load(void) {
  int new_out;

  if (fork_count++)
    return;

  if (pipe(color_pipe) != 0) {
    perror("pipe creation failed");
    return;
  }

  child_pid = fork();
  if (!child_pid) {
    FILE *fd_in = NULL;
    colorit_data data = { NULL, NULL};

    close(color_pipe[1]);

    if (yylex_init_extra(&data, &data.scaninfo)) {
        perror("init alloc failed");
        exit(1);
    }
    yyset_in(fd_in = fdopen(color_pipe[0], "r"), data.scaninfo);
    data.out = stdout;

    if (colorit_init(&data, LIBCOLORIT_TERM) != 0) {
      fprintf(stderr, "Can't init %s\n", LIBCOLORIT_TERM);
      exit(1);
    }

    yyparse(&data);
    fclose(fd_in);
    exit(0);
  }

  unsetenv("LD_PRELOAD");
  close(color_pipe[0]);
  dup2(color_pipe[1], 1);
  dup2(color_pipe[1], 2);
}
Example #11
0
File: main.c Project: Gwaltrip/CLP
int main(int argc, char *argv[]) {

	/* process arguments */
	handle_opts(argc, argv);	

	/* open files */
	vlog("[plppp] opening files\n");
	FILE_INPUT = fopen(S_FILE_INPUT,"r");
	if (FILE_INPUT == NULL) {
		err("[plppp] cannot open input file: %s\n", S_FILE_INPUT);
	}
	FILE_OUTPUT = fopen(S_FILE_OUTPUT,"w");
	if (FILE_OUTPUT == NULL) {
		err("[plppp] cannot open output file: %s\n", S_FILE_OUTPUT);
	}
	yyset_in(FILE_INPUT);

	/* create our first define */
	defines = install_define(defines,"WIRA","brengsek");

	log("[plppp] starting preprocessor\n");
	yyparse();

	/* print the defines */
        if (S_DEFINE_OUTPUT != NULL) {
                vlog("[plppp] printing defines\n");
		FILE_DEFINE = fopen(S_DEFINE_OUTPUT, "w");
                print_defines(FILE_DEFINE, defines);
        }

	fprintf(FILE_OUTPUT, "%s", program);

	vlog("[plppp] closing files\n");
	fclose(FILE_INPUT);
	fclose(FILE_OUTPUT);

	log("[plppp] done\n");
	return 0;
}
Example #12
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);
}
Example #13
0
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_;
}
Example #14
0
void parse_file(FILE* f) {
  yyscan_t scanner;
  lexinfo charpos = {0, 0};
  int token_type;
  const char* token;

  yylex_init_extra(&charpos, &scanner);
  yyset_in(f, scanner);
  do {
    token_type = yylex(scanner);
    token = yyget_text(scanner);
    if(random() < 50000) {
      if(!new_query) printf(" ");
      new_query = 0;
      printf("%s", token);
    }
    if(!new_query && (random() < 50000)) {
      printf("\n");
      new_query = 1;
    }
  } while(token_type != TOK_DONE);
  yylex_destroy(scanner);
}
Example #15
0
static void
rev_list_file(rev_file *file, analysis_t *out, cvs_master *cm, rev_master *rm) 
{
    struct stat	buf;
    yyscan_t scanner;
    FILE *in;
    cvs_file *cvs;

    in = fopen(file->name, "r");
    if (!in) {
	perror(file->name);
	++err;
	return;
    }
    if (stat(file->name, &buf) == -1) {
	fatal_system_error("%s", file->name);
    }

    cvs = xcalloc(1, sizeof(cvs_file), __func__);
    cvs->gen.master_name = file->name;
    cvs->gen.expand = EXPANDUNSPEC;
    cvs->export_name = file->rectified;
    cvs->mode = buf.st_mode;
    cvs->verbose = verbose;

    yylex_init(&scanner);
    yyset_in(in, scanner);
    yyparse(scanner, cvs);
    yylex_destroy(scanner);

    fclose(in);
    cvs_master_digest(cvs, cm, rm);
    out->total_revisions = cvs->nversions;
    out->skew_vulnerable = cvs->skew_vulnerable;
    out->generator = cvs->gen;
    cvs_file_free(cvs);
}
Example #16
0
int main(int argc, char** argv)
{

  int print_t = 0;
  int print_v = 0;
  int print_f = 0;
  int i;
  int rc;

  g_type = (char*) malloc(10);
  symboltable tracking_table = create_symboltable();
  flat_symtab var_table = create_flat_symtab();
  func_table function_table = create_functable();

  
  if (argc < 2){
    printf("usage: scan filename\n");
    return -1;
  }

  FILE* in = fopen(argv[1], "r");
  if (in == NULL){
    printf("file not found\n");
    return -1;
  }

  for(i = 2; i < argc; i++){
    if(strcmp(argv[i], "-t") == 0){
      print_t = 1;
    }else if (strcmp(argv[i], "-v") == 0){
      print_v = 1;
    }else if (strcmp(argv[i], "-f") == 0){
      print_f = 1;
    }else {
      printf("acceptable options are -t, -v and -f\n");
    }
  }

  // set input source
  yyset_in(in);

  // parse input
  rc = yyparse();

  if (rc == 0){
    printf("succeeded parsing input.\n");
  }
  else{
    printf("--- error --- failed parsing input: error at line %d, abort.\n", lineNumber);
    return -1;
  }
  
  // build symbol table
  build_symtab(tracking_table, var_table, function_table, root);
  if (sym_error != 0){
    printf("--- error --- errors found building symboltable, abort. \n");
    return -1;
  }

  // type check
  rc = type_check(var_table, function_table, root);
  if (rc != 0){
    printf("--- error --- errors found in type checking, abort.\n");
    // return -1;
  }

  // print data structures
   if(print_t == 1)
     print_ast(root, 0);	
   if(print_v == 1)
     print_flat_table(var_table);
   if(print_f == 1)
     print_func_table(function_table);
 
  return 0;
} 
Example #17
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;
}
Example #18
0
File: main.c Project: Gwaltrip/CLP
int main(int argc, char *argv[]) {

	/* process arguments */
	handle_opts(argc, argv);	

	/* open files */
	vlog("[plpcc] opening files\n");
	FILE_INPUT = fopen(S_FILE_INPUT,"r");
	if (FILE_INPUT == NULL) {
		err("[plpcc] cannot open input file: %s\n", S_FILE_INPUT);
	}
	FILE_OUTPUT = fopen(S_FILE_OUTPUT,"w");
	if (FILE_OUTPUT == NULL) {
		err("[plpcc] cannot open output file: %s\n", S_FILE_OUTPUT);
	}
	yyset_in(FILE_INPUT);

	if (S_SYMBOL_OUTPUT != NULL) {
		SYMBOL_OUTPUT = fopen(S_SYMBOL_OUTPUT, "w");
		if (SYMBOL_OUTPUT == NULL) {
			err("[plpcc] cannot open symbol table output file: %s\n", S_SYMBOL_OUTPUT);
		}
	}

	if (S_PARSE_OUTPUT != NULL) {
		PARSE_OUTPUT = fopen(S_PARSE_OUTPUT, "w");
		if (PARSE_OUTPUT == NULL) {
			err("[plpcc] cannot open parse tree output file: %s\n", S_PARSE_OUTPUT);
		}
	}
	
	if (S_GRAPH_OUTPUT != NULL) {
		GRAPH_OUTPUT = fopen(S_GRAPH_OUTPUT, "w");
		if (GRAPH_OUTPUT == NULL) {
			err("[plpcc] cannot open parse tree graph output file: %s\n", S_GRAPH_OUTPUT);
		}
	}

	/* grab the lines from the source for error handling and annotation */
	build_lines(S_FILE_INPUT);

	/* create an empty symbol table */
	sym = new_symbol_table(NULL);

	log("[plpcc] starting frontend\n");
	yyparse();

	/* print the parse tree */
	if (PARSE_OUTPUT != NULL) {
		vlog("[plpcc] printing parse tree\n");
		print_tree(parse_tree_head, PARSE_OUTPUT, 0);
	}

	/* print the parse tree graph formatted for Graphviz*/
	if (GRAPH_OUTPUT != NULL) {
		vlog("[plpcc] printing parse tree graph\n");
		print_tree_graph(parse_tree_head, GRAPH_OUTPUT, S_FILE_INPUT);
		
		/* close output file*/
		fclose(GRAPH_OUTPUT);
		
		/* Run Graphviz command to generate PNG of parse tree */
		S_GRAPH_COMMAND = malloc(sizeof(char) * (strlen(S_FILE_OUTPUT) * 2 + 22));
		sprintf(S_GRAPH_COMMAND, "dot -Tpng %s.dot > %s.png", S_FILE_OUTPUT, S_FILE_OUTPUT);
		system(S_GRAPH_COMMAND);
	}
	
	/* print the symbol table */
	if (SYMBOL_OUTPUT != NULL) {
		vlog("[plpcc] printing symbol table\n");
		print_symbols(sym, SYMBOL_OUTPUT, 0);
		vlog("[plpcc] printing activation records\n");
		print_frames(sym, SYMBOL_OUTPUT);
	}

	/* call the backend to compile the parse tree, starting from the head */
	if (NO_COMPILE == 0) {
		handle(parse_tree_head);
		fprintf(FILE_OUTPUT, "%s", program);
	}

	vlog("[plpcc] closing files\n");
	fclose(FILE_INPUT);
	fclose(FILE_OUTPUT);

	log("[plpcc] done\n");
	return 0;
}
Example #19
0
ParseInfo* click_parse_configuration(FILE* input, ClickAllocator alloc, ClickLinker link, void* obj)
{
	ParseInfo* ret = 0;
	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 parse_result = yyparse();
	yylex_destroy();

	if(parse_result == 0)
	{
		int no_modules = __click_modules->len;
		ret = malloc(sizeof(ParseInfo));
		ret->all_modules = malloc(sizeof(void*)*no_modules);
		ret->root_modules = malloc(sizeof(void*)*no_modules);
		ret->leaf_modules = malloc(sizeof(void*)*no_modules);
		ret->no_modules = no_modules;
		ret->no_root = 0;
		ret->no_leaf = 0;

		int k;
		for(k=0; k<no_modules; k++)
		{
			Node* node = (Node*)__click_modules->list[k];
			assert(node->type == Node_MODULE);
			Module* module = (Module*)node->payload;
			char* module_name = module->name->payload;
			assert(module->args->type == Node_ARG_LIST);
			ArgList* arglist = (ArgList*)module->args->payload;
			char** argv = alloc_resource(sizeof(char*) * (arglist->len + 1));
			argv[arglist->len] = 0;
			int argc = arglist->len;
			int j;
			for(j=0; j<arglist->len; j++)
			{
				Node* eacharg = (Node*)arglist->list[j];
				char* value_str = 0;
				if(eacharg == NULL)
				{
				}
				else if(eacharg->type == Node_VALUE_LIST)
				{
					ValueList* valuelist = (ValueList*)eacharg->payload;
					value_str = vlist_to_string(valuelist);
				}
				else
				{
					assert(0);
				}
				argv[j] = value_str;
			}

			void* user_module = 0;
			if(alloc)
				user_module = alloc(k, module_name, argc, argv, obj);
			ret->all_modules[k] = user_module;
		}

		int* inward = alloc_resource(sizeof(int)*no_modules);
		int* outward = alloc_resource(sizeof(int)*no_modules);

		for(k=0; k<no_modules; k++)
		{
			inward[k] = 0;
			outward[k] = 0;
		}

		for(k=0; k<__click_edges->len; k++)
		{
			Chain* edge = (Chain*)__click_edges->list[k];
			assert(edge->head->type == Node_MODULE);
			assert(edge->tail->type == Node_MODULE);

			Module* head = (Module*)edge->head->payload;
			Module* tail = (Module*)edge->tail->payload;

			if(link)
				link(ret->all_modules[head->index], edge->head_outport, ret->all_modules[tail->index], edge->tail_inport, obj);

			inward[tail->index]++;
			outward[head->index]++;
		}

		for(k=0; k<no_modules; k++)
		{
			if(inward[k] == 0)
			{
				ret->root_modules[ret->no_root++] = ret->all_modules[k];
			}
			if(outward[k] == 0)
			{
				ret->leaf_modules[ret->no_leaf++] = ret->all_modules[k];
			}
		}
	}
	clear_resource();
	__click_modules = 0;
	__click_symbol_table = 0;
	__click_edges = 0;

	return ret;
}
Example #20
0
/*Thread task function for reentrant scanner*/
void *lex_thread_task(void *arg)
{  
  lex_thread_arg *ar = (lex_thread_arg*) arg;
  
  FILE * f = fopen(ar->file_name, "r");
  if (f == NULL) {
    DEBUG_STDOUT_PRINT("ERROR> Lexing thread %d could not open input file. Aborting.\n", ar->id);
    exit(1);
  }

  lex_token *flex_token;
  int8_t end_of_chunk = 0;
  yyscan_t scanner;   //reentrant flex instance data
  int32_t flex_return_code;
  token_node *token_builder = NULL;
  token_node_stack stack;
  sem_value_stack stack_char;
  sem_value_stack stack_int;

  uint32_t alloc_size = 0, realloc_size = 0;

  uint32_t chunk_length = ar->cut_point_dx - ar->cut_point_sx;

  par_compute_alloc_realloc_size((ar->cut_point_dx - ar->cut_point_sx), &alloc_size, &realloc_size);
  DEBUG_STDOUT_PRINT("LEXER %d > alloc_size %d, realloc_size %d\n", ar->id, alloc_size, realloc_size)

  /*Initialization flex_token*/
  flex_token = (lex_token*) malloc(sizeof(lex_token));
  if (flex_token == NULL) {
    DEBUG_STDOUT_PRINT("ERROR> could not complete malloc flex_token. Aborting.\n");
    exit(1);
  }
  flex_token->chunk_length = chunk_length;
  flex_token->num_chars = 0;
  flex_token->chunk_ended = 0;
  //Initialize stack for semantic values.
  //Since the possible semantic values are only char or int, we can allocate two different stacks, one for each type, avoiding to waste memory.
  init_sem_value_stack(&stack_char, alloc_size, 0);
  init_sem_value_stack(&stack_int, alloc_size, 1);
  flex_token->stack_char = &stack_char;
  flex_token->stack_int = &stack_int;
  flex_token->realloc_size = realloc_size;

  fseek(f, ar->cut_point_sx, SEEK_SET);

  if(yylex_init_extra(flex_token, &scanner))
  {
    DEBUG_STDOUT_PRINT("ERROR> yylex_init_extra failed.\n")
    exit(1);
  }

  yyset_in(f, scanner);
  
  ar->list_begin = NULL;
  init_token_node_stack(&(stack), alloc_size);

  flex_return_code = yylex(scanner); 

  /*The procedure to find cut points cannot cut a single token in two parts.*/
  while(!end_of_chunk && flex_return_code != __END_OF_CHUNK && flex_return_code != __END_OF_FILE)
  {
    if(flex_return_code == __LEX_CORRECT) {
      //append a token
      par_append_token_node(flex_token->token, flex_token->semantic_value, &token_builder, &(ar->list_begin), &stack, realloc_size);
      ar->lex_token_list_length++;
    }
    else {
      //flex_return_code is __ERROR)
      DEBUG_STDOUT_PRINT("Lexing thread %d scanned erroneous input. Abort.\n", ar->id)
      ar->result = 1; /*Signal error by returning result!=0.*/
      fclose(yyget_in(scanner));
      yylex_destroy(scanner);
      fclose(f);
      pthread_exit(NULL);
    }

    if (flex_token->chunk_ended)
      end_of_chunk = 1;
    else {
      //Continue to scan the chunk
      flex_return_code = yylex(scanner); 
    }
  }

  ar->list_end = token_builder;

  fclose(yyget_in(scanner));
  yylex_destroy(scanner);

  fclose(f);

  pthread_exit(NULL);

}
Example #21
0
char *compilestyle(const char *source, int len, int *len_out) {
#if defined(YYDEBUG) && YYDEBUG
	extern int yydebug;
	yydebug = 1;
#endif

	CompilerState _state={0,}, *state = &_state;
	StrReader reader = {source, 0, len};
	YYLTYPE yylloc = {0,};

	yyscan_t scanner;

	if (!errorprint) {
		set_errorprint((void*) stderr, (errorprint_t)fprintf);
	}

	
	state->ma_r = memarena_new(4096);
	yylex_init(&scanner);
	yyset_in((FILE*)&reader, scanner);

	printf("compiling AST tree. . .\n");

	if (yyparse(state, &yylloc, scanner)==0) {
		astnode_print(state->result, 0, stdout, fprintf);
	} else { //error
		errorprint(errorarg, "Parse error!\n");
		return NULL;
	}

	state->index_globals =  memarena_malloc(state->ma_r, sizeof(*state->globals));
	state->global_indices = memarena_malloc(state->ma_r, sizeof(*state->globals));
	state->globals = memarena_malloc(state->ma_r, sizeof(*state->globals));
	state->scope = memarena_malloc(state->ma_r, sizeof(*state->scope));
	state->builtin_funcs = memarena_malloc(state->ma_r, sizeof(*state->builtin_funcs));
	state->funcs = memarena_malloc(state->ma_r, sizeof(*state->funcs));
	
	strhash_init(state->index_globals);
	strhash_init(state->global_indices);
	strhash_init(state->globals);
	strhash_init(state->scope);
	strhash_init(state->builtin_funcs);
	strhash_init(state->funcs);

	state->error = cerror;

	//set longjump
	state->jmpbuf = memarena_malloc(state->ma_r, sizeof(jmp_buf));
	setjmp(*state->jmpbuf);

	char *buf = NULL;
	int len2 = 0;

	//see if an exception happened
	if (state->haderror) {
		errorprint(errorarg, "compilation failed; aborting\n");
	} else {
		setup_builtins(state);
		transform_ast(state);
		buf = emit_bytecode(state, &len2);
	}

	strhash_release(state->index_globals);
	strhash_release(state->global_indices);
	strhash_release(state->globals);
	strhash_release(state->scope);
	strhash_release(state->builtin_funcs);
	strhash_release(state->funcs);

	yylex_destroy(scanner);
	memarena_free(state->ma_r);

	*len_out = len2;
	return buf;
}