예제 #1
0
static void
test_lex(const char *input)
{
    struct ds output;

    ds_init(&output);
    struct lexer lexer;

    lexer_init(&lexer, input);
    ds_clear(&output);
    while (lexer_get(&lexer) != LEX_T_END) {
        size_t len = output.length;
        lex_token_format(&lexer.token, &output);

        /* Check that the formatted version can really be parsed back
         * losslessly. */
        if (lexer.token.type != LEX_T_ERROR) {
            const char *s = ds_cstr(&output) + len;
            struct lexer l2;

            lexer_init(&l2, s);
            lexer_get(&l2);
            compare_token(&lexer.token, &l2.token);
            lexer_destroy(&l2);
        }
        ds_put_char(&output, ' ');
    }
    lexer_destroy(&lexer);

    ds_chomp(&output, ' ');
    puts(ds_cstr(&output));
    ds_destroy(&output);
}
예제 #2
0
파일: main.c 프로젝트: doniexun/jacc
int cmd_lex(FILE *file, const char *filename)
{
    struct token token;
    buffer_t token_value;

    token_value = buffer_create(1024);

    lexer_init(file);
    log_set_unit(basename(filename));

    printf("Line\tText\tValue\tType\n");
    fflush(stdout);

    while (lexer_next_token(&token)) {
        lexer_token_value(&token, token_value);

        printf("%d:%d\t%s\t%s\t%s\n", token.line, token.column, token.text,
            buffer_data(token_value), lexer_token_type_name(token.type));
        fflush(stdout);

        lexer_token_free_data(&token);
    }

    buffer_free(token_value);
    lexer_destroy();
    log_close();

    return EXIT_SUCCESS;
}
예제 #3
0
파일: main.c 프로젝트: erik/arroyo
int run_repl(context* ctx)
{
  parser_state* ps = calloc(sizeof(parser_state), 1);
  ps->die_on_error = 0;
  ps->error.max = 20;

  for(unsigned line = 1; ; ++line) {
    char* input = readline(">> ");

    if(input == NULL)
      break;

    add_history(input);

    char* ret = malloc(strlen(input) + 2);
    strcpy(ret, input);
    strcat(ret, "\n");
    free(input);

    reader r;
    string_reader_create(&r, ret);

    lexer_state* ls = calloc(sizeof(lexer_state), 1);
    lexer_create(ls, &r);

    ps->ls = ls;
    ps->ls->linenum = line;
    ps->t = lexer_next_token(ps->ls);

    expression_node* program = parse_program(ps);

    if(program && program->node.block) {

      for(struct expression_list* expr = program->node.block->list; expr; expr = expr->next) {

        expression_node* eval = expression_node_evaluate(expr->expression, ctx);
        char* str = expression_node_inspect(eval);

        printf("==> %s\n", str);

        free(str);
        expression_node_destroy(eval);
      }

      expression_node_destroy(program);

      free(r.fn_data);
      lexer_destroy(ls);
      free(ls);

      free(ret);
    }
  }

  free(ps);
  return 0;
}
예제 #4
0
파일: tvm_program.c 프로젝트: atoun/tinyvm
int interpret_program(tvm_program_t* p, char* filename, tvm_memory_t* pMemory)
{
	int i;
	FILE* pFile = NULL;

	int source_length = 0;
	char* source = NULL;

	tvm_lexer_t* lexer = NULL;

	/* Attempt to open the file. If the file cannot be opened, try once more. */
	if(filename)
		for(i = 0; i < 2; i++)
			if(!pFile) pFile = tvm_fopen(filename, ".vm", "r");

	if(!pFile)
	{
		printf("File was not found, or does not exist. Unable to interpret.\n");
		return 1;
	}

	source_length = tvm_flength(pFile);
	source = malloc(source_length);

	tvm_fcopy(source, source_length, pFile);

	lexer = lexer_create();
	lex(lexer, source);

	free(source);
	fclose(pFile);

	if(parse_labels(p, (const char***)lexer->tokens) != 0)
		return 1;

	for(i = 0; lexer->tokens[i]; i++)
	{
		p->instr = (int*)realloc(p->instr, sizeof(int) * (i + 2));
		p->instr[i] = 0;

		p->args = (int***)realloc(p->args, sizeof(int**) * (i + 2));
		p->args[i] = (int**)calloc(MAX_ARGS, sizeof(int*));

		parse_instruction(p, (const char**)lexer->tokens[i], pMemory);
	}

	lexer_destroy(lexer);

	p->args[i] = NULL;
	p->instr[i] = -0x1;

	return 0;
}
예제 #5
0
/* Like actions_parse(), but the actions are taken from 's'. */
char * OVS_WARN_UNUSED_RESULT
actions_parse_string(const char *s, const struct action_params *ap,
                     struct ofpbuf *ofpacts, struct expr **prereqsp)
{
    struct lexer lexer;
    char *error;

    lexer_init(&lexer, s);
    lexer_get(&lexer);
    error = actions_parse(&lexer, ap, ofpacts, prereqsp);
    lexer_destroy(&lexer);

    return error;
}
예제 #6
0
파일: main.c 프로젝트: doniexun/jacc
int cmd_parse_expr(FILE *file, const char *filename, const char *cmd)
{
    log_set_unit(basename(filename));

    lexer_init(file);
    parser_init();
    generator_init();

    struct node* node = NULL;
    symtable_t symtable = NULL;
    code_t code = NULL;

    if (strcmp(cmd, "parse_expr") == 0) {
        parser_flags_set(0);
        node = parser_parse_expr();
        print_node(node, 0, 0);
    } else if (strcmp(cmd, "parse_stmt") == 0) {
        parser_flags_set(0);
        node = parser_parse_statement();
        print_node(node, 0, 0);
    } else if (strcmp(cmd, "parse") == 0) {
        parser_flags_set(PF_RESOLVE_NAMES);
        symtable = parser_parse();
        print_symtable(symtable, 0);
    } else if (strcmp(cmd, "compile") == 0) {
        symtable = parser_parse();
        if (symtable != NULL) {
            code = generator_process(symtable);
            optimizer_optimize(code);
            generator_print_code(code);
        } else {
            print_symtable(symtable, 0);
        }
    }

    parser_free_node(node);
    symtable_destroy(symtable, 1);
    generator_free_code(code);

    generator_destroy();
    parser_destroy();
    lexer_destroy();

    log_close();
    return EXIT_SUCCESS;
}
예제 #7
0
파일: actions.c 프로젝트: HaochuanXJTU/ovs
/* Like actions_parse(), but the actions are taken from 's'. */
char * OVS_WARN_UNUSED_RESULT
actions_parse_string(const char *s, const struct shash *symtab,
                     const struct simap *ports, const struct simap *ct_zones,
                     uint8_t first_table, uint8_t n_tables, uint8_t cur_table,
                     uint8_t output_table, struct ofpbuf *ofpacts,
                     struct expr **prereqsp)
{
    struct lexer lexer;
    char *error;

    lexer_init(&lexer, s);
    lexer_get(&lexer);
    error = actions_parse(&lexer, symtab, ports, ct_zones, first_table,
                          n_tables, cur_table, output_table, ofpacts,
                          prereqsp);
    lexer_destroy(&lexer);

    return error;
}
예제 #8
0
파일: main.c 프로젝트: erik/arroyo
int run_file(const char* filename, context* ctx)
{
  reader r;

  lexer_state* ls = calloc(sizeof(lexer_state), 1);
  parser_state* ps = calloc(sizeof(parser_state), 1);

  FILE* fp = fopen(filename, "r");

  if(!fp) {
    printf("Error: can't open file '%s'\n", filename);
    return 1;
  }

  file_reader_create(&r, fp);
  lexer_create(ls, &r);

  ps->ls = ls;
  ps->die_on_error = 0;
  ps->error.max = 20;
  ps->t = lexer_next_token(ps->ls);

  expression_node* program = parse_program(ps);
  expression_node* eval = expression_node_evaluate(program, ctx);
  expression_node_destroy(eval);
  expression_node_destroy(program);

  lexer_destroy(ls);
  free(ls);
  free(ps);

  free(r.fn_data);

  fclose(fp);

  return 0;
}
예제 #9
0
파일: main.c 프로젝트: nilium/bmxc
int main(int argc, char const *argv[])
{
	for (--argc, ++argv; 0 < argc; --argc, ++argv) {
		lexer_t *lexer = NULL;
		char *buffer = NULL;
		FILE *fp = NULL;
		long length = -1;
		long readlen = 0;

		if (!(*argv)) {
			fprintf(stderr, "NULL argument to bmxlex\n");
			return 1;
		}

		fp = fopen(*argv, "rb");
		if (fp == NULL) {
			fprintf(stderr, "Failed to open file %s\n", *argv);
			return 1;
		}

		if (fseek(fp, 0, SEEK_END)) {
			fprintf(stderr, "Failed to seek to the end of %s\n", *argv);
			goto ERROR_CLOSE_FILE;
		}

		length = ftell(fp);
		if (length == -1) {
			fprintf(stderr, "Failed to get the length of %s\n", *argv);
			goto ERROR_CLOSE_FILE;
		}

		if (fseek(fp, 0, SEEK_SET)) {
			fprintf(stderr, "Failed to seek to the start of %s\n", *argv);
			goto ERROR_CLOSE_FILE;
		}

		buffer = (char *)malloc(length);
		if (buffer == NULL) {
			fprintf(stderr, "Failed to allocate memory for source buffer for %s\n", *argv);
			goto ERROR_CLOSE_FILE;
		}

		if ((readlen = fread(buffer, 1, length, fp)) != length) {
			fprintf(stderr, "Failed to read file %s to source buffer (read %ld)\n", *argv, readlen);
			goto ERROR_CLOSE_FREE;
		}

		fclose(fp);

		lexer = lexer_new(buffer, buffer + length);
		int err = lexer_run(lexer);
		if (err) {
			fprintf(stderr, "Failed to lex %s\nError: %s\n", *argv, lexer_get_error(lexer));
			// free(buffer);
			// return 1;
		}

		{
			size_t num_tokens = lexer_get_num_tokens(lexer);
			size_t index = 0;
			for (; index < num_tokens; ++index) {
				token_t token;
				char *str = NULL;
				char *trapped = NULL;
				size_t len = 0;
				size_t trapped_len = 0;
				size_t idx = 0;
				size_t trapped_idx = 0;

				lexer_get_token(lexer, index, &token);

				if (token.kind == TOK_INVALID) {
					free(buffer);
					fprintf(stderr, "Invalid token encountered at %d:%d in %s\n",
						token.line, token.column, *argv);
					return 1;
				}

				str = token_to_string(&token);
				len = strlen(str);
				for (; idx < len; ++idx)
					trapped_len += ((str[idx] == '\n') ? 2 : 1);
				trapped = calloc(trapped_len + 1, 1);
				for (idx = 0; idx < len; ++idx, ++trapped_idx) {
					if (str[idx] == '\n') {
						trapped[trapped_idx++] = '\\';
						trapped[trapped_idx] = 'n';
					} else {
						trapped[trapped_idx] = str[idx];
					}
				}

				fprintf(stdout, "\"%s\"[%d:%d]:[%d:%s]:%s\n", *argv, token.line, token.column, token.kind, g_token_names[token.kind], trapped);

				free(str);
				free(trapped);
			}
		}

		lexer_destroy(lexer);
		free(buffer);

		continue;

		// error gotos
		ERROR_CLOSE_FREE:
		free(buffer);
		ERROR_CLOSE_FILE:
		fclose(fp);
		return 1;
	}

	return 0;
}
예제 #10
0
/*
 *  Command-line front end for compiler.
 */
int main(int argc, char **argv)
{
	int action;

	int arg;

	buffer_t *in_buffer;
	buffer_t *out_buffer;

	int is_done;
	parser_t *parser;
	lexer_t *lexer;
	token_t *token;


	/* Set default settings. */
	action = ACT_TRANS;
	in_buffer = buffer_create(stdin);
	out_buffer = buffer_create(stdout);

	/* Parse command-line arguments. */
	for (arg = 1; arg < argc; arg++)
	{
		if ((strcmp(argv[arg], "--help") == 0)
			|| (strcmp(argv[arg], "-h") == 0))
		{
			action = ACT_USAGE;
		}
		else if ((strcmp(argv[arg], "--lex") == 0) && (action <= ACT_LEX))
		{
			action = ACT_LEX;
		}
		else if ((strcmp(argv[arg], "--parse") == 0) && (action <= ACT_PARSE))
		{
			action = ACT_PARSE;
		}
		else if ((strcmp(argv[arg], "--translate") == 0)
			&& (action <= ACT_TRANS))
		{
			action = ACT_TRANS;
		}
		else
		{
			fprintf(stderr, "Invalid argument: %s\n", argv[arg]);
			/* Stop parsing command-line. */
			arg = argc;

			action = ACT_USAGE;
		}
	}

	/* Take action. */
	if (action == ACT_USAGE)
	{
		printf(
			"Usage: compiler [option...]\n"
			"\n"
			"  Options:\n"
			"\n"
			"  -h, --help       Display this help text.\n"
			"      --lex        Run the lexer.\n"
			"      --parse      Run the parser. (Calls the lexer.)\n"
			"      --translate  Run the translator. (Calls the parser.)\n"
		);
	}
	else if (action == ACT_LEX)
	{
		is_done = 0;
		lexer = lexer_create(in_buffer);
		token = token_create();

		while (!is_done)
		{
			lexer_lex(lexer, token);
			token_print(token, stdout);
			printf("\n");
			if (token_get_class(token) == T_EOF)
				is_done = 1;
		}

		token_destroy(token);
		lexer_destroy(lexer);

		return EXIT_SUCCESS;
	}
	else if (action == ACT_PARSE)
	{
		parser = parser_create(in_buffer);

		parser_parse(parser);

		parser_destroy(parser);

		return EXIT_SUCCESS;
	}
	else if (action == ACT_TRANS)
	{
		parser = parser_create(in_buffer);

		parser_parse(parser);
		translator_translate(parser_get_tree(parser));

		parser_destroy(parser);

		return EXIT_SUCCESS;
	}

	return EXIT_SUCCESS;
}
예제 #11
0
int parse_string (const string_t client_string, list_s lista) {
	string_t almost_done = NULL;
	string_t done = NULL;
	FILE * strf = NULL;

	int result = CODE_OK;

	assert (client_string != NULL && lista != NULL);
	strf = fmemopen (client_string, strlen(client_string) + 1, "r");
	if (strf != NULL) {
		Lexer * l = lexer_new (strf);

		lexer_next_to (l, "\n");
		almost_done = lexer_item (l);
		if (strncmp (client_string, almost_done, strlen (client_string) - 2) != 0)
			return BAD_EOL;
		lexer_destroy (l);
		fclose (strf);
		
		if (strlen (almost_done) > 1 && almost_done != NULL) {
			strf = fmemopen (almost_done, strlen(almost_done), "r");
			if (strf != NULL) {
				Lexer * j = lexer_new (strf);
				lexer_next_to (j, "\r");
				done = lexer_item (j);
				lexer_destroy (j);
				fclose (strf);

				if (strlen (done) > 1 && done != NULL) {
					strf = fmemopen (done, strlen(done), "r");
					if (strf){
						string_t valid_elem = NULL;
						Lexer * p = lexer_new (strf);
	
						while (!lexer_is_off (p)) {
							lexer_next (p, SUPERALPHANUMERIC);
							valid_elem = lexer_item (p);
							list_adjoin_back (lista, valid_elem);
							lexer_skip (p, BLANK);
						}
						lexer_destroy (p); p = NULL;
						fclose (strf);
					}
					else {
						fprintf (stderr, "fmemopen");
						result = INTERNAL_ERROR;
					}
				}
				else {
					if (done != NULL)
						result = STRING_VOID;
					else
						result = INTERNAL_ERROR;
				}
			}
			else {
				fprintf (stderr, "fmemopen");
				result = INTERNAL_ERROR;
			}
		}
		else {
			if (almost_done != NULL)
				result = STRING_VOID;
			else
				result = INTERNAL_ERROR;
		}
		if(almost_done != NULL) {
			free (almost_done); almost_done = NULL;
		}
		if(done != NULL) {
			free (done); done = NULL;
		}
	}
	else {
		fprintf (stderr, "fmemopen");
		result = INTERNAL_ERROR;
	}

	return result;
}