Exemplo n.º 1
0
json_t *json_loads(const char *string, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    string_data_t stream_data = {
        .data = string,
        .pos = 0
    };

    if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
        return NULL;

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}

json_t *json_loadf(FILE *input, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
        return NULL;

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}
Exemplo n.º 2
0
static int test_lex_sym_literal_1() {
	struct ymd_lex lex;
	struct ytoken token;
	int rv;
	lex_init(&lex, NULL, " a\nb\n\t__\t_1\t_1abcdef\n(ab)");
	ASSERT_SYM(a);
	ASSERT_SYM(b);
	lex_init(&lex, NULL, "4 * (b + 2) / a\n");
	ASSERT_DEC(4);
	ASSERT_TOKEN('*');
	ASSERT_TOKEN('(');
	ASSERT_SYM(b);
	ASSERT_TOKEN('+');
	return 0;
}
Exemplo n.º 3
0
static void cmdloop(void)
{
    int ret = FALSE;
    
    if (chdir("/tmp/")) {
	syslog(LOG_ERR, "Failed to chdir to /tmp/");
	ret = TRUE; /* exit immediately */
    }

    capabilities(sieved_out, sieved_saslconn, 0, 0, 0);

    /* initialize lexer */
    lex_init();

    while (ret != TRUE)
    {
	if (backend) {
	    /* create a pipe from client to backend */
	    bitpipe();

	    /* pipe has been closed */
	    return;
	}

	ret = parser(sieved_out, sieved_in);
    }

    sync_log_done();

    /* done */
    shut_down(0);
}
Exemplo n.º 4
0
int main(int argc, char **argv)
{
        int decres = 0;
        char binres[MAX_BIT_BIN + 1];
        Lex *lex = NULL;

        if (argc != 2) {
                usage(argv[0]);
        }

        lex = lex_init(argv[1]);
        if (lex == NULL) {
                show_error("syntax analizator not initialized");
        }

        decres = parse(lex);

        lex_free(lex);

        /* Convert result in binary format */
        dec2bin((unsigned int)abs(decres), binres);

        /* Print the result */
        if (decres < 0) {
                printf("\n\t%d\t\t\t-0x%x\t\t\t-0b%s\n\n", decres, decres, binres);
        } else {
                printf("\n\t%d\t\t\t0x%x\t\t\t0b%s\n\n", decres, decres, binres);
        }

        return 0;
}
Exemplo n.º 5
0
Arquivo: cc.c Projeto: Fedjmike/mini-c
int main (int argc, char** argv) {
    if (argc != 2) {
        puts("Usage: cc <file>");
        return 1;
    }

    output = fopen("a.s", "w");

    lex_init(argv[1], 256);

    sym_init(256);

    //No arrays? Fine! A 0xFFFFFF terminated string of null terminated strings will do.
    //A negative-terminated null-terminated strings string, if you will
    char* std_fns = "malloc\0calloc\0free\0atoi\0fopen\0fclose\0fgetc\0ungetc\0feof\0fputs\0fprintf\0puts\0printf\0"
                    "isalpha\0isdigit\0isalnum\0strlen\0strcmp\0strchr\0strcpy\0strdup\0\xFF\xFF\xFF\xFF";

    //Remember that mini-c is typeless, so this is both a byte read and a 4 byte read.
    //(char) 0xFF == -1, (int) 0xFFFFFF == -1
    while (std_fns[0] != -1) {
        new_fn(strdup(std_fns));
        std_fns = std_fns+strlen(std_fns)+1;
    }

    program();

    return errors != 0;
}
Exemplo n.º 6
0
void syntax_init() {
	static bool done = FALSE;
	if(done) return;
	lex_init();
	haneda_initialize();
	done = TRUE;
}
Exemplo n.º 7
0
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
{
    lex_t lex;
    const char *source;
    json_t *result;
    (void)flags; /* unused */

    if(lex_init(&lex, (get_func)fgetc, input))
        return NULL;

    if(input == stdin)
        source = "<stdin>";
    else
        source = "<stream>";

    jsonp_error_init(error, source);

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}
Exemplo n.º 8
0
json_t *json_load_callback(json_load_callback_t callback, void *arg, size_t flags, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    callback_data_t stream_data;

    memset(&stream_data, 0, sizeof(stream_data));
    stream_data.callback = callback;
    stream_data.arg = arg;

    jsonp_error_init(error, "<callback>");

    if (callback == NULL) {
        error_set(error, NULL, "wrong arguments");
        return NULL;
    }

    if(lex_init(&lex, (get_func)callback_get, &stream_data))
        return NULL;

    result = parse_json(&lex, flags, error);

    lex_close(&lex);
    return result;
}
Exemplo n.º 9
0
json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error)
{
    lex_t lex;
    json_t *result;
    buffer_data_t stream_data;

    jsonp_error_init(error, "<buffer>");

    if (buffer == NULL) {
        error_set(error, NULL, "wrong arguments");
        return NULL;
    }

    stream_data.data = buffer;
    stream_data.pos = 0;
    stream_data.len = buflen;

    if(lex_init(&lex, buffer_get, (void *)&stream_data))
        return NULL;

    result = parse_json(&lex, flags, error);

    lex_close(&lex);
    return result;
}
Exemplo n.º 10
0
json_t *json_loads(const char *string, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    string_data_t stream_data = {
        /*.data = */string,
        /*.pos = */0
    };

    if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
        return NULL;

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}
Exemplo n.º 11
0
Arquivo: main.c Projeto: thewhoo/ifj15
int main(int argc, char **argv)
{

    if (argc < 2)
        exit_error(E_INTERNAL);

    FILE *fp = fopen(argv[1], "r");
    if (fp == NULL)
        exit_error(E_INTERNAL);

    //gcInit();

    lex_init(fp);

    global_init();
    
    expr_init();

    parse();

    fclose(fp);
	
	expr_destroy();
	
    interpret();

    //gcDestroy();

    return 0;
}
Exemplo n.º 12
0
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
{
    lex_t lex;
    const char *source;
    json_t *result;

    if(input == stdin)
        source = "<stdin>";
    else
        source = "<stream>";

    jsonp_error_init(error, source);

    if (input == NULL) {
        error_set(error, NULL, "wrong arguments");
        return NULL;
    }

    if(lex_init(&lex, (get_func)fgetc, input))
        return NULL;

    result = parse_json(&lex, flags, error);

    lex_close(&lex);
    return result;
}
Exemplo n.º 13
0
json_t *json_loads(const char *string, size_t flags, json_error_t *error)
{
    lex_t lex;
    json_t *result;
    string_data_t stream_data = {string, 0};

    (void)flags; /* unused */

    if(lex_init(&lex, string_get, (void *)&stream_data))
        return NULL;

    jsonp_error_init(error, "<string>");

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}
Exemplo n.º 14
0
int __cdecl main( int argc, char *argv[] )
{
   lex ilex;
   plexitem pil;
   buf in;
   str fn;
   arr out;
   uint i;
   uint fout;

   gentee_init();
   printf("Start\n");
   str_init( &fn );

   str_copyzero( &fn, "gttbl.dat");
   fout = os_fileopen( &fn, FOP_CREATE );
   printf("Fout=%i %s\n", fout, str_ptr( &fn ));
   os_filewrite( fout, ( pubyte )&tbl_gt, 97 * sizeof( uint ));
   os_fileclose( ( pvoid )fout );

   str_copyzero( &fn, "gtdotbl.dat");
   fout = os_fileopen( &fn, FOP_CREATE );
   printf("Fout=%i %s\n", fout, str_ptr( &fn ));
   str_delete( &fn );
   os_filewrite( fout, ( pubyte )&tbl_gtdo, 81 * sizeof( uint ));
   os_fileclose( ( pvoid )fout );

   arr_init( &out, sizeof( lexitem ));
   buf_init( &in );
   buf_copyzero( &in, 
      "</r/&xfa;&#10;&#3;&#1&xfa; #ap/dfield( 'qwer ()ty' , \"my , name\" , qqq)#asdf.fgwsw/# se# &xaa;"
      "<2 qqq> </2><1 a2345=&xf0;> 223&</1><-qwe-rty->"
      "<mygt /asd = \"qwerty sese'\" qq21 = 'dedxd' 'esese;' aqaq=325623/>"
      "<a asdff /a>   <mygtdd><a /><-ooops-><ad />< qq</>"
      "xxx  </r/nm <_aa aqaqa /_aaaa /_a/_aa><a22222/ >"
      "<*abc ></abc><|*aaa = qqqq></aaa>ooops aaa</eee>\"\r\n</>");
//   buf_copyzero( &in, "<mygt > <aaa asdff>qqqq</>      </mygtdd>qq </> xxx  </r/nm <_aa aqaqa /_aaaa /_aa> <a22222/ /> </ >  ");
   printf("lex_init\n");
   lex_init( &ilex, (puint)&tbl_gtdo );
   printf("gentee_lex\n");
   gentee_lex( &in, &ilex, &out );
   if (arr_count(&ilex.state))
      printf("================= State=%x/%i \n", arr_getuint( &ilex.state,
          arr_count(&ilex.state) - 1 ), arr_count(&ilex.state));
   for ( i = 0; i < arr_count( &out ); i++ )
   {
      pil = ( plexitem )arr_ptr( &out, i );
      printf("ID=%x pos=%i len=%i \n", pil->type, pil->pos, pil->len,
             buf_ptr( &in ) + pil->pos );
   }
//   gentee_compile();
   lex_delete( &ilex );
   buf_delete( &in );
   arr_delete( &out );
   gentee_deinit();
   printf("OK\n");
   getch();
   return 0;
}
Exemplo n.º 15
0
static int test_lex_num_literal_2() {
	struct ymd_lex lex;
	struct ytoken token;
	int rv;
	lex_init(&lex, NULL, "0x0\t0x1\t0x0123456789abcdefABCDEF");
	ASSERT_HEX(0x0);
	ASSERT_HEX(0x1);
	ASSERT_HEX(0x0123456789abcdefABCDEF);
	ASSERT_TOKEN(EOS);
	return 0;
}
Exemplo n.º 16
0
void init(void) 
{
   exceptions_init();
   hide_cursor();
   vga_setcolor(0x07);
   pic_init();
   idt_write_table();
   kb_init();         //Init keyboard driver
   floppy_init();
   lex_init();
   printf("\nSystem booted.\n\n");
}
Exemplo n.º 17
0
bool Parser::Init(const char *file)
{
//	fileStack.clear();

	if(!lex_init(file))
	{
		return false;
	}

	t = gettok();

	return true;
}
Exemplo n.º 18
0
static int test_lex_keyword() {
	struct ymd_lex lex;
	struct ytoken token;
	int rv;
	lex_init(&lex, NULL, "nil true false and or func var");
	ASSERT_KWD(nil, NIL);
	ASSERT_KWD(true, TRUE);
	ASSERT_KWD(false, FALSE);
	ASSERT_KWD(and, AND);
	ASSERT_KWD(or, OR);
	ASSERT_KWD(func, FUNC);
	ASSERT_KWD(var, VAR);
	return 0;
}
Exemplo n.º 19
0
static int test_lex_num_literal_1() {
	struct ymd_lex lex;
	struct ytoken token;
	int rv;
	lex_init(&lex, NULL, "0 -1 -0 0123456789 9 1 12 123 1234");
	ASSERT_DEC(0);
	ASSERT_DEC(-1);
	ASSERT_DEC(-0);
	ASSERT_DEC(0123456789);
	ASSERT_DEC(9);
	ASSERT_DEC(1);
	ASSERT_DEC(12);
	ASSERT_DEC(123);
	ASSERT_DEC(1234);
	ASSERT_TOKEN(EOS);
	return 0;
}
Exemplo n.º 20
0
void do_whole_file(const char *filename)
{
  // Do not set current_filename.
  FILE *fp;
  if (strcmp(filename, "-") == 0)
    fp = stdin;
  else {
    errno = 0;
    fp = fopen(filename, "r");
    if (fp == 0)
      fatal("can't open `%1': %2", filename, strerror(errno));
  }
  lex_init(new file_input(fp, filename));
  if (yyparse())
    had_parse_error = 1;
  parse_cleanup();
  lex_cleanup();
}
Exemplo n.º 21
0
int main()
{
    FILE *in = stdin;
    char *line = NULL;
    size_t line_len = 0;
    struct lexer lexer;
    lex_init(&lexer);
    void *pParser;

    pParser = ParseAlloc(malloc);
    ssize_t nread = 0;
    while (-1 != (nread = getline(&line, &line_len, in))) {
        lex_setup_next_line(&lexer, line, nread, feof(in));
        struct token token;

        while (lex(&lexer, &token))
            Parse(pParser, token.type, token, print);
    }
    Parse(pParser, 0, (struct token){.type = 0, .location = lexer.location}, print);
Exemplo n.º 22
0
static int test_lex_punc_1() {
	struct ymd_lex lex;
	struct ytoken token;
	int rv;
	lex_init(&lex, NULL, "->!~+-=<>/\n\t .%^*(){}[]:==>=<=!=~=>>|><<@{|&");
	ASSERT_TOKEN(DICT);
	ASSERT_TOKEN('!');
	ASSERT_TOKEN('~');
	ASSERT_TOKEN('+');
	ASSERT_TOKEN('-');
	ASSERT_TOKEN('=');
	ASSERT_TOKEN('<');
	ASSERT_TOKEN('>');
	ASSERT_TOKEN('/');
	ASSERT_TOKEN('.');
	ASSERT_TOKEN('%');
	ASSERT_TOKEN('^');
	ASSERT_TOKEN('*');
	ASSERT_TOKEN('(');
	ASSERT_TOKEN(')');
	ASSERT_TOKEN('{');
	ASSERT_TOKEN('}');
	ASSERT_TOKEN('[');
	ASSERT_TOKEN(']');
	ASSERT_TOKEN(':');
	ASSERT_TOKEN(EQ);
	ASSERT_TOKEN(GE);
	ASSERT_TOKEN(LE);
	ASSERT_TOKEN(NE);
	ASSERT_TOKEN(MATCH);
	ASSERT_TOKEN(RSHIFT_A);
	ASSERT_TOKEN(RSHIFT_L);
	ASSERT_TOKEN(LSHIFT);
	ASSERT_TOKEN(SKLS);
	ASSERT_TOKEN('|');
	ASSERT_TOKEN('&');
	ASSERT_TOKEN(EOS);
	return 0;
}
Exemplo n.º 23
0
json_ref json_loads(const char *string, size_t flags, json_error_t *error)
{
    lex_t lex;
    string_data_t stream_data;

    jsonp_error_init(error, "<string>");

    if (string == nullptr) {
        error_set(error, nullptr, "wrong arguments");
        return nullptr;
    }

    stream_data.data = string;
    stream_data.pos = 0;

    if(lex_init(&lex, string_get, (void *)&stream_data))
        return nullptr;

    auto result = parse_json(&lex, flags, error);

    lex_close(&lex);
    return result;
}
Exemplo n.º 24
0
json_t *json_loadf(FILE *input, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
        return NULL;

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}
Exemplo n.º 25
0
int main(int argc, char** argv, char** envv)
{
    int symbolCount = 0;
    const struct Symbol* symbols[MAX_PROGRAM_SYMBOLS];
    const struct Program* program;
    FILE* inputFile = stdin;
    if (argc > 1)
    {
        //printf("Reading from file %s\n", argv[1]);
        inputFile = fopen(argv[1], "r");
    }
    else
    {
        //printf("Reading from stdin\n");
    }
    
    // Perform lexical analysis
    lex_init(inputFile);
    symbols[symbolCount] = lex_next_symbol();
    while (symbols[symbolCount]->type != SYMBOL_EOF)
    {
        //printf("symbol %d\n", symbols[symbolCount]->type);
        symbolCount += 1; if (symbolCount >= MAX_PROGRAM_SYMBOLS) break;
        symbols[symbolCount] = lex_next_symbol();
    }
    if (symbols[symbolCount]->type == SYMBOL_EOF)
        symbolCount += 1;
    //printf("eof\n");
    
#if 0
    // Test code
    // foo = 3 + fred
    // print foo + 16 + 4
    symbolCount = 12;
    symbols[0] = &testIds[0].base;
    symbols[1] = &testSyms[0];
    symbols[2] = &testNums[0].base;
    symbols[3] = &testSyms[1];
    symbols[4] = &testIds[1].base;
    symbols[5] = &testSyms[2];
    symbols[6] = &testSyms[3];
    symbols[7] = &testIds[2].base;
    symbols[8] = &testSyms[4];
    symbols[9] = &testNums[1].base;
    symbols[10] = &testSyms[5];
    symbols[11] = &testNums[2].base;
#endif

    // Eliminate trailing newlines
    while ((symbolCount > 1) && (symbols[symbolCount-1]->type == SYMBOL_EOF) &&
        (symbols[symbolCount-2]->type == SYMBOL_EOL))
    {
        symbols[symbolCount-2] = symbols[symbolCount-1];
        symbolCount -= 1;
        //printf("eliminated newline at end of file\n");
    }
    
    
    // Perform grammatical analysis
    program = parser_parse(symbols, symbolCount);
    
    // Interpret the instructions
    interp_execute(program);
        
    return 0;
}
Exemplo n.º 26
0
void do_picture(FILE *fp)
{
  flyback_flag = 0;
  int c;
  a_delete graphname;
  graphname = strsave("graph");		// default picture name in TeX mode
  while ((c = getc(fp)) == ' ')
    ;
  if (c == '<') {
    string filename;
    while ((c = getc(fp)) == ' ')
      ;
    while (c != EOF && c != ' ' && c != '\n') {
      filename += char(c);
      c = getc(fp);
    }
    if (c == ' ') {
      do {
	c = getc(fp);
      } while (c != EOF && c != '\n');
    }
    if (c == '\n') 
      current_lineno++;
    if (filename.length() == 0)
      error("missing filename after `<'");
    else {
      filename += '\0';
      const char *old_filename = current_filename;
      int old_lineno = current_lineno;
      // filenames must be permanent
      do_file(strsave(filename.contents()));
      current_filename = old_filename;
      current_lineno = old_lineno;
    }
    out->set_location(current_filename, current_lineno);
  }
  else {
    out->set_location(current_filename, current_lineno);
    string start_line;
    while (c != EOF) {
      if (c == '\n') {
	current_lineno++;
	break;
      }
      start_line += c;
      c = getc(fp);
    }
    if (c == EOF)
      return;
    start_line += '\0';
    double wid, ht;
    switch (sscanf(&start_line[0], "%lf %lf", &wid, &ht)) {
    case 1:
      ht = 0.0;
      break;
    case 2:
      break;
    default:
      ht = wid = 0.0;
      break;
    }
    out->set_desired_width_height(wid, ht);
    out->set_args(start_line.contents());
    lex_init(new top_input(fp));
    if (yyparse()) {
      had_parse_error = 1;
      lex_error("giving up on this picture");
    }
    parse_cleanup();
    lex_cleanup();

    // skip the rest of the .PF/.PE line
    while ((c = getc(fp)) != EOF && c != '\n')
      ;
    if (c == '\n')
      current_lineno++;
    out->set_location(current_filename, current_lineno);
  }
}
Exemplo n.º 27
0
/** Run in interactive mode.
 *
 * Repeatedly read in statements from the user and execute them.
 */
void imode_run(void)
{
	input_t *input;
	lex_t lex;
	parse_t parse;
	stree_program_t *program;
	stree_stat_t *stat;
	stree_proc_t *proc;
	stree_fun_t *fun;
	stree_symbol_t *fun_sym;
	stype_t stype;
	stype_block_vr_t *block_vr;
	list_node_t *bvr_n;
	rdata_item_t *rexpr;
	rdata_item_t *rexpr_vi;

	run_t run;
	run_proc_ar_t *proc_ar;

	bool_t quit_im;

	/* Create an empty program. */
	program = stree_program_new();
	program->module = stree_module_new();

	/* Declare builtin symbols. */
	builtin_declare(program);

	/* Process the library. */
	if (program_lib_process(program) != EOK)
		exit(1);

	/* Resolve ancestry. */
	ancr_module_process(program, program->module);

	/* Bind internal interpreter references to symbols. */
	builtin_bind(program->builtin);

	/* Resolve ancestry. */
	ancr_module_process(program, program->module);

	/* Construct typing context. */
	stype.program = program;
	stype.proc_vr = stype_proc_vr_new();
	list_init(&stype.proc_vr->block_vr);
	stype.current_csi = NULL;
	proc = stree_proc_new();

	fun = stree_fun_new();
	fun_sym = stree_symbol_new(sc_fun);
	fun_sym->u.fun = fun;
	fun->name = stree_ident_new();
	fun->name->sid = strtab_get_sid("$imode");
	fun->sig = stree_fun_sig_new();

	stype.proc_vr->proc = proc;
	fun->symbol = fun_sym;
	proc->outer_symbol = fun_sym;

	/* Create block visit record. */
	block_vr = stype_block_vr_new();
	intmap_init(&block_vr->vdecls);

	/* Add block visit record to the stack. */
	list_append(&stype.proc_vr->block_vr, block_vr);

	/* Construct run context. */
	run_gdata_init(&run);

	run.thread_ar = run_thread_ar_new();
	list_init(&run.thread_ar->proc_ar);
	run_proc_ar_create(&run, run.gdata, proc, &proc_ar);
	list_append(&run.thread_ar->proc_ar, proc_ar);

	printf("SBI interactive mode. ");
	os_input_disp_help();

	quit_im = b_false;
	while (quit_im != b_true) {
		parse.error = b_false;
		stype.error = b_false;
		run.thread_ar->exc_payload = NULL;
		run.thread_ar->bo_mode = bm_none;

		input_new_interactive(&input);

		/* Parse input. */
		lex_init(&lex, input);
		parse_init(&parse, program, &lex);

		if (lcur_lc(&parse) == lc_eof)
			break;

		stat = parse_stat(&parse);

		if (parse.error != b_false)
			continue;

		/* Type statement. */
		stype_stat(&stype, stat, b_true);

		if (stype.error != b_false)
			continue;

		/* Run statement. */
		run_init(&run);
		run.program = program;
		run_stat(&run, stat, &rexpr);

		/* Check for unhandled exceptions. */
		run_exc_check_unhandled(&run);

		if (rexpr != NULL) {
			/* Convert expression result to value item. */
			run_cvt_value_item(&run, rexpr, &rexpr_vi);
			rdata_item_destroy(rexpr);

			/* Check for unhandled exceptions. */
			run_exc_check_unhandled(&run);
		} else {
			rexpr_vi = NULL;
		}

		/*
		 * rexpr_vi can be NULL if either repxr was null or
		 * if the conversion to value item raised an exception.
		 */
		if (rexpr_vi != NULL) {
			assert(rexpr_vi->ic == ic_value);

			/* Print result. */
			printf("Result: ");
			rdata_value_print(rexpr_vi->u.value);
			printf("\n");

			rdata_item_destroy(rexpr_vi);
		}
	}

	run_proc_ar_destroy(&run, proc_ar);

	/* Remove block visit record from the stack, */
	bvr_n = list_last(&stype.proc_vr->block_vr);
	assert(list_node_data(bvr_n, stype_block_vr_t *) == block_vr);
	list_remove(&stype.proc_vr->block_vr, bvr_n);

	printf("\nBye!\n");
}
Exemplo n.º 28
0
/* Main program */
int main(int argc, char *argv[]){
   char **infiles, **outfiles;
   int nfiles, nout;
   char *arg_string;
   Loop_Options *loop_options;
   char *pname;
   int i;
   ident_t ident;
   scalar_t scalar;

   /* Save time stamp and args */
   arg_string = time_stamp(argc, argv);

   /* Get arguments */
   pname = argv[0];
   if (ParseArgv(&argc, argv, argTable, 0) || (argc < 2)) {
      (void) fprintf(stderr, 
      "\nUsage: %s [options] [<in1.mnc> ...] <out.mnc>\n",
                     pname);
      (void) fprintf(stderr, 
        "       %s -help\n\n", pname);
      exit(EXIT_FAILURE);
   }

   /* Get output file names */
   nout = (Output_list == NULL ? 1 : Output_list_size);
   outfiles = malloc(nout * sizeof(*outfiles));
   if (Output_list == NULL) {
      outfiles[0] = argv[argc-1];
   }
   else {
      for (i=0; i < Output_list_size; i++) {
         outfiles[i] = Output_list[i].file;
      }
   }

   /* check for output files */
   for (i=0; i < nout; i++){
      if(access(outfiles[i], F_OK) == 0 && !clobber){
         fprintf(stderr, "%s: %s exists, use -clobber to overwrite\n\n",
                 argv[0], outfiles[i]);
         exit(EXIT_FAILURE);
      }
   }

   /* Get the list of input files either from the command line or
      from a file, or report an error if both are specified.
      Note that if -outfile is given then there is no output file name
      left on argv after option parsing. */
   nfiles = argc - 2;
   if (Output_list != NULL) nfiles++;
   if (filelist == NULL) {
      infiles = &argv[1];
   }
   else if (nfiles <= 0) {
      infiles = read_file_names(filelist, &nfiles);
      if (infiles == NULL) {
         (void) fprintf(stderr, 
                        "Error reading in file names from file \"%s\"\n",
                        filelist);
         exit(EXIT_FAILURE);
      }
   }
   else {
      (void) fprintf(stderr, 
                     "Do not specify both -filelist and input file names\n");
      exit(EXIT_FAILURE);
   }

   /* Make sure that we have something to process */
   if (nfiles == 0) {
      (void) fprintf(stderr, "No input files specified\n");
      exit(EXIT_FAILURE);
   }

   /* Get the expression from the file if needed */
   if ((expression == NULL) && (expr_file == NULL)) { 
      (void) fprintf(stderr, 
                     "An expression must be specified on the command line\n");
      exit(EXIT_FAILURE);
   }
   else if (expression == NULL) {
      expression = read_expression_file(expr_file);
   }

   /* Parse expression argument */
   if (debug) fprintf(stderr, "Feeding in expression %s\n", expression);
   lex_init(expression);
   if (debug) yydebug = 1; else yydebug = 0; 
   yyparse();
   lex_finalize();
   
   /* Optimize the expression tree */
   root = optimize(root);
   
   /* Setup the input vector from the input files */
   A = new_vector();
   for (i=0; i<nfiles; i++) {
      if (debug) fprintf(stderr,"Getting file[%d] %s\n", i, argv[i+1]);
      scalar = new_scalar(eval_width);
      vector_append(A, scalar);
      scalar_free(scalar);
   }
      
   /* Construct initial symbol table from the A vector. Since setting
      a symbol makes a copy, we have to get a handle to that copy. */
   rootsym = sym_enter_scope(NULL);
   ident = new_ident("A");
   sym_set_vector(eval_width, NULL, A, ident, rootsym);
   vector_free(A);
   A = sym_lookup_vector(ident, rootsym);
   if (A==NULL) {
      (void) fprintf(stderr, "Error initializing symbol table\n");
      exit(EXIT_FAILURE);
   }
   vector_incr_ref(A);

   /* Add output symbols to the table */
   if (Output_list == NULL) {
      Output_values = NULL;
   }
   else {
      Output_values = malloc(Output_list_size * sizeof(*Output_values));
      for (i=0; i < Output_list_size; i++) {
         ident = ident_lookup(Output_list[i].symbol);
         scalar = new_scalar(eval_width);
         sym_set_scalar(eval_width, NULL, scalar, ident, rootsym);
         scalar_free(scalar);
         Output_values[i] = sym_lookup_scalar(ident, rootsym);
      }
   }

   /* Set default copy_all_header according to number of input files */
   if (copy_all_header == DEFAULT_BOOL)
      copy_all_header = (nfiles == 1);

   if (value_for_illegal_operations == DEFAULT_DBL) {
      if (use_nan_for_illegal_values)
         value_for_illegal_operations = INVALID_DATA;
      else
         value_for_illegal_operations = 0.0;
   }

   /* Do math */
   loop_options = create_loop_options();
   set_loop_verbose(loop_options, verbose);
   set_loop_clobber(loop_options, clobber);
#if MINC2
   set_loop_v2format(loop_options, minc2_format);
#endif /* MINC2 */
   set_loop_datatype(loop_options, datatype, is_signed, 
                     valid_range[0], valid_range[1]);
   set_loop_copy_all_header(loop_options, copy_all_header);
   
   /* only set buffer size if specified */
   if(max_buffer_size_in_kb != 0){
      set_loop_buffer_size(loop_options, (long) 1024 * max_buffer_size_in_kb);
      }
   
   set_loop_check_dim_info(loop_options, check_dim_info);
   voxel_loop(nfiles, infiles, nout, outfiles, arg_string, loop_options,
              do_math, NULL);
   free_loop_options(loop_options);

   
   /* Clean up */
   vector_free(A);
   sym_leave_scope(rootsym);
   if (expr_file != NULL) free(expression);
   free(outfiles);
   if (Output_list != NULL) free(Output_list);
   if (Output_values != NULL) free(Output_values);
   exit(EXIT_SUCCESS);
}
Exemplo n.º 29
0
int main(int argc, char *argv[])
{
    STANDARDIZER *std;
    LEXICON *lex;
    LEXICON *gaz;
    RULES *rules;

    char buf[1024];

    int seq;
    char input_str[ 4096 ] ;
    char word[512];
    char stdword[512];
    int token;
    int nr;
    int rule[RULESIZE];
    int err;
    int cnt;
    int option = 0;

    FILE *in;

    if (argc == 3 && !strcmp(argv[1], "-o")) {
        option = strtol(argv[2], NULL, 10);
        argc -= 2;
        argv += 2;
    }
    else if (argc != 1) 
        Usage();

    std = std_init();
    assert(std);

    lex = lex_init(std->err_p);
    assert(lex);

    in = fopen(LEXIN, "rb");
    assert(in);

    cnt = 0;
    while (!feof(in) && fgets(buf, 1024, in)) {
        cnt++;
        /* parse into fields */
        if (parse_csv(buf, &seq, word, stdword, &token)) {
            /* add the record to the lexicon */
            err = lex_add_entry(lex, seq, word, stdword, token);
            if (err != 1)
                printf("lex: Failed: %d: %s", cnt, buf);
        }
        else {
            printf("lex: Skipping: %d: %s", cnt, buf);
        }
    }
    fclose(in);

    if (option & 1) {
        printf("------------ address lexicon --------------\n");
        print_lexicon(lex->hash_table);
        printf("\n");
    }

    gaz = lex_init(std->err_p);
    assert(gaz);

    in = fopen(GAZIN, "rb");
    assert(in);

    cnt = 0;
    while (!feof(in) && fgets(buf, 1024, in)) {
        cnt++;
        /* parse into fields */
        if (parse_csv(buf, &seq, word, stdword, &token)) {
            /* add the record to the lexicon */
            err = lex_add_entry(gaz, seq, word, stdword, token);
            if (err != 1)
                printf("gaz: Failed: %d: %s", cnt, buf);
        }
        else {
            printf("gaz: Skipping: %d: %s", cnt, buf);
        }
    }
    fclose(in);

    if (option & 2) {
        printf("------------ gazeteer lexicon --------------\n");
        print_lexicon(gaz->hash_table);
        printf("\n");
    }

    rules = rules_init(std->err_p);
    assert(rules);
    rules -> r_p -> collect_statistics = TRUE ;

    /* ************ RULES **************** */

    in = fopen(RULESIN, "rb");
    assert(in);

    cnt = 0;
    while (!feof(in) && fgets(buf, 1024, in)) {
        cnt++;
        /* parse into fields */
        nr = parse_rule(buf, rule);

        /* add the record to the rules */
        err = rules_add_rule(rules, nr, rule);
        if (err != 0)
            printf("rules: Failed: %d (%d): %s", cnt, err, buf);
    }
    err = rules_ready(rules);
    if (err != 0)
        printf("rules: Failed: err=%d\n", err);
    fclose(in);

    std_use_lex(std, lex);
    std_use_gaz(std, gaz);
    std_use_rules(std, rules);
    std_ready_standardizer(std);

    printf( "Standardization test. Type \"exit\" to quit:\n" ) ;
    fflush( stdout ) ;
    while ( TRUE ) {
        err = standardize_command_line( std, input_str, option ) ;
        if ( err == FAIL ) {
            break ;
        }
    }
    printf( "OK\n" ) ;
    fflush( stdout ) ;

    std_free(std);
/* these were freed when we bound them with std_use_*()
    rules_free(rules);
    lex_free(gaz);
    lex_free(lex);
*/

    return 0;
}
Exemplo n.º 30
0
static STANDARDIZER *
CreateStd(char *lextab, char *gaztab, char *rultab)
{
    STANDARDIZER        *std;
    LEXICON             *lex;
    LEXICON             *gaz;
    RULES               *rules;
    int                  err;
    int                  SPIcode;

    DBG("Enter: CreateStd");
    SPIcode = SPI_connect();
    if (SPIcode != SPI_OK_CONNECT) {
        elog(ERROR, "CreateStd: couldn't open a connection to SPI");
    }

    std = std_init();
    if (!std)
        elog(ERROR, "CreateStd: could not allocate memory (std)");

    lex = lex_init(std->err_p);
    if (!lex) {
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: could not allocate memory (lex)");
    }

    err = load_lex(lex, lextab);
    if (err == -1) {
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: failed to load '%s' for lexicon", lextab);
    }

    gaz = lex_init(std->err_p);
    if (!gaz) {
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: could not allocate memory (gaz)");
    }

    err = load_lex(gaz, gaztab);
    if (err == -1) {
        lex_free(gaz);
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: failed to load '%s' for gazeteer", gaztab);
    }

    rules = rules_init(std->err_p);
    if (!rules) {
        lex_free(gaz);
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: could not allocate memory (rules)");
    }

    err = load_rules(rules, rultab);
    if (err == -1) {
        rules_free(rules);
        lex_free(gaz);
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: failed to load '%s' for rules", rultab);
    }

    std_use_lex(std, lex);
    std_use_gaz(std, gaz);
    std_use_rules(std, rules);
    std_ready_standardizer(std);

    SPI_finish();

    return std;
}