⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 一个编译器原码
💻 CPP
字号:
#include "compile.h"

//******************************************************************/
//函数原型:int main(int argc,char * argv[])
//参数说明:argv[1]: 源程序文件的文件名
//函数功能:主程序
//返回值  :0表示正常退出
//******************************************************************/
int main(int argc,char * argv[])
{
	int	mode;

	init();					//初始化一些数组等
	mode = loadProgram(argc, argv);	//将源程序读入sourceProgram[]字符串

	getChar();				//第一次调用getSym()时,需要把源程序第一个字符赋给ch
	getSym();
	yacc_prog();			//进行语法分析

	if(errorCount == 0 || errorCount ==1)
		printf("there are %d error occurs\n\n",errorCount);
	else
		printf("there are %d errors occur\n\n",errorCount);

	if(errorCount == 0)
	{
		if(mode == 1)
			printDesCode();		//打印生成的目标代码
		interpret();		//解释执行目标代码
	}

	return 0;
}

//******************************************************************/
//函数原型:int loadProgram(int paraNum, char ** paraStr)
//参数说明:paraNum: 命令行输入时的参数个数
//			paraStr: 命令行输入的字符串内容
//函数功能:将源程序读入sourceProgram[]字符串
//返回值  :1,表示用户输入时有-a选项,需要打印生成目标代码;0表示不需要
//******************************************************************/
int loadProgram(int paraNum, char ** paraStr)
{
	FILE *	pFile;
	int		temp;
	int		returnVal;

	if(paraNum < 2)
	{
		printf("you should enter Compile + [-a] + filename\n");
		exit(0);
	}

	if( strcmp(paraStr[1],"-a") == 0 )
	{
		returnVal=1;
		if( (pFile=fopen(paraStr[2],"r")) == NULL )
		{
			printf("can't open the file, compile terminate\n");
			exit(0);
		}
	}
	else
	{
		returnVal=0;
		if( (pFile=fopen(paraStr[1],"r")) == NULL )
		{
			printf("can't open the file, compile terminate\n");
			exit(0);
		}
	}

	sCount=0;
	while( (temp=fread(sourceProgram + sCount, sizeof(char), BUF_SIZE, pFile)) == BUF_SIZE )
		sCount+=BUF_SIZE;
	totalCount=sCount+temp;//加上temp,因为最后一次因循环条件失败而没有执行sCount+=BUF_SIZE
	sCount=0;
	
	if( returnVal == 1 )
	{
		for(int i=0;i<totalCount;i++)
			printf("%c",sourceProgram[i]);
		printf("\n\n");
	}

	sourceProgram[totalCount++]=' ';
	sourceProgram[totalCount++]=' ';//在源程序末尾添加空格符,使词法分析在源程序末尾可以停下来

	return returnVal;
}

//******************************************************************/
//函数原型:void init()
//参数说明:无    
//函数功能:初始化数组等
//返回值  :无
//******************************************************************/
void init()
{
	word[0]="program   ";
	word[1]="integer   ";
	word[2]="logical   ";
	word[3]="if        ";
	word[4]="then      ";
	word[5]="else      ";
	word[6]="while     ";
	word[7]="repeat    ";
	word[8]="begin     ";
	word[9]="end       ";
	word[10]="or        ";
	word[11]="and       ";
	word[12]="not       ";
	word[13]="true      ";
	word[14]="false     ";
	word[15]="do        ";
	word[16]="until     ";
	word[17]="write     ";

	wsym[0]=PROGRAM;
	wsym[1]=INTEGER;
	wsym[2]=LOGICAL;
	wsym[3]=IF;
	wsym[4]=THEN;
	wsym[5]=ELSE;
	wsym[6]=WHILE;
	wsym[7]=REPEAT;
	wsym[8]=BEGIN;
	wsym[9]=END;
	wsym[10]=OR;
	wsym[11]=AND;
	wsym[12]=NOT;
	wsym[13]=TRUE;
	wsym[14]=FALSE;
	wsym[15]=DO;
	wsym[16]=UNTIL;
	wsym[17]=WRITE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -