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

📄 cifafenxi.cpp

📁 1.掌握手工生成词法分析器的方法
💻 CPP
字号:
#include <stdio.h>
#include<iostream.h>
#include<process.h>
#include<string.h>
#include<fstream.h>
#define MaxSize 50
typedef struct SqList
{
	char data[MaxSize][10];
	int length;
}SqList;

SqList *init_SqList()//初始化表L
{
	SqList *L;
	L=new SqList;
	if(!L)  exit(1);
	L->length=0;
	return L;
}


SqList *Insert_SqList(SqList *L,int i,char x[])//向表中插入
{
	strcpy(L->data[i-1],x);L->length++;
	return L;
}

int LocateElem_Sq(SqList *L,char x[])//比较字母
{
	int i=1;
	while(i<=L->length&&strcmp(L->data[i-1],x)!=0)
		i++;
	if(i>=L->length)
	{     
		
	
		
		cout<<"(0     "<<x<<")"<<endl;
	}
	
		
	
	else if(i>=33&&i<41)
	{ 
		cout<<"( 34     "<<x<<")"<<endl;
	}else if(i>40)
	{
		cout<<"( 35     "<<x<<")"<<endl;
	}else
		cout<<"("<<i<<"       "<<x<<")"<<endl;
	return 1;
}




void main()
{
 printf("********************说明************************\n");
 printf("1——32为关键字\n");
 printf("32——36为算术运算符\n");
 printf("37——38为关系运算符\n");
 printf("39为界符\n");
 printf("40为赋值运算符\n");
 printf("41——50为常数\n");
 printf("************************************************\n");
 printf("\n\n\n");

char m[50];char s,temp[20];
SqList *L=init_SqList();

L=Insert_SqList(L,1,"while");
L=Insert_SqList(L,2,"if");
L=Insert_SqList(L,3,"auto");
L=Insert_SqList(L,4,"double");
L=Insert_SqList(L,5,"int");
L=Insert_SqList(L,6,"struct");
L=Insert_SqList(L,7,"break");
L=Insert_SqList(L,8,"else");
L=Insert_SqList(L,9,"long");
L=Insert_SqList(L,10,"switch");
L=Insert_SqList(L,11,"case");
L=Insert_SqList(L,12,"enum");
L=Insert_SqList(L,13,"register");
L=Insert_SqList(L,14,"typedef");
L=Insert_SqList(L,15,"char");
L=Insert_SqList(L,16,"extern");
L=Insert_SqList(L,17,"return");
L=Insert_SqList(L,18,"union");
L=Insert_SqList(L,19,"const");
L=Insert_SqList(L,20,"float");
L=Insert_SqList(L,21,"short");
L=Insert_SqList(L,22,"unsigned");
L=Insert_SqList(L,23,"continue");
L=Insert_SqList(L,24,"for");
L=Insert_SqList(L,25,"signed");
L=Insert_SqList(L,26,"void");
L=Insert_SqList(L,27,"default");
L=Insert_SqList(L,28,"goto");
L=Insert_SqList(L,29,"sizeof");
L=Insert_SqList(L,30,"volatile");
L=Insert_SqList(L,31,"do");
L=Insert_SqList(L,32,"static");
L=Insert_SqList(L,33,"+");
L=Insert_SqList(L,34,"-");
L=Insert_SqList(L,35,"*");
L=Insert_SqList(L,36,"/");
L=Insert_SqList(L,37,"<");
L=Insert_SqList(L,38,">");
L=Insert_SqList(L,39,";");
L=Insert_SqList(L,40,"=");
L=Insert_SqList(L,41,"0");
L=Insert_SqList(L,42,"1");
L=Insert_SqList(L,43,"2");
L=Insert_SqList(L,44,"3");
L=Insert_SqList(L,45,"4");
L=Insert_SqList(L,46,"5");
L=Insert_SqList(L,47,"6");
L=Insert_SqList(L,48,"7");
L=Insert_SqList(L,49,"8");
L=Insert_SqList(L,50,"9");



ifstream infile;
infile.open("源程序.txt");
infile.getline(m,100,'*');

int j=0;int size=strlen(m);
for(int i=0;i<=size;i++)
{
	s=m[i];
	if(m[i]=='\0')
	{
		temp[j]='\0';j=0;
		LocateElem_Sq(L, temp);
	}
	else if(s!=' ')
	{
	    if(s=='\n')
		{
          
		}else
		{
		temp[j]=s;
		j++; 
		}
		
	}
	else 
	{
		temp[j]='\0';j=0;
		LocateElem_Sq(L, temp);
		


	}

	
	
}

}

⌨️ 快捷键说明

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