📄 form1.cs
字号:
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();
textBox1.Text = CountError.ToString();
textBox6.Text = CountDouble.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,CountDouble;
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)
{
listBox2.Items.Add( "(" + c + "," + s + ")" );
} //输出信息
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[21];
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);
SignTable[20] = new Keyword("浮点数", DOUBLE);
listBox1.Items.Add("类型码 单词符号");
foreach (Keyword t in SignTable)
{
listBox1.Items.Add(" " + t.value + "\t " + t.key);
}
foreach (Keyword t in KeywordTable)
{
listBox1.Items.Add(" " + t.value + "\t " + t.key);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string ResultText = string.Empty, myText;
for (int i = 0; i < listBox2.Items.Count; i++)
{
ResultText += listBox2.GetItemText(listBox2.Items[i]) + Environment.NewLine;
}
myText = "*****原文件内容为*****" + textBox4.Text + Environment.NewLine + "**********************" + Environment.NewLine + Environment.NewLine + "****词法分析结果为****" + Environment.NewLine + ResultText + "**********************" + 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();
listBox2.Items.Clear();
textBox10.Clear();
textBox9.Clear();
textBox8.Clear();
textBox7.Clear();
textBox1.Clear();
textBox6.Clear();
}
private void button6_Click(object sender, EventArgs e)
{
try
{
int n = 0;
string[] temp;
using (TextFieldParser myReader = new TextFieldParser(@"D:\T3.txt"))
{
while (!myReader.EndOfData)
{
myReader.ReadLine();
n++;
}
}
using (TextFieldParser myReader = new TextFieldParser(@"D:\T3.txt"))
{
myReader.TextFieldType = FieldType.FixedWidth;
myReader.SetFieldWidths(-1);
KeywordTable = new Keyword[n];
for (int i=0;n>0 ;i++,n-- )
{
temp = myReader.ReadFields();
KeywordTable[i] = new Keyword(temp[0],22+i);
}
listBox1.Items.Clear();
listBox1.Items.Add("类型码 单词符号");
foreach (Keyword t in SignTable)
{
listBox1.Items.Add(" " + t.value + "\t " + t.key);
}
foreach (Keyword t in KeywordTable)
{
listBox1.Items.Add(" " + t.value + "\t " + t.key);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button8_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
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[21];
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);
SignTable[20] = new Keyword("浮点数", DOUBLE);
listBox1.Items.Add("类型码 单词符号");
foreach (Keyword t in SignTable)
{
listBox1.Items.Add(" " + t.value + "\t " + t.key);
}
foreach (Keyword t in KeywordTable)
{
listBox1.Items.Add(" " + t.value + "\t " + t.key);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -