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

📄 bianyi1.cpp

📁 词法分析的一些程序进攻参考,呵呵,但是希望大家能够提出宝贵的意见啊,一定能够改进
💻 CPP
字号:
#include <iostream>
#include<string>
using namespace std;
char* typeword[5]={"DIM","IF","DO","STOP","END"};
string ch;
void main()
{
	string ch;
	string str;
	int k,i,j;
    char* temp1=new char[];
	cout<<"Enter a string:"<<endl;
	getline(cin,ch);
	for(i=0;i<ch.length();i++)
	{
		j=0;
		if(ch[i]==' ')
		{}
	    else if(ch[i]>='a'&& ch[i]<='z'||ch[i]>='A'&&ch[i]<='Z')
		{
			do
			{
			   temp1[j]=ch[i];
			   j++;
			   i++;
			}
			while(ch[i]>='a'&& ch[i]<='z'||ch[i]>='A'&& ch[i]<='Z'||ch[i]>='0'&& ch[i]<='9');
			i--;
			str=temp1;
			for(k=0;k<5;k++)
			{
			    if(str.compare(0,j,typeword[k])==0)
				{
                   cout<<"("<<k+1<<","<<typeword[k]<<")"<<endl;
				   break;
				}
				else if(k==5)
				{
                   cout<<"("<<"6"<<",";
				   for(int s=0;s<j;s++)
                      cout<<temp1[s];
				   cout<<")"<<endl;
				}
			}
		}
		 else if(ch[i]>='0' && ch[i]<='9')
			 {
				 do
				 {
					 temp1[j]=ch[i];
					 i++;
					 j++;
				 }
				 while(ch[i]>='0'&& ch[i]<='9');
				 i--;
                  cout<<"("<<"7"<<",";
				   for(int s=0;s<j;s++)
                      cout<<temp1[s];
				   cout<<")"<<endl;
			 }
		 else
		 {
			 switch(ch[i])
			 {
			     case'=':
				 cout<<"("<<"8"<<","<<"="<<")"<<endl;
				 break;
				 case'+':
					 cout<<"("<<"9"<<","<<"+"<<")"<<endl;
					 break;
				 case'*':
					 if(ch[i+1]!='*')
					 {
						 cout<<"("<<"10"<<","<<"*"<<")"<<endl;
					      break;
					 }
					 else
					 {
						 cout<<"("<<"11"<<","<<"**"<<")"<<endl;
                         i++;
						 break;
					 }
				 case',':
					 cout<<"("<<"12"<<","<<","<<")"<<endl;
					 break;
				 case'(':
					 cout<<"("<<"13"<<","<<"("<<")"<<endl;
					 break;
				 case')':
					 cout<<"("<<"14"<<","<<")"<<")"<<endl;
					 break;
				 default:
					 cout<<ch[i]<<"error"<<endl;
			 }
		 }
	}
   delete [] temp1;
}

⌨️ 快捷键说明

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