示例#1
0
int main(int argc, char** argv)	{
    char* output;
    
	if (argc != 2)	{
		fprintf(stderr, "USAGE: cbnf <grammar file>\n");
		return 1;
	}
	
	FILE* fp = fopen(argv[1], "r");
	
	ast_node* root = parse_grammar(fp);
    ast_pretty_print(stdout, root, cbnf_node_type_to_string);
	
	analyze(root);
	
    if (codegen_run(root, &output) == 0)    {
	    fprintf(stdout, "\n\n%s\n", output);
    }
	
	ast_free_node(root);
	
	fclose(fp);
	return 0;
}
示例#2
0
文件: parser.c 项目: Jille/splparser
int
main(int argc, char **argv) {
	// Parse command-line options
	int showsynt = 0, showir = 0, showssm = 0, next_out = 0, fileset = 0;
	char *file = "in.spl";
	char *outfile = "out.ssm";
	int argi;

	for(argi = 1; argi < argc; ++argi) {
		char *arg = argv[argi];
		if(next_out == 1) {
			next_out = 0;
			// if -noout was not explicitly given earlier:
			if(outfile != 0)
				outfile = arg;
			continue;
		}
		if(strcmp(arg, "-o") == 0 || strcmp(arg, "-out") == 0) {
			next_out = 1;
		} else if(strcmp(arg, "-noout") == 0) {
			outfile = 0;
		} else if(strcmp(arg, "-sSYNT") == 0) {
			showsynt = 1;
		} else if(strcmp(arg, "-sIR") == 0) {
			showir = 1;
		} else if(strcmp(arg, "-sSSM") == 0) {
			showssm = 1;
		} else if(strncmp(arg, "-", 1) == 0) {
			fprintf(stderr, "Didn't understand parameter: %s\n", argv[argi]);
			usage(argv[0]);
			return 2;
		} else {
			if(fileset) {
				fprintf(stderr, "Two output files given, this is illegal (was=%s, new=%s)\n", file, arg);
				usage(argv[0]);
				return 3;
			}
			fileset = 1;
			file = arg;
		}
	}

	if(next_out) {
		fprintf(stderr, "-o needs an argument, the output file\n");
		usage(argv[0]);
		return 4;
	}

	struct stat res;
	if(stat(file, &res) < 0) {
		fprintf(stderr, "Could not open input file %s: %s\n", file, strerror(errno));
		return 5;
	}

	lazyarray *g  = lazyarray_create(gen_tokens, file, 1);
	// XXX load the grammar in another thread
	grammar *gram = parse_grammar("grammar.g");

	synt_error *e = create_synt_error();
	synt_tree *t;
	if(!parser(g, gram, &t, e)) {
		fprintf(stderr, "Failed to parse, error on line %d: %s\n", e->row, e->error);
		return 1;
	}

	if(showsynt) {
		fprintf(stderr, "Syntax tree:\n");
		show_synt_tree(t, 0, gram);
	}

	struct irunit *ir = typechecker(t, gram);
	if(showir) {
		fprintf(stderr, "IR tree:\n");
		show_ir_tree(ir, 0);
		printf("\n");
	}

	struct ssmline *ssm = ir_to_ssm(ir);
	if(showssm) {
		fprintf(stderr, "SSM:\n");
		show_ssm(ssm);
	}

	if(outfile != 0) {
		FILE *outh = fopen(outfile, "w");
		if(outh == 0) {
			fprintf(stderr, "Could not open output file %s: %s\n", outfile, strerror(errno));
			return 6;
		}
		write_ssm(ssm, outh);
		fclose(outh);
	}

	free(t);
	lazyarray_destroy(g);
	return 0;
}