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

📄 form1.cs

📁 编译原理 词法分析器 识别标识符
💻 CS
📖 第 1 页 / 共 4 页
字号:
			this.错误提示.ForeColor = System.Drawing.Color.Red;
			this.错误提示.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.错误提示.MappingName = "";
			this.错误提示.PreferredColumnWidth = 187;
			this.错误提示.RowHeadersVisible = false;
			this.错误提示.RowHeaderWidth = 150;
			// 
			// 错误单词
			// 
			this.错误单词.DataGrid = this.dataGrid_error;
			this.错误单词.ForeColor = System.Drawing.Color.Red;
			this.错误单词.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.错误单词.MappingName = "";
			this.错误单词.PreferredColumnWidth = 50;
			// 
			// 行
			// 
			this.行.DataGrid = this.dataGrid_error;
			this.行.ForeColor = System.Drawing.Color.Red;
			this.行.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.行.MappingName = "";
			this.行.PreferredColumnWidth = 25;
			// 
			// 列
			// 
			this.列.DataGrid = this.dataGrid_error;
			this.列.ForeColor = System.Drawing.Color.Red;
			this.列.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.列.MappingName = "";
			this.列.PreferredColumnWidth = 25;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(784, 580);
			this.Controls.Add(this.dataGrid_error);
			this.Controls.Add(this.dataGrid_token);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.toolBar1);
			this.Controls.Add(this.richTextBox1);
			this.Cursor = System.Windows.Forms.Cursors.Hand;
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid_token)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid_error)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		#region 将新归类的Token字添加到结构体数组,并使结构体数组容量增1
		private void oneArrayToTwo(ref words[] twoArray,ref int j)
		{               	
		    words[] tempArray = twoArray;

			twoArray = new words[j+2];
			for(int x=0;x<twoArray.Length-1;x++)
			{
				twoArray[x].word = tempArray[x].word;
                twoArray[x].row = tempArray[x].row;
				twoArray[x].col = tempArray[x].col;
				twoArray[x].type = tempArray[x].type;	
			}
			j = j + 1;  
		}
		#endregion
		#region 将单词添加到数组中
		private void stringToArrayString(ref words[] stringArrange, words st)
	    {   
			if(stringArrange[0].word == "")
			{
				stringArrange[0].word = st.word;
				stringArrange[0].row = st.row;
				stringArrange[0].col = st.col;
				stringArrange[0].type = st.type;
			}
			else
			{
				words[] oldA = stringArrange;
				int i=oldA.Length +1;
				stringArrange = new words[i]; 
				oldA.CopyTo(stringArrange,0);//将先前的单词结构体数组考到现在这个结构体数组中
				stringArrange[stringArrange.Length -1].word = st.word;
                stringArrange[stringArrange.Length -1].row = st.row;
				stringArrange[stringArrange.Length -1].col = st.col;
				stringArrange[stringArrange.Length -1].type = st.type;
			}
		 }
		#endregion
		#region 将单词添加到错误单词数组中
		private void stringToErrorsArrayString(ref words[] errors, words st)
		{   
			if(errors[0].word == "")
			{
				errors[0].word = st.word;
				errors[0].row = st.row;
				errors[0].col = st.col;
				errors[0].type = st.type;
			}
			else
			{
				words[] oldA = errors;
				int i=oldA.Length +1;
				errors = new words[i]; 
				oldA.CopyTo(errors,0);//将先前的单词结构体数组考到现在这个结构体数组中
				errors[errors.Length -1].word = st.word;
				errors[errors.Length -1].row = st.row;
				errors[errors.Length -1].col = st.col;
				errors[errors.Length -1].type = st.type;
			}
		}
		#endregion
		#region 结构体初始化
		public void  zero(words a)
		{
			a.word = "";
			a.row = 0;
			a.col = 0;
			a.type = "";
		}
		#endregion
		#region  读取要处理的字符串
		private char[] textToCharArray()
		{   

			string stringTemp;
			stringTemp = this.richTextBox1.Text;
			string str = stringTemp.Replace(" ","#");		
			char[] getch = str.ToCharArray();//要处理的字符都在getch这个数组中。
			return getch;
		}
		#endregion
		#region 将字符数组转换为单词数组
	   private words[] charArrayToStringArray(char[] getch)
	   {
		    words[] stringArrange = new words[1];//存放词法分析后得到的单词。
			char charTemp;
			words stringSave = new words();//存放一个分析得到的单词
		   //
		   words[] errors = new words[1];
		   //
            zero(stringSave);
			int col=0;  //记录当前行数
		    int row = 1;//记录当前行数
			//循环一次得到一个单词。
			for (int i = 0; i < getch.Length; i++)
			{   
				charTemp = getch[i];
				//遇到换行符,列数置零
				if (charTemp == '\n')
				{
					col = 0;
					row++;
				}
				if (charTemp == '#')
				{
					col++;
				}

				//由字母开头 数字和字母组成的单词。
				if (charTemp >= 'a' &&
					charTemp <= 'z'
					||
					charTemp >= 'A' &&
					charTemp <= 'Z')
				{   
					col++;
					stringSave.word = charTemp.ToString();
					stringSave.row = row;
					stringSave.col = col;
					i = i + 1;
					int test = 0;//判断循环是否结束,1为结束。
					while (test == 0)
					{
						charTemp = getch[i];

						if (charTemp >= 'a' &&
							charTemp <= 'z'
							||
							charTemp >= 'A' &&
							charTemp <= 'Z' 
							||
							charTemp >= '0' &&
							charTemp <= '9')
						{   
							col++;
							stringSave.word = stringSave.word + charTemp.ToString();
							i = i + 1;
						}
						else
						{
							test = 1;
							// i=i-1;
						}
						if(charTemp == '\n' )
							row++;
					}
			        stringToArrayString(ref stringArrange, stringSave);
				}
				zero(stringSave);//置空
               
                
				//由数字开头的字母数字组成的错误单词。
				if (charTemp >= '0' &&
					charTemp <= '9')
				{   
					col++;
					i = i + 1;
					charTemp=getch[i];
					int test = 0;//判断循环是否结束,1为结束。
					if (charTemp >= 'a'&&
						charTemp <= 'z'
						||
						charTemp >= 'A'&&
						charTemp <= 'Z')
					{   
						stringSave.word = getch[i-1].ToString();
						stringSave.col = col;
						stringSave.row = row;
						stringSave.type = "变量必须为以字母开头的字母或字母数字组合";

						col++;
						stringSave.word =stringSave.word + charTemp.ToString();
						
						i++;
					
						while (test == 0)
						{
							charTemp = getch[i];

							if (charTemp >= 'a' &&
								charTemp <= 'z'
								||
								charTemp >= 'A' &&
								charTemp <= 'Z')
							{   
								col++;
								stringSave.word = stringSave.word + charTemp.ToString();
								i = i + 1;
							}
							else
							{
								test = 1;
							}
							if(charTemp == '\n' )
								row++;
						}
				
						//stringToErrorsArrayString(ref errors, stringSave);
					}
					else 	
					{
							i = i - 1;
						    col --;
					}
				}
				zero(stringSave);//置空
                
				//由数字组成的单词
				charTemp = getch[i];
				if (charTemp >= '0' &&
					charTemp <= '9')
				{   
					col++;
					stringSave.word = charTemp.ToString();
					stringSave.row = row;
					stringSave.col = col;
					
					i = i + 1;
					int test1 = 0;
					while (test1 == 0)
					{   
						charTemp = getch[i];
						if (charTemp >= '0' 
							&& charTemp <= '9'
							|| charTemp == '.')
							
						{   
							col++;
							stringSave.word = stringSave.word + charTemp.ToString();
							
							i = i + 1;
						}
						else
						{
							test1 = 1;
							//  i=i-1;
						}
						if(charTemp == '\n' )
							row++;
					}
					stringToArrayString(ref stringArrange, stringSave);
				}
			    zero(stringSave);//置空

				//由运算符(>=,<=,:=)组成的单词。
				if (charTemp == '<'
					||
					charTemp == '>'
					||
					charTemp == ':')
				{   
					col++;
					stringSave.word = charTemp.ToString();
					stringSave.row = row;
					stringSave.col = col;
					i = i + 1;
					int test = 0;
					//判断循环是否结束,1 为结束。
					while (test == 0)
					{
						charTemp = getch[i];
						if (charTemp == '=')
						{   
							col++;
							stringSave.word= stringSave.word + charTemp.ToString();
							i = i + 1;
						}
						else
                            
						{
							test = 1;
							i = i - 1;
						}
					}
					stringToArrayString(ref stringArrange, stringSave);
				}
			    zero(stringSave);

				//注释语句
				if (charTemp == '/')
				{
					//stringSave = charTemp.ToString();
					col++;
					i = i + 1;
					
					int test = 0;
					charTemp = getch[i];//提前判断下一字符,如果为‘/’,则该行为注释语句
					if (charTemp == '/')//由//分隔的行注释
					{
						//判断循环是否结束,1 为结束。
						while (test == 0)
						{

⌨️ 快捷键说明

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