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

📄 main.cpp

📁 词法分析程序。可实现对C语言的词法分析,关键字32个,包含了C的绝大部分运算、限界符,主要是对文件进行读、写操作,节省内存消耗,是一个不错的词法分析程序。
💻 CPP
字号:
//***********Author: Wei Peidong.
//***********Computer Science Class 1 Grade 03.
//***********Student Number:200345003090 38.

#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;


ifstream infile;//读原始文件
fstream newfile;//拆单词后写入的文件
ifstream oldfile;//读取删除/**/内容的文件
fstream infile2;//读取拆单词后的文件
fstream outfile2;//删除/**/内容后写入


char *filename=new char;

string key[35]={"and","array","begin","bool","call",
"case","char","constant","dim","do",
"else","end","false","for","if",
"input","integer","not","of","or",
"output","procedure","program","read","real",
"repeat","set","stop","then","to",
"true","until","var","while","write"};

string symb[22]={"(",")","*","*/","+",",","-",".","..","/","/*",
":",":=",";","<","<=","<>","=",">",">=","[","]"};

bool issymb(char x)
{
	bool flag=false;
	for(int i=0;i<22;i++)
	{
		if(symb[i][0]==x)flag=true;
	}
	return flag;
}


string number[10]={"0","1","2","3","4","5","6","7","8","9"};
bool isnum(char x)
{ 
	bool flag=false;
	for(int i=0;i<10;i++)
	{
		if(number[i][0]==x)flag=true;
	}
	return flag;
}

string alph[52]={"Q","W","R","T","Y","U","I","O","P","L","K","J","H",
"G","F","D","S","A","Z","X","C","V","B",
"N","M","q","w","e","r","t","y","u","i","o","p","l",
"k","j","h","g","f","d","s","a","z","x","c","v","b","n","m"};

bool isalph(char x)
{
	bool flag=false;
	for(int i=0;i<52;i++)
	{
		if(alph[i][0]==x)flag=true;
	}
	return flag;
}

void trans1()
{
	cout<<"please input file name:"<<endl;
	
	cin>>filename;
	if(fopen(filename,"r")!=NULL)
//	if(true)
	{
		infile.open(filename,ios::in);

		newfile.open("temp1.txt",ios::out);

		char buf1='a';//存放临时字符
		char buf2;//存放临时字符2
		infile.get(buf2);

		while(!infile.eof())
		{
			

			if(!issymb(buf1)&&!issymb(buf2))newfile.put(buf2);
			else
			{
				if((buf1=='<'&&buf2=='>')||(buf1=='<'&&buf2=='=')||(buf1=='>'&&buf2=='=')||(buf1==':'&&buf2=='=')||(buf1=='/'&&buf2=='*')||(buf1=='*'&&buf2=='/')||(buf1=='.'&&buf2=='.'))
				{
					newfile.put(buf2);
					newfile.put(' ');
				}else
				{
					newfile.put(' ');
					newfile.put(buf2);
				}
			}
			buf1=buf2;			
	    	infile.get(buf2);
		}
		
//		newfile.seekp(-1,ios::end);
//		newfile.write(" ",1);
		infile.close();
		newfile.close();
	}else
		cout<<"no this file."<<endl;
	return;
}

void trans2()
{
	infile2.open("temp1.txt",ios::in);
	outfile2.open("temp2.txt",ios::out);	
	string x;
	bool flag=false;
	while(!infile2.eof())
	{
		infile2>>x;	
     	if(x=="/*")
    	{
			flag=true;
	    	while(x!="*/"&&!infile2.eof())infile2>>x;
			if(x=="*/")flag=false;
	    	infile2>>x;
		}
		if(flag){cout<<"no maching for /*."<<endl;abort();}
		outfile2<<" "<<x;
	}

	infile2.close();
	outfile2.close();
	

}
bool iserrors()//有错误时返回true
{
	newfile.open("temp1.txt",ios::in);
	string x=new char;
	bool flag;//假设该词错误。
	bool t;
	while(!newfile.eof())
	{
		flag=true;//假设该词错误。
		int i;
		newfile>>x;
	
		for(i=0;i<22;i++)if(symb[i]==x){flag=false;continue;}//界符
		for(i=0;i<35;i++)if(key[i]==x){flag=false;continue;}//保留字
		if(x.substr(0,1)=="'"&&x.substr(x.length()-1,1)=="'"){flag=false;continue;}//字符串
		if(isalph(x[0]))//表示符
		{
			t=true;
			for(i=1;i<x.length();i++){if(!(isnum(x[i])||isalph(x[i])))t=false;}
			if(t==true){flag=false;continue;}
		}
		
		if(isnum(x[0]))//整数
		{
			t=true;
			for(i=1;i<x.length();i++){if(!isnum(x[i]))t=false;}
			if(t==true){flag=false;continue;}
		}
		if(flag==true)
		{
			cout<<"something wrong with this word:"<<endl;
    		cout<<x<<endl;
		}		
	}
	newfile.close();
/*
    newfile.open(newfilename,ios::in);

	newfile.seekg(0,ios::beg);
	if(newfile.eof())cout<<"eof."<<endl;
	while(!newfile.eof())
	{
		newfile>>x;
		cout<<x<<" ";
	}
*/
	return flag;
}

struct word //存放变量和常量的链表
{
	string name;
	word *next;
	int num;
};
struct intnum//存放整数
{
	int name;
	intnum *next;
	int num;
};
word *w1=NULL;//变量
word *w3=NULL;//字符串
word *w2=NULL;
intnum *i2=NULL;//数字
int var=0;

word * insert(word *w,string x)//把变量存入链表
{
	word *p=w;
	if(p==NULL)
	{ 
		w=new word;
		word *s=new word;
    	s->name=x;
    	s->next=NULL;
    	var++;
    	s->num=var;		
		w=s;
		return(w);
	}
	while(p->next)
	{
		if(p->name==x)return(w);
		p=p->next;
	}

	word *s=new word;
	s->name=x;
	s->next=NULL;
	var++;
	s->num=var;
	if(p==NULL)
	{
		p=s;
	}
	else
	{
		p->next=s;
	}
	return(w);
}
/*
intnum *insert(intnum *w,int x)//把变量存入链表
{
	intnum *p=w;
	if(p==NULL)
	{ 
		w=new intnum;
		intnum *s=new intnum;
    	s->name=x;
    	s->next=NULL;
    	var++;
    	s->num=var;		
		w=s;
		return(w);
	}
	while(p->next)
	{
		if(p->name==x)return(w);
		p=p->next;
	}

	intnum *s=new intnum;
	s->name=x;
	s->next=NULL;
	var++;
	s->num=var;
	if(p==NULL)
	{
		p=s;
	}
	else
	{
		p->next=s;
	}
	return(w);
}
*/
/*
void printlink(word *w)
{ 
	word *p=w;
	if(p==NULL){cout<<"no link."<<endl;return;}
	while(p)
	{
		cout<<p->name<<"\t"<<p->num<<endl;
		p=p->next;
	}
}
*/
int n=5,nn=0;
void print1(string x,int i)
{
	cout<<"("<<i<<",-)"<<"\t";
	nn++;
	if(nn%n==0)cout<<endl;
}
void print2(string x,word *w)
{
	word *p=w;
	while(p->name!=x)p=p->next;
	cout<<"(36,"<<p->num<<")"<<"\t";
	nn++;
	if(nn%n==0)cout<<endl;
}
/*
void print3(int x,intnum *i)
{
	intnum *p=i;
	while(p->name!=x)p=p->next;
	cout<<"(37,"<<p->num<<")"<<"\t";
	nn++;
	if(nn%n==0)cout<<endl;
}
*/

void print3(string x,word *w)
{
	word *p=w;
	while(p->name!=x)p=p->next;
	cout<<"(37,"<<p->num<<")"<<"\t";
	nn++;
	if(nn%n==0)cout<<endl;
}

void print4(string x,word *w)
{
	word *p=w;
	while(p->name!=x)p=p->next;
	cout<<"(38,"<<p->num<<")"<<"\t";
	nn++;
	if(nn%n==0)cout<<endl;
}

void main()
{
	cout<<"Author: Wei Peidong."<<endl;
	cout<<"Computer Science Class 1 Grade 03."<<endl;
	cout<<"Student Number:200345003090 38."<<endl<<endl;
		

	trans1();
	trans2();
	bool errors=iserrors();	

	if(!errors)
	{
		oldfile.open("temp2.txt",ios::in);
		string x;
		int i;
		while(!oldfile.eof())
		{
			bool flag=false;//true表示已输出
			oldfile>>x;
			for(i=0;i<22;i++)
			{
				if(symb[i]==x)
				{print1(x,i+39);flag=true;break;}
			}//界符
			if(flag)continue;

			for(i=0;i<35;i++)
			{
				if(key[i]==x)
				{print1(x,i+1);flag=true;break;}
			}//保留字
			if(flag)continue;

			if(x.substr(0,1)=="'")//字符串
			{
				w3=insert(w3,x);
				print4(x,w3);flag=true;	
			}
			if(flag)continue;

			if(isnum(x[0]))//整数
			{
//				i2=insert(i2,x);
//				print3(x,i2);flag=true;
				w2=insert(w2,x);
				print3(x,w2);flag=true;
			}
			if(flag)continue;

			if(isalph(x[0]))//表示符
			{
				w1=insert(w1,x);
				print2(x,w1);flag=true;
			}
			if(flag)continue;
		}
		newfile.close();
	}
//	printlink(w1);
}

⌨️ 快捷键说明

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