?? lexical.cs
字號(hào):
using System;
using System.IO;
using System.Collections;
namespace Lexical_Analyzer
{
/// <summary>
/// Lexical 的摘要說明。
/// </summary>
public class Lexical
{
private int rnum;//行號(hào)
private int rpos;//列號(hào)
private static ArrayList symbolTable;
private StreamReader br;
private char c;
private char bC;
private int state;
private String str;
public Lexical(StreamReader br)
{
symbolTable = new ArrayList();
this.br=br;
c=' ';
bC=' ';
state = 0;
rnum=1;
rpos=0;
str="";
}
public ArrayList getSymbolTable()
{
c = nextChar();
state = 0;
analyzer();
return symbolTable;
}
private void analyzer()
{
bool isRunning=true;
while(isRunning)
{
switch (state)
{
case 0:
if(c==' ') //空格
{
}
else if(c=='\t') //tab
{
rpos+=3;
}
else if((int)c==13) //空格
{
}
else if((int)c==10) //回車
{
rpos = 0;
rnum++;
}
else if(c=='~') //結(jié)束符
{
return;
}
else if(c=='+') //操作符+
{
install("操作符","+",rpos,rnum);
}
else if(c=='-') //操作符-
{
install("操作符","-",rpos,rnum);
}
else if(c=='/') //操作符/
{
state = 1;
}
else if(c=='*') //操作符*
{
install("操作符","*",rpos,rnum);
}
else if(c=='=') //操作符=
{
state = 2;
}
else if(c=='<') //操作符<
{
state = 3;
}
else if(c=='>') //操作符>
{
state = 4;
}
else if(c=='!') //操作符!
{
state = 5;
}
else if(c=='{') //分隔符{
{
install("分隔符","{",rpos,rnum);
}
else if(c=='}') //分隔符}
{
install("分隔符","}",rpos,rnum);
}
else if(c=='(') //分隔符(
{
install("分隔符","(",rpos,rnum);
}
else if(c==')') //分隔符)
{
install("分隔符",")",rpos,rnum);
}
else if(c==';') //分隔符;
{
install("分隔符",";",rpos,rnum);
}
else if((int)c==65535)
{
isRunning=false;
}
else if(isLetter(c)) //讀到了字符
{
bC = c;
state = 6;
}
else if(isDigit(c)) //讀到了數(shù)字符
{
bC = c;
str="";
state = 7;
}
else if(c=='#')
{
state=13;
}
else
{
state=0;
isRunning=false;
fail(0);
}
c = nextChar();
break;
case 1:
if(c=='/') //表示注釋后面的東西,不讀它
{
while((int)c!=10)
{
c = nextChar();
}
rpos=0; //從下一行開始
rnum++;
c = nextChar();
state = 0;
}
else
{
state = 0; // 下一個(gè)不是/, 后退一步到state=0
install("操作符","/",rpos-1,rnum);
}
break;
case 2:
if(c=='=') //表示操作符==
{
state = 0;
install("操作符","==",rpos-1,rnum);
c = nextChar();
}
else
{
state = 0;
install("操作符","=",rpos-1,rnum);
}
break;
case 3:
if(c=='=') //表示操作符<=
{
state = 0;
install("操作符","<=",rpos-1,rnum);
c = nextChar();
}
else
{
state = 0;
install("操作符","<",rpos-1,rnum);
}
break;
case 4:
if(c=='=') //表示操作符>=
{
state = 0;
install("操作符",">=",rpos-1,rnum);
c = nextChar();
}
else
{
state = 0;
install("操作符",">",rpos-1,rnum);
}
break;
case 5:
if(c=='=') // 表示操作符!=
{
state = 0;
install("操作符","!=",rpos-1,rnum);
c = nextChar();
}
else
{
state = 0;
isRunning=false;
fail(1);
}
break;
//***********************************
//讀入標(biāo)識(shí)符,后面是 數(shù)字符和字母才是對(duì)的
//***********************************
case 6:
String id = ""+bC;
while(isLetter(c)||isDigit(c))
{
id+=c;
c = nextChar();
}
if(isKey(id))
install("關(guān)鍵字",id,rpos-id.Length,rnum);
else
install("標(biāo)識(shí)符",id,rpos-id.Length,rnum);
state = 0;
break;
//*********************************************
//讀入數(shù)字符,后面是 數(shù)字符(循環(huán)); .(轉(zhuǎn)狀態(tài));E(轉(zhuǎn)狀態(tài))
//**********************************************
case 7:
str+=bC;
while(isDigit(c))
{
str+=c;
c=nextChar();
}
if(c=='.')
{
state=8;
c=nextChar();
}
else if(c=='E')
{
state=10;
c=nextChar();
}
//需要判斷是否是 11w等
else if(isLetter(c))
{
state = 0;
isRunning=false;
fail(4);
}
else
{
state = 0;
install("數(shù)字符",str,rpos-str.Length,rnum);
}
break;
//***********************************
// " ." 后面跟的字符,除了數(shù)字符其它都出錯(cuò)
//***********************************
case 8:
str+='.';
if(isDigit(c))
{
state = 9;
str+=c;
c=nextChar();
}
else
{
state = 0;
isRunning=false;
fail(2);
}
break;
//***********************************************************************************
// ".數(shù)字符" 后面字符,可以是數(shù)字符或者是E(需要加錯(cuò)誤提示,當(dāng)不是空格而是其他字母時(shí)候)
//************************************************************************************
case 9:
while(isDigit(c))
{
str+=c;
c=nextChar();
}
if(c=='E')
{
state = 10;
c=nextChar();
}
//需要判斷是否是 11w 等
else if(isLetter(c))
{
state = 0;
isRunning=false;
fail(2);
}
else
{
state = 0;
install("數(shù)字符",str,rpos-str.Length,rnum);
}
break;
//*********************************************
// "E" 后面字符, "+";"-";"數(shù)字符" 都是對(duì)的,其他都是錯(cuò)的
//********************************************
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(2);
}
break;
//*****************************************
// "+" "-" 后面字符, 數(shù)字符是對(duì)的.其他都是錯(cuò)的
//*****************************************
case 11:
if(isDigit(c))
{
state=12;
str+=c;
c=nextChar();
}
else
{
state = 0;
isRunning=false;
fail(2);
}
break;
//***********************************
// "E數(shù)字符" 后面字符,只有數(shù)字符是對(duì)的
//*********************************
case 12:
while(isDigit(c))
{
str+=c;
c=nextChar();
}
if(isLetter(c))
{
state = 0;
isRunning=false;
fail(2);
}
install("數(shù)字符",str,rpos-str.Length,rnum);
state=0;
break;
case 13: //聲明部分
id ="#";
while(isLetter(c))
{
id+=c;
c = nextChar();
}
if(id=="#include"&&c=='<')
{
id+=c;
c = nextChar();
while(isLetter(c)||this.isDigit(c))
{
id+=c;
c = nextChar();
}
if(c=='>')
{
id+=c;
install("文件引入",id,rpos-id.Length,rnum);
c = nextChar();
}
else
fail(3);
}
else
fail(0);
state = 0;
break;
}
}
}
private void fail(int i)
{
string typeerror="";;
switch(i)
{
case 0:
typeerror="輸入非法字符錯(cuò)誤";
break;
case 1:
typeerror="操作符錯(cuò)誤";
break;
case 2:
typeerror="數(shù)字符錯(cuò)誤";
break;
case 3:
typeerror="聲明錯(cuò)誤";
break;
case 4:
typeerror="標(biāo)識(shí)符錯(cuò)誤";
break;
}
Console.Write(typeerror+",位置<"+rnum+","+rpos+">");
}
private char nextChar()
{
char t;
rpos++;
if(br.Peek() == -1)
{
br.Close();
return '~';
}
t= (char) br.Read();
return t;
}
private void install(String tType,String aValue,int rpos,int rnum)
{
ArrayList tokenArray = new ArrayList();
tokenArray.Add(tType);
tokenArray.Add(aValue);
tokenArray.Add("<"+rnum.ToString()+","+rpos.ToString()+">");
symbolTable.Add(tokenArray);
ArrayList tempArray=tokenArray;
for(int j=0;j<tempArray.Count;j++)
{
Console.Write(tempArray[j].ToString()+" ");
}
Console.WriteLine();
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"))
{
return true;
}
return false;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -