www.pudn.com > subpas.rar > subpas.c


/****************************************************/ 
/* File: subpas.c                                   */ 
/* The main procedure to use other function         */ 
/* to parse,errorcheck and build destination code   */ 
/****************************************************/ 
 
#include "globals.h" 
#include "util.h" 
#include "parse.h" 
#include "analyze.h" 
#include "CodeGen.h" 
 
int lineno = 0; 
FILE * source; 
FILE * listing; 
FILE * asmfile; 
TreeNode * syntaxTree; 
 
int TraceScan = TRUE; 
int TraceParse = TRUE; 
int TraceAnalyze = TRUE; 
 
int Error = FALSE; 
 
int main(int argc, char * argv[]) 
{ 
	int i,len; 
	char srcfilename[40],desfilename[40];	/* pascal源文件名,汇编目标文件名 */ 
 
	if(argc != 2) 
	{ 
		fprintf(stderr,"Usage: %s \n",argv[0]); 
		exit(1); 
	} 
	strcpy(srcfilename, argv[1]); 
	source = fopen(srcfilename,"r"); 
	if (source==NULL) 
	{  
		printf("Unable to open file: %s\n", srcfilename); 
		exit(1); 
	} 
	len = strlen(srcfilename);		//除去扩展名 
	for(i = len-1;i>0;i--)  
		if(srcfilename[i] == '.'){ 
			srcfilename[i] = 0; 
			break; 
		} 
	strcpy(desfilename, srcfilename); 
	strcat(srcfilename, ".lst");		//打开listing文件 
	listing = fopen(srcfilename,"w");  
	if (listing == NULL) 
	{  
		printf("Unable to open file: %s\n", srcfilename); 
		exit(1); 
	} 
 
	syntaxTree = parse();	//进行词法和语法分析,建立语法树 
	fclose(source); 
 
	if (TraceParse && !Error) { 
		printTree(syntaxTree);	//打印出语法树,作调试用 
 
	} 
	fclose(listing); 
 
	if (!Error) {	 
		errorCheck(syntaxTree);	//错误检查 
	} 
 
	if (!Error) 
	{  
		strcat(desfilename, ".asm");	//打开目标汇编文件 
		asmfile = fopen(desfilename,"w"); 
		if (asmfile == NULL) 
		{  
			printf("Unable to open file: %s\n", srcfilename); 
			exit(1); 
		} 
		genCode(syntaxTree);	//生成目标汇编代码 
		fclose(asmfile); 
	} 
 
	return 0; 
}