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

📄 scan.cpp

📁 编译原理课程设计中的词法和语法分析器的设计
💻 CPP
字号:
#include"global.h"
#include"scan.h"
#include<windows.h>
char lexemes[STRMAX];
struct entry symtable[SYMMAX];
int lastentry;
int lastchar=-1;
int SNum=0;/*输入串长度*/
char identifier[MAX];/*定义标识符*/
int  num;/*定义数字*/
int String[MAX]={0};
ifstream fin ("input.txt");/*建立输入文件*/
ofstream fError ("error.txt");/*报错文件*/
ofstream fsymbol ("output.txt");/*token表*/
int isLetter(char c)/*判断是否为字母*/
{
	if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
		return 1;
	return 0;
}
int isDigit(char c)/*判断是否为数字*/
{
	if(c>='0'&&c<='9')
		return 1;
	return 0;
}
int isOpertor(char c)/*判断是否为操作符*/
{
	if(c=='*'||c=='+'||c=='-'||c=='/')
		return 1;
	return 0;
}
int lookup(char s[])/*判断当前标识符是否已存在*/
{
	
    for(int i=0;i<=lastentry;i++)
	{
		if(strcmp(symtable[i].lexptr,s)==0)
			return i;
	}
	return 0;
}
int insert(char s[])/*插入标识符*/
{
	int len;
	len=strlen(s);
	if(lastentry+1>=SYMMAX)
		fError<<"  symbol table is full"<<'\n';/*符号表已满*/
	
	symtable[lastentry].token=lastentry;
    symtable[lastentry].lexptr=&lexemes[lastchar+1];
	lastchar+=len+1;
	strcpy(symtable[lastentry].lexptr,s);
	
	fsymbol<<symtable[lastentry].lexptr<<"\tID\t"<<symtable[lastentry].token<<'\n';
	
	String[SNum]=symtable[lastentry].token+1;
	SNum++;
	lastentry++;
	return lastentry;
}
void init()/*初始化符号表*/
{
	lastentry=0;
	for(int i=0;i<SYMMAX;i++)
	{
		symtable[i].lexptr="\0";
		symtable[i].token=0;
	}
}
void lexan()
{
	int state=0;/*设定初始状态*/
	char c;
	int count;
	while(1)
	{
		fin>>c;
		switch(state)
		{
		case 0:/*start*/
			if(c==' '||c=='\t')                   
				;
			else if(c==';')
				return;
			else if(isLetter(c))
			{
				state=1;
				count=0;
				identifier[count++]=c;
			}
			else if(isDigit(c))
			{
				state=2;
                num=c-48;
			}
			else if(isOpertor(c))
			{
				if(c=='+')
				{
					fsymbol<<c<<"\tPLUS\t"<<'\n';
					String[SNum]=-1;
					SNum++;
				}
				else if(c=='-')
				{
					fsymbol<<c<<"\tSUB\t"<<'\n';
					String[SNum]=-2;
					SNum++;
				}
				else if(c=='*')
				{
					fsymbol<<c<<"\tMUL\t"<<'\n';
					String[SNum]=-3;
					SNum++;
				}
				else
				{
					fsymbol<<c<<"\tDIV\t"<<'\n';
					String[SNum]=-4;
					SNum++;
				}
                  
			}
			else if(c=='=')
			{
				fsymbol<<c<<"\tEQ\t\n";
				String[SNum]=-5;
				SNum++;
			}
			else if(c=='(')
			{
				fsymbol<<c<<"\tLPAR\t\n";
				String[SNum]=-6;
				SNum++;
			}
			else if(c==')')
			{
				fsymbol<<c<<"\tRPAR\t\n";
				String[SNum]=-7;
				SNum++;
				fin>>c;
				if(c==';')
				{
					String[SNum]=-8;
				    return;
				}
				else
					fin.seekg(-1,ios::cur);


			}
			else
				fError<<c<<",not legal symbol"<<'\n';
			break;
		    case 1:/*identifier*/
			if(isLetter(c)||isDigit(c))/*是数字或字母*/
			{
				identifier[count++]=c;
			}
			else if(c==';')/*文件结束*/
			{
				
				int p=lookup(identifier);
				if(p==0)
				{
					insert(identifier);
				}
				String[SNum]=-8;
				return;
			}
            else/*标识符形成*/
			{
				
				int p=lookup(identifier);
				if(p==0)
				{
					insert(identifier);
				}
				for(int i=0;i<count;i++)
				{
					identifier[i]='\0';
				}
				state=0;
				count=0;
				fin.seekg(-1,ios::cur);/*指针后退1*/

			}
			break;
		case 2:/*number*/
			if(isDigit(c))
			{
				state=2;
                num*=10;
				num=num+c-48;
			}
			else if(c==';')
			{
				fsymbol<<num<<"\tNUM\t"<<num<<'\n';
				String[SNum]=-1*num-9;
				SNum++;
				String[SNum]=-8;
				return;
			}
            else
			{
				fin.seekg(-1,ios::cur);/*指针后退1*/
				fsymbol<<num<<"\tNUM\t"<<num<<'\n';
				String[SNum]=-1*num-9;
				SNum++;
				state=0;
			}
			break;
		}
	}
	
    fin.close();
	fsymbol.close();
	fError.close();
}
	

⌨️ 快捷键说明

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