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

📄 lexicalanalysis.cs

📁 词法分析的小工具 有点意思 还算可以 不过比较简陋
💻 CS
字号:
using System;
using System.Text;
using System.IO;

namespace LexicalAnalysis
{
	/// <summary>
	/// Class1 的摘要说明。
	/// </summary>
	class LexicalAnalysis
	{

		


		[STAThread]
		static void Main(string[] args)
		{
            LexicalAnalysis myLex = new LexicalAnalysis();
			string[] Text = new string[100];
			StreamReader myStreamReader;
			StreamWriter myStreamWriter;
		    string strReadFile;
			string strWriterFile;
			strWriterFile= "result.txt";
			strReadFile="LexInput.txt";
			myStreamReader=new StreamReader(strReadFile);
			if (File.Exists(strWriterFile)) 
			{
				File.Delete(strWriterFile);
			}
			myStreamWriter= File.CreateText(strWriterFile);
			Console.WriteLine("内容:");
			myStreamWriter.WriteLine("分析结果:");
			//文件读入
			string strBufferText;
			int i = 0;
			do
			{
				
				strBufferText =myStreamReader.ReadLine();
				if(strBufferText==null)
					break;
				foreach (String subString in strBufferText.Split())
				{
					
					if(subString!="")
					{
						Text[i]=subString;
						i++;
					}
				}
			}
			while (strBufferText!=null);
			int jj=0;
			while (Text[jj]!=null)
			{
				Console.WriteLine(Text[jj]);
				jj++;
			}
			myStreamReader.Close();
			//分析
			foreach(string str in Text)
			{
				int length ;
				if(str!=null)
				{length = str.Length;}
				else{break;}
				int a=length+1;
				char[] b = new char[a];
				StringReader sr = new StringReader(str);
				sr.Read(b, 0, length);
				sr.Close();
				int state=0;
				bool flag=true;
				int j =0;
				while(flag)
				{
					switch(state)
					{
						case 0:
							string f=b[j].ToString();
							if(Char.IsDigit(b[j]))
						    {
								state=1;
								j++;
								
							}
							else 
							{
								if(Char.IsLetter(b[j]))
								{
									state=2;

								}
								else 
								{
									if(b[j]=='+'|b[j]=='-'|b[j]=='/'|b[j]=='*'|b[j]==';'|b[j]==':'|b[j]=='{'|b[j]=='}')
									{
										state=3;
										j++;

									}
									else
									{state=4;}
								}
							}
							
							break;
						case 1:
							f=b[j].ToString();

							if(f.Equals("\0"))
							{
								Console.WriteLine("得到一个数字"+str);
								myStreamWriter.WriteLine("得到一个数字"+str);
								flag=false;
							}
							else
							{
								if(Char.IsDigit(b[j]))
								{
									state=1;
									j++;
								
								}
								else
								{
								  state=4;
								}

							}
							break;
						case 2:
							if(b[j]=='i')
							{
								state=5;         
							}
							else
							{
								if(b[j]=='e')
								{
									state=6;
								}
								else
								{
									
									state=7;
									
								}
							}
							j++;
							break;
						case 3:
							f=b[j].ToString();

							if(f.Equals("\0"))
							{
								Console.WriteLine("得到一个符号"+str);
								myStreamWriter.WriteLine("得到一个符号"+str);
								flag=false;
							}
							else
							{
								state=4;
							}
							break;
						case 4:
						
							Console.WriteLine("错误的词或格式"+str);
							myStreamWriter.WriteLine("错误的词或格式"+str);
							flag=false;
							break;
						case 5:
							f=b[j].ToString();
							if(f.Equals("\0"))
							{
								state=7;
							}
							else
							{
								if(b[j]=='f')
								{
									state=8;
									j++;
								}
								else
								{
									if(Char.IsDigit(b[j])|Char.IsLetter(b[j]))
									{
										state=7;
										j++;
									}
									else
									{
										state=4;
									}
								}

							}
							break;
                        case 6:
							f=b[j].ToString();
							if(f.Equals("\0"))
							{
								state=7;
							}
							else
							{
								if(b[j]=='l')
								{
									state=9;
									j++;
								}
								else
								{
									if(Char.IsDigit(b[j])|Char.IsLetter(b[j]))
									{
										state=7;
										j++;
									}
									else
									{
										state=4;
									}
								}

							}
							break;

						case 7:
							f=b[j].ToString();
							if(f.Equals("\0"))
							{
								Console.WriteLine("取得一个正规串"+str);
								myStreamWriter.WriteLine("取得一个正规串"+str);
								flag=false;
								
							}
							else
							{
								if(Char.IsDigit(b[j])|Char.IsLetter(b[j]))
								{
									state=7;
									j++;
								}
								else
								{
									
									state=4;
								}

							}
							break;
						case 8:
							f=b[j].ToString();
							if(f.Equals("\0"))
							{
								Console.WriteLine("得到一个关键字:"+str);
								myStreamWriter.WriteLine("得到一个关键字"+str);
								flag=false;
							}
							else
							{
								if(Char.IsDigit(b[j])|Char.IsLetter(b[j]))
								{
									state=7;
									j++;
								}
								else
								{
									
								    state=4;

								}

							}
							break;
						case 9:
							f=b[j].ToString();
							if(f.Equals("\0"))
							{
								state=7;
							}
							else
							{
								if(b[j]=='s')
								{
									state=10;
									j++;
								}
								else
								{
									if(Char.IsDigit(b[j])|Char.IsLetter(b[j]))
									{
										state=7;
										j++;
									}
									else
									{
										state=4;
									}
								}

							}
							break;

						case 10:
							f=b[j].ToString();
							if(f.Equals("\0"))
							{
								state=7;
							}
							else
							{
								if(b[j]=='e')
								{
									state=11;
									j++;
								}
								else
								{
									if(Char.IsDigit(b[j])|Char.IsLetter(b[j]))
									{
										state=7;
										j++;
									}
									else
									{
										state=4;
									}
								}

							}
							break;
						case 11:
							f=b[j].ToString();
							if(f.Equals("\0"))
							{
								Console.WriteLine("得到一个关键字:"+str);
								myStreamWriter.WriteLine("得到一个关键字"+str);
								flag=false;
							}
							else
							{
								if(Char.IsDigit(b[j])|Char.IsLetter(b[j]))
								{
									state=7;
									j++;
								}
								else
								{
									
									state=4;

								}

							}
							break;



					}
				}
			}
			myStreamWriter.Close();
			Console.Read();
			

		}
	}
}

⌨️ 快捷键说明

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