Exemplo n.º 1
0
int main (int argc, char **argv)
{
	FILE *stream;
	signed short c;
	char cache_file_ln[256];
	int line_length, line_nr;
	int status;
	
	if (argc != 2)
		fprintf(stderr, "%s: Argc != 2\n", argv[0]);
	
	stream = fopen(argv[1], "r");
	if (stream == NULL)
		fprintf(stderr, "File % cannot be opened\n", argv[1]);
	
	tmp = fopen("tmp", "w");
	if (tmp == NULL)
		fprintf(stderr, "tmp file cannot be opened\n");
	
	
	status = line_length = line_nr = 0;
	memset(cache_file_ln, 0, 256);
	root = (struct symbols *) create_symbol_table();
	printf("root symbol address: %p\n\n", root);
	
	while (status != EOF) {
		c = fgetc(stream);
		if (c != '\n' && c != EOF) {
			cache_file_ln[line_length++] = c;
			if (line_length >= 256)
				fprintf(stderr, "Line (%d) too large!\n", 
					line_nr + 1);
		} else {
			if (c == EOF) 
				status = EOF;
			else if (c == '\n') 
				cache_file_ln[line_length] = c;
			
			if (line_length >= 1) {
				int err = parse_line(cache_file_ln, line_length);
				if (err == OVERFLOW_DETECTED)
					fprintf(stderr, "Overflow!\n");
			}
			
			line_length = 0;
			memset(cache_file_ln, 0, 256);
			line_nr++;
		}		
	}
	puts("\n");
	print_all_symbols(root);
		
	free_symbol_table(root);
	fclose(tmp);
	fclose(stream);
	return 0;
}
Exemplo n.º 2
0
Arquivo: main.c Projeto: marcinlos/FPL
int main(int argc, char* argv[])
{
    create_symbol_table();
    register_builtins();
    greeting();
    yyparse();
    free_symbol_table();
    printf("\n");
    return 0;
}
symbol_table get_data_from_file(FILE *fp, int size){
     symbol_table ret = create_symbol_table(create_phone_entry, compar);

     int i;
     for(i = 0; i < size;i++){
          char name[25],
               tel[12];
          fscanf(fp, "%[^|]|%[^|]|%*[^|]|%*[^\n]\n", name, tel);
          add_entry(name, tel, &ret);
     }

     return ret;
}
Exemplo n.º 4
0
main(int argc, char *argv[]) {

  // make sure the filename is specified
  if (argc < 2) {
    printf("Please specify the name of the file to compile\n");
    return;
  }
  char *name  = argv[1];
  char funName[20];
  char newFileName[20];
  char line[110];

  // create the symbol table
  if (create_symbol_table(name, funName) == 0) {
    exit(0);
  }

  // create the target file
  int i = 0;
  while(name[i] != '.'){
    newFileName[i] = name[i];
    i++;
  }
  newFileName[i] = '.';
  newFileName[i + 1] = 'l';
  newFileName[i + 2] = 'c';
  newFileName[i + 3] = '3';
  newFileName[i + 4] = '\0';
  FILE *output = fopen(newFileName, "w");
  if(output == NULL) exit(0);

  //compile the file
  FILE *infile = open_file(name);
  fgets(line, 110, infile);
  generate_prologue(funName, output);
  while(!feof(infile)){
    fgets(line, 110, infile);
    compile_line(line, output);
  }
  generate_epilogue(output);

}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
	int ret;
	if(argc != 2)
	{
		printf("Inappropriate format\n");
		printf("\t%s filename\n",argv[0]);
		return 1;
	}
	/*get an empty parse tree*/
	t_parse_tree *parse_tree = NULL;
	parse_tree = create_parse_tree();
	if(parse_tree == NULL)
	{
		printf("Insufficient memory\n");
		return 1;
	}
	/*get an empty symbol table*/
	symbol_table *table = create_symbol_table(START_SIZE);
	if(table == NULL)
	{
		printf("Insufficient memory\n");
		return 2;
	}

	char *filename = argv[1];
	char pt_out[100];
	char ast_out[100];
	char lexer_out[100];
	sprintf(lexer_out, "%s_temp_lex", filename);
	sprintf(pt_out, "%s_pt.txt",filename);
	sprintf(ast_out, "%s_ast.txt",filename);
	ret = lexer(filename);
	if(ret)
	{
		printf("ERROR in the lexical phase exiting\n");
		return 1;
	}
	printf("lexer completed\n");


	FILE *fp_pt = fopen(pt_out,"w");
	FILE *fp_ast = fopen(ast_out, "w");
	ret = parser(lexer_out, fp_pt, fp_ast, &parse_tree, &table);
	if(ret)
	{
		printf("Parser ERROR\n");
		fclose(fp_pt);
		return 1;
	}
	printf("parser completed\n");
	display_parse_tree(parse_tree->root, 0, fp_pt);
	// t_parse_tree *ast = get_AST(parse_tree);
	// display_parse_tree(ast->root, 0, fp_ast);
	// free_parse_tree(parse_tree);
	// free_parse_tree(ast);
	fclose(fp_pt);
	// fclose(fp_ast);
	/**/
	ret = type_checker(parse_tree->root, &table);
	if(DISPLAY_SCOPE_GRAPH)
	{
		char file[100];
		sprintf(file,"%s_sg.txt",filename);
		FILE *fp_sg = fopen(file, "w");
		display_scope_graph(table->sg->root, 0,fp_sg);
		// display_scope_graph(table->sg->root, 0,stdout);
		fclose(fp_sg);
		//symbol table
		sprintf(file,"%s_symbol_table.txt", filename);
		FILE *fp_st = fopen(file, "w");
		display_symbol_table(table, fp_st);
		// display_symbol_table(table, stdout);
		// fclose(fp_st);
	}
	char file[100];
	parse_tree = get_decorated_ast(parse_tree);
	display_parse_tree(parse_tree->root, 0, fp_ast);
	fclose(fp_ast);
	sprintf(file, "%s_deco_ast.txt", filename);
	parse_tree = get_AST(parse_tree);
	fp_ast = fopen(file, "w");
	display_parse_tree(parse_tree->root, 0, fp_ast);
	fclose(fp_ast);

// 	symbol_list temp =  lookup_symbol_table(table, " j", 2);
// debug("in driver");
// 	printf("id-%s scope(%d) referred_count:- %d\n", temp->identifier, temp->scope, temp->referred_count);
// 	temp = exist_in_scope(" j", 5, table);
// 	printf("id-%s scope(%d) referred_count:- %d\n", temp->identifier, temp->scope, temp->referred_count);
	return 0;
}