?? lexical.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: 在此處添加構(gòu)造函數(shù)邏輯
//
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++;
//是否為文件結(jié)尾
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;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -