form1.cs
来自「一个用C#编写的解释器」· CS 代码 · 共 507 行 · 第 1/2 页
CS
507 行
else Out(SUB, "");
CountSign++;
break;
case '+':
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
if (ch == '+')
{ stemp += ch; Out(INC, ""); CountSign++; MyStreamReader.Read(); }
else Out(ADD, "");
CountSign++;
break;
default:
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
while (MyStreamReader.Peek() != -1 && !IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
}
Out(ERROR, stemp);
CountError++;
break;
}
textBox7.Text = CountKeyword.ToString();
textBox8.Text = CountID.ToString();
textBox9.Text = CountInt.ToString();
textBox10.Text = CountSign.ToString();
}
MyStreamReader.Close();
////////////////////////////////////////////////////////////////////////////
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
private bool isalpha(int c)
{
if ((c <= 'z' && c >= 'a') | (c <= 'Z' && c >= 'A'))
return true;
else return false;
} //判断是否为大小写字母
private bool isalnum(int c)
{
if ((c <= 'z' && c >= 'a') | (c <= 'Z' && c >= 'A') | (c <= '9' && c >= '0'))
return true;
else return false;
} //判断是否为大小写字母或数字
private bool isdigit(int c)
{
if (c <= '9' && c >= '0')
return true;
else return false;
} //判断是否为数字
private bool isrelationsign(int c) //判断是否为关系符
{
if (c == '>' | c == '<' | c == '=')
return true;
else return false;
}
private bool isoperator(int c) //判断是否为关系符
{
if (c == '+' | c == '-' | c == '*' | c == '/' | c == '(' | c == ')')
return true;
else return false;
}
class Keyword
{
public string key;
public int value;
public Keyword(string s, int v)
{
this.key = s;
this.value = v;
}
} //关键字类定义
Keyword[] KeywordTable;
Keyword[] SignTable;
uint CountInt, CountID, CountKeyword, CountSign,CountError;
private int lookup(string s)
{
foreach (Keyword t in KeywordTable)
{
if (s == t.key)
return t.value;
}
return 0;
} //判断输入串是否在关键字表中
private void Out(int c, string s)
{
textBox1.Text += "(" + c + "," + s + ") "+Environment.NewLine;
} //输出信息
private bool IsBlank(char c)
{
if (c == ' ' | c == '\t' | c=='\n')
return true;
else return false;
} //是空白符吗
private bool IsListSeparatorOrBlank(char c)
{
if (c == ' ' | c == '\t' | c=='\n'|c==','|c==';'|c=='\r')
return true;
else return false;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text += (@"D:\T1.txt");
textBox5.Text += (@"D:\T2.txt");
textBox3.Text += "分析结果是形如(CLASS,VALUE)的二元表达式,CLASS为类型码,VALUE为其值。若当前分析的串为关键字,则VALUE为空。另外,我们约定CLASS为-1时表示此串为非法串(查错功能)";
KeywordTable = new Keyword[12];
KeywordTable[0] = new Keyword("begin", BEGIN);
KeywordTable[1] = new Keyword("end", END);
KeywordTable[2] = new Keyword("if", IF);
KeywordTable[3] = new Keyword("then", THEN);
KeywordTable[4] = new Keyword("else", ELSE);
KeywordTable[5] = new Keyword("while", WHILE);
KeywordTable[6] = new Keyword("do", DO);
KeywordTable[7] = new Keyword("var", VAR);
KeywordTable[8] = new Keyword("integer", INTEGER);
KeywordTable[9] = new Keyword("procedure", PROCEDURE);
KeywordTable[10] = new Keyword("parbegin", PARBEGIN);
KeywordTable[11] = new Keyword("parend", PAREND);
SignTable = new Keyword[20];
SignTable[0] = new Keyword("<", LT);
SignTable[1] = new Keyword("<=", LE);
SignTable[2] = new Keyword("=", EQ);
SignTable[3] = new Keyword("<>", NE);
SignTable[4] = new Keyword(">", GT);
SignTable[5] = new Keyword(">=", GE);
SignTable[6] = new Keyword("+", ADD);
SignTable[7] = new Keyword("-", SUB);
SignTable[8] = new Keyword("*", MUL);
SignTable[9] = new Keyword("/", DIV);
SignTable[10] = new Keyword("++", INC);
SignTable[11] = new Keyword("--", DEC);
SignTable[12] = new Keyword(" ( ", LP);
SignTable[13] = new Keyword(" ) ", RP);
SignTable[14] = new Keyword("/*", LN);
SignTable[15] = new Keyword("*/", RN);
SignTable[16] = new Keyword(" { ", LFP);
SignTable[17] = new Keyword(" } ", RFP);
SignTable[18] = new Keyword("标识符", ID);
SignTable[19] = new Keyword("整数", INT);
textBox6.Text += "类型码 单词符号"+Environment.NewLine;
foreach (Keyword t in SignTable)
{
textBox6.Text += " "+t.value + "\t\t" + t.key + Environment.NewLine;
}
foreach (Keyword t in KeywordTable)
{
textBox6.Text += " "+t.value+"\t\t"+t.key+ Environment.NewLine ;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string myText;
myText = "*****原文件内容为*****" + textBox4.Text + Environment.NewLine + "**********************" + Environment.NewLine + Environment.NewLine + "****词法分析结果为****" + Environment.NewLine + textBox1.Text + "**********************" + Environment.NewLine + "建立文件时间:" + System.DateTime.Now.ToString() + Environment.NewLine;
File.WriteAllText(textBox5.Text, myText, Encoding.Default);
MessageBox.Show("成功写入文件","操作成功",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
string myText;
string mypath=@"D:\范例.txt";
myText = Environment.NewLine + " begin" + Environment.NewLine + "\tS1=123+(3+2*5+3/1234);" + Environment.NewLine + "\tS2=456;" + Environment.NewLine + "\tif S1<=S2 then S1=S2;" + Environment.NewLine + "\telse S2=S1;" + Environment.NewLine + " end";
File.WriteAllText(mypath, myText, Encoding.Default);
textBox2.Clear();
textBox2.Text += @"D:\范例.txt";
MessageBox.Show("成功生成范例,您可以开始分析范例文件", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(@"D:\范例.txt"))
{
File.Delete(@"D:\范例.txt");
textBox2.Clear();
textBox2.Text += @"D:\T1.txt";
MessageBox.Show("成功删除范例", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
MessageBox.Show("范例不存在或已被删除", "操作无效", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button5_Click(object sender, EventArgs e)
{
textBox4.Clear();
textBox1.Clear();
textBox10.Clear();
textBox9.Clear();
textBox8.Clear();
textBox7.Clear();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?