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

📄 词法分析.cpp

📁 吃法分析程序
💻 CPP
字号:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ofstream fout("out.txt");
string SYM[255],ID[255],NUM[255];
int _i=0,_j=0,_k=0;
//pointer of SYM[],ID[]&NUM[].

string Kwords[]={"CONST","VAR","procedure","begin","end","ood","if","then","call","while","do","read","write"};
string BSymbols[]={",","(",")","{","}",";","."};
string CSymbols[]={":=",">=","<=","=","+","-","*","/","#","<",">"};
int min(int a,int b)
{
    if (a<=b)return a;else return b;
}
bool isKeyWord(string word)
{
    bool flag=false;
    for (int i=0;(i<13);i++)
     if (word==Kwords[i]) flag=true;
    return flag;
}
bool isBorderSymbol(string sym)
{
    bool flag=false;
    for (int i=0;i<7;i++) 
		if (sym==BSymbols[i])
		{
			flag=true;
			break;
		}
    return flag;
}
bool isCalculateSymbol(string sym)
{
    bool flag=false;
    for (int i=0;i<11;i++) 
		if (sym==CSymbols[i])
		{
			flag=true;
			break;
		}
    return flag;
}
bool isDigit(char ch)
{
    if(ch>=0x30 && ch<=0x39) return true;
	else return false;
}
bool isLetter(char ch)
{
    if((ch>=0x61 && ch<=0x7a)||(ch>=0x41&&ch<=0x5a))
		return true;
	else return false;
}
bool isBSymbol(char ch)
{
    return(ch==','||ch=='('||ch==')'||ch=='{'||ch=='}'||ch==';'||ch=='.');
}
bool isCSymbol(char ch)
{
    return(ch==':'||ch=='>'||ch=='<'||ch=='='||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='#');
}
int Typeof(char w)
{
    if(isDigit(w)) return 0;
    else if(isLetter(w)) return 1;
    else if(isBSymbol(w)) return 2;
    else if(isCSymbol(w)) return 3;
    else return 4;
}

class FileReader
{
   private:
       ifstream inpfile;
   public:
       FileReader()
	   {
           inpfile.open("in.txt");
       }
       ~FileReader()
	   {
           inpfile.close();
       }
       char peekNextChar()
	   {
           if (!inpfile.eof())
		   {
			   char p=inpfile.peek();
			   return p;}
               else return 0;
	   }
       char getNextChar()
	   {
           if (!inpfile.eof())
		   {
			   char p=inpfile.get();
               return p;}
               else return 0;
	   } 
        bool eof()
		{
           return inpfile.eof();
        }
};

char getch(FileReader* inp)
{
    return inp->getNextChar();
}
char peekch(FileReader* inp)
{
    return inp->peekNextChar();
}
int main()
{
    FileReader f;
    char buff[10];
    int pos=0;
    while(!f.eof())
	{
       for (int i=0;i<10;i++)buff[i]=0;
       pos=0;
       buff[pos++]=getch(&f);
       while ((Typeof(peekch(&f))==Typeof(buff[0]))||((Typeof(buff[0])==1)&&(Typeof(peekch(&f))==0)))
		   buff[pos++]=getch(&f);
       switch(Typeof(buff[0]))
	   {
           case 0:
			   fout<<buff<<"\t数字"<<endl;
			   break;
           case 1:
			   if (isKeyWord(buff))
			   {
				   fout<<buff<<"\t关键字"<<endl;
			   }
			   else
			   {
				   fout<<buff<<"\t标识符"<<endl;
			   }
                   break;
           case 2:
			   if (isBorderSymbol(buff))
			   {
				   fout<<buff<<"\t界限符"<<endl;
			   }
               else fout<<buff<<"\t-------------ERROR"<<endl;
               break;
           case 3:
			   if (isCalculateSymbol(buff))
			   {
				   fout<<buff<<"\t运算符"<<endl;
			   }
               else fout<<buff<<"\t-------------ERROR"<<endl;
               break;
	   }
	}
	return 0;
}

⌨️ 快捷键说明

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