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

📄 lexical.cs

📁 简单的词法分析器 利用vc编程 功能 可以打开任意位置和名称的原文件进行词法分析
💻 CS
字号:
using System;
using System.IO;
using System.Collections;

namespace Analyzer
{
	/// <summary>
	/// Lexical 的摘要说明。
	/// </summary>
	public class Lexical
	{

		private static ArrayList symbolTable;
    
		private static int tokentype=0;
		private static int attributevalue=1;
		private static int linenumber=2;
		private static int lineposition=3;

		//行号
		private int lnum;
		//字符位置
		private int lpos;
    
		private StreamReader br;
		private char c;
		private char preC;
		private int state;
		private String str;

		public Lexical(StreamReader br)
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//

			this.br=br;
			c=' ';
			preC=' ';
			state = 0;
			symbolTable = new ArrayList();
			lnum=1;
			lpos=0;
			str="";
		}

		public ArrayList getSymbolTable()
		{
			c = nextChar();
			state = 0;
			analyzer();
			install("$","$",lpos,lnum);
			return symbolTable;
		}

		
		private void analyzer()
		{
			bool isRunning=true;
			while(isRunning)
			{
				switch (state)
				{
					case 0:
						if(c==' ')
						{
                    
						}
						else if(c=='\t')
						{
							lpos+=3;
						}
						else if((int)c==13)
						{
                    
						}
						else if((int)c==10)
						{
							lpos = 0;
							lnum++;
						}
						else if(c=='~')
						{
							return;
						}
						else if(c=='+')
						{
							install("OP","+",lpos,lnum);
						}
						else if(c=='-')
						{
							install("OP","-",lpos,lnum);
						}
						else if(c=='/')
						{
							state = 1;
						}
						else if(c=='*')
						{
							install("OP","*",lpos,lnum);
						}
						else if(c=='=')
						{
							state = 2;
						}
						else if(c=='<')
						{
							state = 3;
						}
						else if(c=='>')
						{
							state = 4;
						}
						else if(c=='!')
						{
							state = 5;
						}
						else if(c=='{')
						{
							install("DELIM","{",lpos,lnum);
						}
						else if(c=='}')
						{
							install("DELIM","}",lpos,lnum);
						}
						else if(c=='(')
						{
							install("DELIM","(",lpos,lnum);
						}
						else if(c==')')
						{
							install("DELIM",")",lpos,lnum);
						}
						else if(c==';')
						{
							install("DELIM",";",lpos,lnum);
						}
						else if((int)c==65535)
						{
							isRunning=false;
						}
						else if(isLetter(c))
						{
							preC = c;
							state = 6;
						}
						else if(isDigit(c))
						{
							preC = c;
							str="";
							state = 7;
						}
						else 
						{
							state=0;
							isRunning=false;
							fail();
						}
						c = nextChar();
						break;
					case 1:
						if(c=='/')
						{
							while((int)c!=10)
							{
								c = nextChar();
							}
							lpos=0;
							lnum++;
							c = nextChar();
							state = 0;
						}
						else 
						{
							state = 0;
							install("OP","/",lpos-1,lnum);
						}

						break;
					case 2:
                
						if(c=='=')
						{
							state = 0;
							install("OP","==",lpos-1,lnum);
							c = nextChar();
						}
						else 
						{
							state = 0;
							install("OP","=",lpos-1,lnum);
						}

						break;
					case 3:
						if(c=='=')
						{
							state = 0;
							install("OP","<=",lpos-1,lnum);
							c = nextChar();
						}
						else 
						{
							state = 0;
							install("OP","<",lpos-1,lnum);
						}
                
						break;
					case 4:
						if(c=='=')
						{
							state = 0;
							install("OP",">=",lpos-1,lnum);
							c = nextChar();
						}
						else 
						{
							state = 0;
							install("OP",">",lpos-1,lnum);
						}

						break;
					case 5:
						if(c=='=')
						{
							state = 0;
							install("OP","!=",lpos-1,lnum);
							c = nextChar();
						}
						else 
						{
							state = 0;
							isRunning=false;
							fail();
						}
						break;
					case 6:
						String id = ""+preC;
						while(isLetter(c)||isDigit(c))
						{
							id+=c;
							c = nextChar();
						}
						if(isKey(id))
							install("KEY",id,lpos-id.Length,lnum);
						else
							install("ID",id,lpos-id.Length,lnum);
						state = 0;
						break;
					case 7:
						str+=preC;
						while(isDigit(c))
						{
							str+=c;
							c=nextChar();
						}
						if(c=='.')
						{
							state=8;
							c=nextChar();
						}
						else if(c=='E')
						{
							state=10;
							c=nextChar();
						}
						else
						{
							state = 0;
							install("NUM",str,lpos-str.Length,lnum);
						}
						break;
					case 8:
						str+='.';
						if(isDigit(c))
						{
							state = 9;
							str+=c;
							c=nextChar();
						}
						else
						{
							state = 0;
							isRunning=false;
							fail();
						}
						break;
					case 9:
                
						while(isDigit(c))
						{
							str+=c;
							c=nextChar();
						}
						if(c=='E')
						{
							state = 10;
							c=nextChar();
						}
						else
						{
							state = 0;
							install("NUM",str,lpos-str.Length,lnum);
						}
						break;
					case 10:
						str+='E';
						if(c=='+'||c=='-')
						{
							state=11;
							str+=c;
							c=nextChar();
						}
						else if(isDigit(c))
						{
							state=12;
							str+=c;
							c=nextChar();
						}
						else
						{
							state=0;
							isRunning=false;
							fail();
						}
						break;
					case 11:
						if(isDigit(c))
						{
							state=12;
							str+=c;
							c=nextChar();
						}
						else
						{
							state = 0;
							isRunning=false;
							fail();
						}
						break;
					case 12:
						while(isDigit(c))
						{
							str+=c;
							c=nextChar();
						}
						install("NUM",str,lpos-str.Length,lnum);
						state=0;
						break;
				}  
			}
		}
 
		private void fail() 
		{
			//System.out.println("Error at line:"+lnum+"  pos:"+lpos);
			Console.WriteLine("Error at line:"+lnum+"  pos:"+lpos);
		}

		private char nextChar()
		{
			char t;
			lpos++;

			//是否为文件结尾
			if(br.Peek() == -1)
			{
				br.Close();
				return '~';
			}

			t= (char) br.Read();

			//System.out.println(t+":"+lnum+"  "+lpos);

			//Console.WriteLine(t+":"+lnum+"  "+lpos);
			return t;
		}
    
		private void install(String tType,String aValue,int lpos,int lnum)
		{
			ArrayList tokenArray = new ArrayList();
//			tokenArray.Add(tokentype,tType);
//			tokenArray.Add(attributevalue,aValue);
//			tokenArray.Add(linenumber,lnum.ToString());
//			tokenArray.Add(lineposition,lpos.ToString());

			tokenArray.Add(tType);
			tokenArray.Add(aValue);
			tokenArray.Add(lnum.ToString());
			tokenArray.Add(lpos.ToString());

			symbolTable.Add(tokenArray);
        
			str="";
		}
    
		private bool isLetter(char checkChar)
		{
			int checkInt = (int)checkChar;
			if((checkInt<=122&&checkInt>=97)||(checkInt>=65&&checkInt<=97))
			{
				return true;
			}
			return false;
		}
    
		private bool isDigit(char checkChar)
		{
			int checkInt = (int)checkChar;
			if(checkInt<=57&&checkInt>=48)
			{
				return true;
			}
			return false;
		}
    
		private bool isKey(String checkKey)
		{
			if(checkKey.Equals("if")||checkKey.Equals("then")||
				checkKey.Equals("else")||checkKey.Equals("int")||
				checkKey.Equals("real")||checkKey.Equals("while")||
				checkKey.Equals("void")||checkKey.Equals("main"))
			{
				return true;
			}
			return false;
		}
	}
}

⌨️ 快捷键说明

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