C实现Brainfuck编译器

#include <stdio.h>
main(int a,char *v[]) {
	char c;
	FILE *p=fopen(v[1],"r");
	freopen("a.c","w",stdout);
	printf("#include <stdio.h>\nchar a[9999];int c;main() {");
	while((c=fgetc(p))!=EOF) {
		if(c==62)printf("++c;");
		else if(c==60)printf("--c;");
		else if(c==43)printf("++a[c];");
		else if(c==45)printf("--a[c];");
		else if(c==46)printf("putchar(a[c]);");
		else if(c==44)printf("a[c]=getchar();");
		else if(c==91)printf("while(a[c]) {");
		else if(c==93)printf("}");
	}
	printf("}");
}

这样使用:

compile.exe src.bf

然后,a.c就是编译后的结果。

猜你喜欢

转载自blog.csdn.net/yoga1976/article/details/82926209