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

📄 form1.cs

📁 编译原理 词法分析器 识别标识符
💻 CS
📖 第 1 页 / 共 4 页
字号:
					
					//////////
				
                else col++;

			}
			DataTable dt = new DataTable();
            
			dt.Columns.Add("错误提示",System.Type.GetType("System.String"));
			dt.Columns.Add("错误单词",System.Type.GetType("System.String")); 
			dt.Columns.Add("行",System.Type.GetType("System.String")); 
			dt.Columns.Add("列",System.Type.GetType("System.String"));
			    
			for(int k=1;k<errors.Length;k++)
			{
				System.Data.DataRow dr=dt.NewRow(); 
				dr["错误提示"]=errors[k].type;
				dr["错误单词"]=errors[k].word; 
				dr["行"]=errors[k].row; 
				dr["列"]=errors[k].col; 
				dt.Rows.Add(dr); 
			}
			this.dataGrid_error.SetDataBinding(dt,"");

		return errors;
            
	}
	
    #endregion
	    //
		#region 单词分类
		private words[] stringArrayTotoken(words[] stringArray)
	     {
			//存放单词标识后的结果。
			words[] twoArray = new words[2];
			//单词的标志
			int j=1;
            int i=0;
			int test=0;
			//每循环一次,把一个单词归于一类,即前面加上一个数字。
			
			for(i=1;i<stringArray.Length;i++)
			{   
				string str = stringArray[i].word;
				
				char[] word = str.ToCharArray();
				int flag =1;
				int flag1=1;
				int flag2=0;
				for(int k=0;k<word.Length;k++)
				{
					if(!(word[k]<'9'&&word[k]>'0'))
					    flag = 0;
					if(!((word[k]<'9'&&word[k]>'0')||word[k]=='.'))
						flag1 = 0;
				}
				
				for(int m=0;m<word.Length;m++)
				{
					if(word[m]=='.')
						test=1;
					
				}
				if(flag1==1&&test==1)
					flag2=1;
					//实常数
				if(flag2==1)
				{
					twoArray[i].type ="36";
					twoArray[i].word = stringArray[i].word;
					twoArray[i].col = stringArray[i].col;
					twoArray[i].row = stringArray[i].row;
					this.oneArrayToTwo(ref twoArray,ref j);
				}
					//整常数
				else if(flag==1)
				{
					twoArray[i].type ="35";
					twoArray[i].word = stringArray[i].word;
					twoArray[i].col = stringArray[i].col; 
					twoArray[i].row = stringArray[i].row;
					this.oneArrayToTwo(ref twoArray,ref j);
				}
					//单词分类
				else switch(str)
					 {   
						 case "program":{twoArray[i].type = " 1";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "var":    {twoArray[i].type = " 2";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "integer":{twoArray[i].type = " 3";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "bool":   {twoArray[i].type = " 4";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "real":   {twoArray[i].type = " 5";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "char":   {twoArray[i].type = " 6";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "const":  {twoArray[i].type = " 7";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "begin":  {twoArray[i].type = " 8";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "if":     {twoArray[i].type = " 9";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "then":   {twoArray[i].type = "10";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "else":   {twoArray[i].type = "11";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "while":  {twoArray[i].type = "12";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "do":     {twoArray[i].type = "13";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "repeat": {twoArray[i].type = "14";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "until":  {twoArray[i].type = "15";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "for":    {twoArray[i].type = "16";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "to":     {twoArray[i].type = "17";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "not":    {twoArray[i].type = "21";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "and":    {twoArray[i].type = "22";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "or":     {twoArray[i].type = "23";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "+":      {twoArray[i].type = "24";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "-":      {twoArray[i].type = "25";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "*":      {twoArray[i].type = "26";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "/":      {twoArray[i].type = "27";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "<":      {twoArray[i].type = "28";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case ">":      {twoArray[i].type = "29";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "<=":     {twoArray[i].type = "30";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case ">=":     {twoArray[i].type = "31";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "=":      {twoArray[i].type = "32";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case ":=":     {twoArray[i].type = "33";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case ":":      {twoArray[i].type = "39";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case ",":      {twoArray[i].type = "40";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "'":      {twoArray[i].type = "41";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "\"":     {twoArray[i].type = "42";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "//":     {twoArray[i].type = "43";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "/*":     {twoArray[i].type = "44";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "*/":     {twoArray[i].type = "45";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case ";":      {twoArray[i].type = "46";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case "(":      {twoArray[i].type = "47";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case ")":      {twoArray[i].type = "48";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 case ".":      {twoArray[i].type = "49";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
						 default:       {twoArray[i].type = "34";twoArray[i].word = stringArray[i].word;twoArray[i].col=stringArray[i].col;twoArray[i].row=stringArray[i].row;this.oneArrayToTwo(ref twoArray,ref j);break;}
					 }

			}
			      
			DataTable dt = new DataTable();
            
			dt.Columns.Add("种别码",System.Type.GetType("System.String"));
			dt.Columns.Add("单词",System.Type.GetType("System.String")); 
			dt.Columns.Add("行",System.Type.GetType("System.String")); 
			dt.Columns.Add("列",System.Type.GetType("System.String"));
			    
			for(i=1;i<twoArray.Length-1;i++)
			{
				System.Data.DataRow dr=dt.NewRow(); 
				dr["种别码"]=twoArray[i].type;
				dr["单词"]=twoArray[i].word; 
				dr["行"]=twoArray[i].row; 
				dr["列"]=twoArray[i].col; 
				dt.Rows.Add(dr); 
			}
			this.dataGrid_token.SetDataBinding(dt,""); 
			dataGrid_token.SelectionBackColor=System.Drawing.Color.Green;
			//this.dataGrid_token.ColumnHeadersVisible = true;
			
			return twoArray;
			
		}
		#endregion
        #region 打开事件
		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
			openFileDialog1.Title = "打开要分析的源文件。";
			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				System.IO.StreamReader sr = new
					System.IO.StreamReader(openFileDialog1.FileName);
				this.richTextBox1.Text = sr.ReadToEnd();
				//
				
				string str=sr.ReadLine();
                    
				while (str!=null)
                 
				//
					sr.Close();
			}
		    
		}
        #endregion
		private void menuItem3_Click(object sender, System.EventArgs e)
		{
		  
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
			this.Close();	
		}
        #region 保存事件
		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
			saveFileDialog1.Title = "保存分析结果.";
			if(saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				System.IO.StreamWriter wr = new
					System.IO.StreamWriter(saveFileDialog1.FileName);
				wr.Write(this.richTextBox1.Text);
				//wr.Write(this.dataGrid1.DataSource);
				wr.Close();    
			}
		}
        #endregion
		private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
		
		}

		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			
		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{ 
			
		}

		private void menuItem6_Click(object sender, System.EventArgs e)
		{
		
		}

		private void menuItem11_Click(object sender, System.EventArgs e)
		{
		
		}

		private void menuItem12_Click(object sender, System.EventArgs e)
		{
		
		}

		private void richTextBox1_TextChanged(object sender, System.EventArgs e)
		{
		 
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}
        #region 键盘取当前坐标
		private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			Point P = new Point(0);
			GetCaretPos(ref   P);
			int Pos = this.richTextBox1.GetCharIndexFromPosition(P);
			this.Y = this.richTextBox1.GetLineFromCharIndex(Pos);

			if (this.Y > 0)
			{
				int offset = 1;
				while (this.richTextBox1.Text[Pos - offset] != '\n')
					offset++;
				this.X = offset;
				this.Y++;
			}
			else
			{
				this.X = Pos + 1;
				this.Y = 1;
			}

			label1.Text = "当前位置:   第" + this.Y.ToString() + "行   " + "第" + this.X.ToString() + "列"; 
		}
        #endregion
		#region 鼠标取当前坐标
		private void richTextBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			Point P = new Point(0);
			GetCaretPos(ref   P);
			int Pos = this.richTextBox1.GetCharIndexFromPosition(P);
			this.Y = this.richTextBox1.GetLineFromCharIndex(Pos);

			if (this.Y > 0)
			{
				int offset = 1;
				while (this.richTextBox1.Text[Pos - offset] != '\n')
					offset++;
				this.X = offset;
				this.Y++;
			}
			else
			{
				this.X = Pos + 1;
				this.Y = 1;
			}

			label1.Text = "当前位置:   第" + this.Y.ToString() + "行   " + "第" + this.X.ToString() + "列"; 
			
			//int  max = dataGrid_token.VisibleRowCount;
			//int i=0;
			//while( i < max && dataGrid_token.Select())
			//dataGrid_token.roSelect = true;
			
		}
        #endregion
		#region 主函数
		private void menuItem10_Click(object sender, System.EventArgs e)
		{          
			//得到想要的字符数组。
			char[] getch = textToCharArray();

			//将字符数组,转换为词法分析后的 单词数组。
			words[] stringArray = charArrayToStringArray(getch);

			//将单词数组分类,用数字标出各个单词所在的类别。
			words[] token = stringArrayTotoken(stringArray);

			//错误检测
			words[] errors = stringArrayToError(getch);
			
		}
        #endregion
		#region 保存事件
		private void menuItem16_Click(object sender, System.EventArgs e)
		{
			string strFilter;
			strFilter = "文本文档(*.txt) |*.txt|" + "所有文件(*.*) |*.*";
			saveFileDialog1.Filter = strFilter;
			string sPath = "";
			if (saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				string strFileName = saveFileDialog1.FileName;
				sPath = strFileName;
				System.IO.StreamWriter strW = new System.IO.StreamWriter(sPath);
				if (richTextBox1.Text != "")
					strW.Write(richTextBox1.Text);
				strW.Close();
			}
		}
        #endregion

		private void dataGrid_token_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
		{
		
		}

		private void dataGrid_error_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
		{
		
		}
		
				
	}

}


⌨️ 快捷键说明

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