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

📄 cifafenxiqi.cpp

📁 c语言词法分析器
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>		//用于字符处理
#include<iostream>
#define MAX1  900		//字符串最大长度

using namespace std;

char mystring[MAX1];	//字符串
int top=0;				
int lastentry=1;		//最后进入字符表的address
int lastok=0;
int lastdigit=0;

//定义编码表的数据结构
struct symbolrecord
{
	char symbol[10];
	int  id;
};
struct symbolrecord bianma[999];

//定义符号表数据结构
struct entrytype
{
	char idname[10];
	int  address;
	char type[10];
};
struct entrytype fuhao[999];

//Token字数据结构
struct tokentype
{
	int id;
	int entry;
};
struct tokentype token[999];

struct digittype
{
	int value;
	int entry;
	char type[10];
};
struct digittype digit[100];

//函数声明:
void init(void);
void scanner(void);
void sort(char ch);
void recogid(char ch);
void handlecom(char ch);
void recogdig(char ch);
void recogstr(char ch);
void recogdel(char ch);
void output(void);

//扫描函数
void scanner(void)

{
	FILE *fp;
	char ch;
	int i;
	fp=fopen("jiance.txt","r");
	if(fp==NULL)
		cout<<"file can't be opened!"<<endl;
	i=0;
	while(!feof(fp))		//当文件未结束读入字符
	{
		ch=fgetc(fp);
		if(ch!=EOF)
		{
			mystring[i]=ch;
			i++;
		}
	}
		mystring[i]='\0';
	do
	{
		ch=mystring[top];
		top++;
		sort(ch);
	}while(ch!='\0');

	fclose(fp);
	return;
}

//初始化编码表	

void init(void)
{
	strcpy(bianma[0].symbol,"func");		bianma[0].id=1;
	strcpy(bianma[1].symbol,"while");		bianma[1].id=2;
	strcpy(bianma[2].symbol,"do");			bianma[2].id=3;
	strcpy(bianma[3].symbol,"if");			bianma[3].id=4;
	strcpy(bianma[4].symbol,"then");		bianma[4].id=5;
	strcpy(bianma[5].symbol,"else");		bianma[5].id=6;
	strcpy(bianma[6].symbol,"true");		bianma[6].id=7;
	strcpy(bianma[7].symbol,"false");		bianma[7].id=8;
	strcpy(bianma[8].symbol,"int");			bianma[8].id=9;
	strcpy(bianma[9].symbol,"const");		bianma[9].id=10;
	return ;
}

//单词分类
void sort(char ch)
{
	
		if(isalpha(ch))
			recogid(ch);
		else if(isdigit(ch))
			recogdig(ch);
		else
			recogdel(ch);
	
	return;
}

//插入符号表
void insert(char *name,int addr,char *type)
{
	strcpy(fuhao[lastentry].idname,name);
	fuhao[lastentry].address=addr;
	strcpy(fuhao[lastentry].type,type);
	lastentry++;
	return;
}

//识别标识符
void recogid(char ch)
{
	char word[66];
	char s=ch;
	int i=0;
	int j,t;
	int node1=0,node2=0;

	word[i]=ch;
	
	while(isalpha(s)||isdigit(s))
	{
		s=mystring[top];
		top++;
		if(isalpha(s)||isdigit(s))
		{
			i++;
			word[i]=s;
		}	
	}
	top--;
	word[i+1]='\0';

	//判断是否为关键字
	for(j=0;j<=9;j++)
	{
		int p=strcmp(bianma[j].symbol,word);
		if(p==0)
		{
			token[lastok].id=bianma[j].id;
			token[lastok].entry=-1;
			lastok++;
			node1=1;
			break;
		}
	}
	if(node1==0)
		{
			for(t=1;t<=lastentry;t++)
			{	
				if(strcmp(fuhao[t].idname,word)==0)
				{
					token[lastok].id=11;
					token[lastok].entry=fuhao[t].address;
					node2=1;
					break;
				}
			}
			if(node2==0)
			{
				insert(word,lastentry,"id");						
				token[lastok].id=11;
				token[lastok].entry=fuhao[lastentry-1].address;
			}
			lastok++;	
		}
}

//识别数字常数
void recogdig(char ch)
{
	char ch1=ch;
	int sum=ch1-'0';
	do
	{
		ch1=mystring[top++];
		if(isdigit(ch1))
			sum= sum*10+ch1-'0';
	}while(isdigit(ch1));
	top--;
	token[lastok].id=10;
	token[lastok].entry=lastdigit;
	lastok++;
	digit[lastdigit].value=sum;
	digit[lastdigit].entry=lastdigit;
	strcpy(digit[lastdigit].type,"const");
	lastdigit++;
	return;
}

//识别界限符
void recogdel(char ch)
{
	char ch1;
	ch1=mystring[top];
	switch(ch)
	{
		case '+':
			token[lastok].id=26;
			token[lastok].entry=-1;
			lastok++;
			break;
		case '*':
			token[lastok].id=27;
			token[lastok].entry=-1;
			lastok++;
			break;
		case '(':
			token[lastok].id=28;
			token[lastok].entry=-1;
			lastok++;
			break;
		case ')':
			token[lastok].id=29;
			token[lastok].entry=-1;
			lastok++;
			break;
		case '{':
			token[lastok].id=12;
			token[lastok].entry=-1;
			lastok++;
			break;
		case '}':
			token[lastok].id=13;
			token[lastok].entry=-1;
			lastok++;
			break;
		case ';':
			token[lastok].id=14;
			token[lastok].entry=-1;
			lastok++;
			break;
		case ',':
			token[lastok].id=15;
			token[lastok].entry=-1;
			lastok++;
			break;
		case '=':
			if(ch1!='=')
			{
				token[lastok].id=16;
				token[lastok].entry=-1;
				lastok++;
			}
			else
			{
				token[lastok].id=25;
				token[lastok].entry=-1;
				lastok++;
				top++;
			}
				break;
		case '!':
			token[lastok].id=17;
			token[lastok].entry=-1;
			lastok++;
			break;
		case '&':
			token[lastok].id=18;
			token[lastok].entry=-1;
			lastok++;
			break;
		case '|':
			token[lastok].id=19;
			token[lastok].entry=-1;
			lastok++;
			break;
		case '<':
			if(ch1=='=')
			{
				token[lastok].id=22;
				token[lastok].entry=-1;
				top++;
			}
			else if(ch1=='>')
			{
				token[lastok].id=24;
				token[lastok].entry=-1;
				top++;
			}
			else
			{
				token[lastok].id=20;
				token[lastok].entry=-1;
			}
			lastok++;
			break;
		case '>':
			if(ch1=='=')
			{
				token[lastok].id=23;
				token[lastok].entry=-1;
				top++;
			}
			else 
			{
				token[lastok].id=21;
				token[lastok].entry=-1;
			}
			lastok++;
			break;
		default:
			break;
	}
	return;
}

//写入并输出文件
void output(void)
{
	int i;
	FILE *ffuhao,*ftok,*fdigit;

	ffuhao=fopen("fuhao.txt","w");
	cout<<"输出符号表"<<endl;
	for(i=1; i<lastentry;i++)
	{
		printf("%s  %d  %s\n",fuhao[i].idname,fuhao[i].address,fuhao[i].type);
		fprintf(ffuhao,"%8s%8d\n",fuhao[i].idname,fuhao[i].address);
	}
	fclose(ffuhao);
	
	ftok=fopen("ftok.txt","w");
	cout<<"数出头根字"<<endl;
	for(i=0;i<lastok;i++)
	{
		printf("%d  %d\n",token[i].id,token[i].entry);
		fprintf(ftok,"%3d%3d\n",token[i].id,token[i].entry);
	}
	fclose(ftok);

	
	if((fdigit=fopen("digit.txt","w"))==NULL)
		printf("file4 cannot open\n");
	for(i=0; i<lastdigit;i++)
	{
		printf("%d  %d  %s\n",digit[i].entry,digit[i].value,digit[i].type);
	    fprintf(fdigit,"%8d%8d%8s\n",digit[i].entry,digit[i].value,digit[i].type);
	}
	fclose(fdigit);

	return;
}

//主函数
void main()
{
 	init();
	scanner();
	output();
	return;
}

⌨️ 快捷键说明

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