📄 lexicalanalyze.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Analyzer
{
class LexicalAnalyze
{
private int typeofkey; //关键字的类型
private int row; //当前编译的行数
private string text; //此法分析类构造时传入要处理的代码
private int length; //代码长度
private int pointer; //指向下一个要处理的字符
private char c; //当前正在处理的字符
private char bC; //当前处理的字符的前一个字符
private int state; //处理状态
private string str; //当前处理行,处理后的结果
private string result; //最后处理完所有代码结果存放在result中等待返回
public LexicalAnalyze(string t)//构造函数初始化
{
this.text = t;
length = text.Length;
pointer = 0;
typeofkey = -1;
row = 0;
c=' ';
bC=' ';
state = 0;
str = "";
result = "";
}
public string getSymbolTable()//处理入口
{
c = nextChar();//取下一个字符
analyzer();//开始分析
return result;//返回结果输出在richtexbox中
}
private void analyzer()
{
bool flag=true;//标志是否继续执行
while(flag)
{
switch (state)
{
case 0:
if(c==' ') //空格
{
}
else if((int)c==10) //回车
{
row++;
result +="第"+row.ToString()+"行"+ str + "\n";
str = "";
}
else if(c=='~') //结束符
{
return;
}
else if(c=='+') //操作符+
{
install("操作符","+",1);
}
else if(c=='-') //操作符-
{
install("操作符","-",2);
}
else if(c=='=') //操作符=
{
state = 2;
}
else if(c=='<') //操作符<
{
state = 3;
}
else if(c=='>') //操作符>
{
state = 4;
}
else if(c=='!') //操作符!
{
state = 5;
}
else if(c=='{') //分隔符{
{
install("分隔符","{",3);
}
else if(c=='}') //分隔符}
{
install("分隔符","}",4);
}
else if(c=='(') //分隔符(
{
install("分隔符","(",5);
}
else if(c==')') //分隔符)
{
install("分隔符",")",6);
}
else if(c==';') //分隔符;
{
install("分隔符",";",7);
}
else if(isLetter(c)) //读到了字符
{
bC = c;
state = 6;
}
else if(isDigit(c)) //读到了数字
{
bC = c;
state = 7;
}
else if(c=='#')
{
state=8;
}
else
{
state=0;
flag=false;
fail(0);
}
c = nextChar();
break;
case 2:
if(c=='=') //表示操作符==
{
state = 0;
install("操作符","==",8);
c = nextChar();
}
else
{
state = 0;
install("操作符","=",9);
}
break;
case 3:
if(c=='=') //表示操作符<=
{
state = 0;
install("操作符","<=",10);
c = nextChar();
}
else
{
state = 0;
install("操作符","<",11);
}
break;
case 4:
if(c=='=') //表示操作符>=
{
state = 0;
install("操作符",">=",12);
c = nextChar();
}
else
{
state = 0;
install("操作符",">",13);
}
break;
case 5:
if(c=='=') // 表示操作符!=
{
state = 0;
install("操作符","!=",14);
c = nextChar();
}
else
{
state = 0;
flag=false;
fail(1);
}
break;
//读入标识符,后面是 数字符和字母才是对的
case 6:
string id = bC.ToString();
while(isLetter(c)||isDigit(c))
{
id += c.ToString() ;
c = nextChar();
}
if(isKey(id))
install("关键字",id,typeofkey);
else
install("标识符",id,15);
state = 0;
break;
//读入数字符,后面是 数字符(循环);
case 7:
string digit = bC.ToString();
while(isDigit(c))
{
digit+=c;
c=nextChar();
}
state = 0;
install("数字符",digit,16);
break;
case 8: //声明部分
string temp = "";
install("操作符", "#", 17);
while(isLetter(c))
{
temp += c;
c = nextChar();
}
if (temp == "include" && c == '<')
{
install("关键字", "include", 18);
install("操作符", c.ToString(), 11);
c = nextChar();
while(isLetter(c)||this.isDigit(c))
{
c = nextChar();
}
if(c=='>')
{
install("操作符", c.ToString(), 13);
c = nextChar();
}
else
fail(2);//如果不是">"则出错
}
else
fail(0);
state = 0;
break;
}
}
}
private void fail(int i) //出错处理函数
{
string typeerror="";;
switch(i)
{
case 0:
typeerror="输入非法字符错误";
break;
case 1:
typeerror="操作符错误";
break;
case 2:
typeerror="声明错误";
break;
}
MessageBox.Show(typeerror, "出现错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private char nextChar()
{
if(pointer>=length)return '~';//如果读完最后一个字符返回"~"表示结束
return text[pointer++];//取下一个字符
}
private void install(string type,string value,int n)
{
//str += type + n +" \"" + aValue + "\" " + "->" ;
str += " "+n + "\"" + value + "\" " + "->";//格式输出
}
private bool isLetter(char checkChar)
{
int checkInt = Convert.ToInt32(checkChar);//转换为asc码,判断是否是字符
if((checkInt<=122&&checkInt>=97)||(checkInt>=65&&checkInt<=90))
return true;
else return false;
}
private bool isDigit(char checkChar)
{
int checkInt = Convert.ToInt32(checkChar); //转换为asc码,判断是否是数字
if (checkInt >= 48 && checkInt <= 57) return true;
else return false;
}
private bool isKey(string key)//判断是否为关键字
{
switch (key)//关键字分开编码
{ //分别对应关键字编码
case "if": typeofkey = 19; break;
case "else": typeofkey = 20; break;
case "return": typeofkey = 21; break;
case "int": typeofkey = 22; break;
case "main": typeofkey = 23; break;
case "printf": typeofkey = 24; break;
default: return false;
}
return true;
//关键字统一编码
//if(checkKey.Equals("if")||checkKey.Equals("return")||
// checkKey.Equals("else")||checkKey.Equals("int")||
// checkKey.Equals("main") || checkKey.Equals("printf"))
//{
// return true;
//}
//return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -