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

📄 form1.cs

📁 PL0语法分析器
💻 CS
📖 第 1 页 / 共 2 页
字号:
					if (a==b)
						return true;
				}
				return false;
			}//是否是基本字
			public bool IsOp(string a)
			{
				foreach(string b in optxt)
				{
					if (a==b)
						return true;
				}
				return false;
			}//是否是运算符
			public bool IsEop(string a)
			{
				foreach(string b in eoptxt)
				{
					if (a==b)
						return true;
				}
				return false;
			}//是否是界符
			public void IntoBuffer(string a)
			{
				this.keybuffer=a;
			}//文本框里的字符串输入缓冲区
			public void WordAnalyze()
			{
				int n=0;//n为字符串中的当前位置
				
				while(n<this.keybuffer.Length) //控制分析的字符个数
				{
					bool DoFinishWork=false;
					if(this.keybuffer.Substring(n,1)==" ") 
					{
						n++;
						continue;
					}//删除前面多余空格

					foreach(string a in this.keytxt)//开始分析是否是基本字
					{
						if((n+a.Length)>this.keybuffer.Length) continue;
						string b=this.keybuffer.Substring(n,a.Length);
						if(this.IsKey(b))
						{
							if((n+b.Length)<=(this.keybuffer.Length-1))
							{
								char r=this.keybuffer[n+b.Length];
								if((r>='a'&&r<='z')||(r>='A'&&r<='Z')||(r>='0'&&r<='9'))
									continue;
								else
								{
									int m=0,mm=0;//m为for中循环变量,mm为这个基本字在keytxt中是第几个
									for(;m<12;m++)
									{
										if (b==keytxt[m])
											mm=m+1;;
									}
									string result="("+"1"+","+mm+")";
									this.ResultList.Add(result);
									n=n+b.Length;
									DoFinishWork=true;
									break;
								} 
							}
							else
							{
								int m=0,mm=0;//m为for中循环变量,mm为这个基本字在keytxt中是第几个
								for(;m<12;m++)
								{
									if (b==keytxt[m])
										mm=m+1;
								}
								string result="("+"1"+","+mm+")";
								this.ResultList.Add(result); 
								n=n+b.Length;
								DoFinishWork=true;
								break;
							}
						}
					}
					if(DoFinishWork) continue; //分析基本字结束

					foreach(string a in this.optxt)//开始分析是否是运算符
					{
						if((n+a.Length)>this.keybuffer.Length) continue;
						string b=this.keybuffer.Substring(n,a.Length);
						if(this.IsOp(b))
						{
							int m=0,mm=0;//m为for中循环变量,mm为这个基本字在optxt中是第几个
							for(;m<8;m++)
							{
								if (b==optxt[m])
									mm=m+1;
							}
							string result="("+"2"+","+mm+")";
							this.ResultList.Add(result); n=n+b.Length;
							DoFinishWork=true;
						}
					}
					if(DoFinishWork) continue; //分析运算符结束

					string number=""; //开始分析常数
					if(n>=this.keybuffer.Length) continue; 
					for(char t=this.keybuffer[n];t>='0'&&t<='9'&&(n<this.keybuffer.Length);)
					{
						number +=this.keybuffer.Substring(n,1);
						n++;
						if(n<this.keybuffer.Length)
							t=this.keybuffer[n];
						else
							t='+';//t随便赋一个值使其不在0-9中
					}
					if (number!="")
					{
						int NumId=0;//常数存放区里常数的编号
						int m=0;//这个常数在NumList有,m=1,没有则m=0
						IEnumerator enu1 =this.NumList.GetEnumerator();
						while(enu1.MoveNext())
						{
							NumId++;
							if (number==enu1.Current.ToString())
							{
								m=1;
								break;
							}
						}
						if (m==0)
						{
							this.NumList.Add(number);
							NumId++;
							ResultNum=ResultNum+NumId+"、'"+number+"' ";
						}
						string result="("+"4"+","+NumId+")";
						this.ResultList.Add(result);
						DoFinishWork=true;
					}
					if(DoFinishWork) continue; //分析常数结束

					foreach(string a in eoptxt)//开始分析是否为界符
					{
						if((n+a.Length)>this.keybuffer.Length) continue;
						string b=keybuffer.Substring(n,a.Length);
						if(this.IsEop(b))
						{
							int m=0,mm=0;//m为for中循环变量,mm为这个基本字在eoptxt中是第几个
							for(;m<8;m++)
							{
								if (b==eoptxt[m])
									mm=m+1;
							}
							string result="("+"5"+","+mm+")";
							this.ResultList.Add(result); 
							n=n+b.Length;
							DoFinishWork=true;
							break;
						}      
					}
					if(DoFinishWork) continue;//分析界符结束

					string identification=""; //开始分析标示符
					if(n>=this.keybuffer.Length) continue;
					char z=this.keybuffer[n];//z为标示符第一个字母,判断z来判断是不是标示符
					if((z>='a'&&z<='z')||(z>='A'&&z<='Z'))
					{
						identification +=z;
						n++;
						if(n<this.keybuffer.Length)
						{
							for(char t=this.keybuffer[n];((t>='a'&&t<='z')||(t>='A'&&t<='Z')||(t>='0'&&t<='9'))&&(n<this.keybuffer.Length);)
							{
								identification+=this.keybuffer.Substring(n,1);
								n++;
								if(n<this.keybuffer.Length)
									t=this.keybuffer[n];
								else
									t='+';//t随便赋一个值使其不在0-9,a-z,A-Z中
							}
						}
						
						int IdId=0;//标识符存放区里标识符的编号
						int m=0;//这个标识符在IdList有,m=1,没有则m=0
						IEnumerator enu1 =this.IdList.GetEnumerator();
						while(enu1.MoveNext())
						{
							IdId++;
							if (identification==enu1.Current.ToString())
							{
								m=1;
								break;
							}
						}
						if (m==0)
						{
							this.IdList.Add(identification);
							IdId++;
							ResultId=ResultId+IdId+"、'"+identification+"' ";
						}
						string result="("+"3"+","+IdId+")";
						this.ResultList.Add(result);
						DoFinishWork=true;
					}
					if(DoFinishWork) continue; //分析标示符结束
					
					if(n>=this.keybuffer.Length) continue;//开始过滤无用字符包括回车
					char s=this.keybuffer[n];
					string ss;//ss为s的string形式
					if(!(((int)s==35)||((int)s>=40&&(int)s<=62)||((int)s>=65&&(int)s<=91)||(int)s==93||((int)s>=97&&(int)s<=122)))
					{
						if ((int)s==13)
							n=n+2;
						else
						{
							n++;
							ss=s.ToString();
							string result="("+"6"+","+ss+")";
							this.ResultList.Add(result);
							DoFinishWork=true;
						}

					}
					if(DoFinishWork) continue;//过滤结束
				}
			}//词法分析
			public string Analyze(string a)
			{
				int n=a.Length;
				int op=0;//括号的偏正量
				for(int i=0;i<n;i=i=i+4)
				{
					if(a[i]=='1'||a[i]=='6')
						return "你的表达式中有错误的字符";
					else if (a[i]=='2')
					{
						if (a[i+2]=='5'||a[i+2]=='6'||a[i+2]=='7'||a[i+2]=='8')
							return "你的表达式中有错误的字符";
						else if (a[i]=='5')
							if (!(a[i+2]=='1')||!(a[i+2]=='2'))
								return "你的表达式中有错误的字符";
					}
				}
				for(int i=0;i<n;i=i=i+4)
				{
					if(i<1)
					{
						if((a[i]=='2'&&(a[i+2]=='1'||a[i+2]=='2'))||a[i]=='3'||a[i]=='4'||(a[i]=='5'&&a[i+2]=='1'))
						{
							if (a[i]=='5'&&a[i+2]=='1')
								op++;
						}
						else return "你的表达式格式不对";
					}
					else
					{
						if(a[i-4]=='3'||a[i-4]=='4')
						{
							if((a[i]=='5'&&a[i+2]=='2')&&op>=0)
								op--;
							else if(a[i]!='2')
								return "你的表达式格式不对";
						}
						if(a[i-4]=='2')
						{
							if(a[i]=='5'&&a[i+2]=='1')
								op++;
							else if (a[i]!='3'&&a[i]!='4')
								return "你的表达式格式不对";
						}
						if(a[i-4]=='5'&&a[i-2]=='1')
						{
							if(a[i]=='5'&&a[i+2]=='1')
								op++;
							else if(a[i]!='3'&&a[i]!='4')
								return "你的表达式格式不对";
						}
						if(a[i-4]=='5'&&a[i-2]=='2')
						{
							if ((a[i]=='5'&&a[i+2]=='2')&&op>=0)
								op--;
							else if (a[i]!='2')
								return "你的表达式格式不对";
						}
					}
				}
				if(a[n-4]=='2')
					return "表达式的最后不应该为运算符";
				if (op!=0)
					return "左括号与右括号数量不相等";
				return "表达式正确";
			}
		}
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ListBox List;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;


		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
			
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			List.Items.Clear();
			KeyAnalyze spl =new KeyAnalyze();
			spl.IntoBuffer(textBox1.Text);
			spl.WordAnalyze();
			IEnumerator enu =spl.ResultList.GetEnumerator();
			string cc="";
			while(enu.MoveNext())
			{
				List.Items.Add(enu.Current);//一个一个读出结果
				cc +=enu.Current.ToString().Substring(1,3);
				cc+=",";
			}
			textBox2.Text=spl.ResultId;
			textBox3.Text=spl.ResultNum;
			textBox4.Text=spl.Analyze(cc);
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			textBox1.Clear();
		}
	}
}

⌨️ 快捷键说明

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