1、 richTextBox1.Load, RichTextBoxStreamType.PlainText); Str = richTextBox1.Text; private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) string Fname = open; richTextBox1.Save, RichTextBoxStreamType.PlainText); private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e) string Fname
2、; save = 保存文件 save = true; save(); Fname = save; if (Fname != ) private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e) Application.Exit(); private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) fontDialog1.ShowEffects = true; fontDialog1.Font = richTextBox1.SelectionFont; if
3、(fontDialog1.ShowDialog() = DialogResult.OK) richTextBox1.SelectionFont = fontDialog1.Font; private void 词法分析ToolStripMenuItem_Click(object sender, EventArgs e) Gets.LineNo = 1; /初始化行号 Gets.errors = 0; /初始化错误个数 Gets.text5 = string text1 = richTextBox1.Text + 0 Gets getstring = new Gets(); text2 = ge
4、tstring.GetString(text1); richTextBox2.Text = *Token串生成表如下* + rn + text2; textBox2.Text = getstring.ErrorN0(); private void Form1_Load(object sender, EventArgs e) private void 符号表ToolStripMenuItem_Click(object sender, EventArgs e) textBox2.Text = *符号表信息*+ textBox2.Text += Gets.text4; private void 错误
5、详细信息ToolStripMenuItem_Click(object sender, EventArgs e) textBox2.Text = Gets.text5;主要实现功能的代码如下:创建一个Gets类用来对输入的字符串进行扫描 class Gets int i = 0, j = 0; /记录字符位置,token数组的位置 public static int LineNo = 1; /行号 string token = /记录识别出的单词 string text2 = string text3 = /用来记录错误信息 string wrong = /记录异常信息 public stati
6、c int errors = 0; /错误字段的个数 public static string text5 = / 记录错误的详细信息 int flag1=0, flag2=0,flag3,flag4,flag5,flag6 = 0; /标记成对出现的界符 public static string text4 = 入口:单词名称 长度 类型 种属 值 内存地址 /用来记录符号表 string m_KeyWords =new string 32auto, doubleint,structbreakelselongswitchcaseenum, registertypedefcharexternr
7、eturnunionconstfloatshortunsignedcontinueforsignedvoiddefaultgotosizeofvolatiledoifwhilestatic ; string m_operator =new string 28-*/%=-=+=*=/=, /运算符!%=&|+-?:; string m_bound = new string15 .()# , /界符 public string GetString(string str) /读入字符串 string space = /空格数 if (str.Length = 0) /判断字符串是否为空 return
8、 try while (stri!= ) /读入字符判断,空格、字母、数字、界符 if (stri = | stri = tr i+; /跳过无意义的字符 else if (stri = n) /如果是换行符,则行号加1 LineNo+; else if (isalpha(stri) /如果是字母 i = recog_id(str, i); for (j = 0; j = m_KeyWords.Length) /不是保留字 for (int m = 0; m 12 - token.Length; m+) space = space + text2 = text2 + LineNo.ToStri
9、ng() + + token + space+标识符 Token码 75 ; text4 = text4 + LineNo.ToString() + + token + + token.Length + space + 标识符简单变量未知 未知 token = space= if (j m_KeyWords.Length) /是保留字 12- token.Length; + m_KeyWordsj +space+ 保留字 Token码 + Convert.ToString(gettoken(token, 1) + space = else if (isdigit(stri) /如果是数字 i = recog_dig(str, i);常量 Token码 76整数 else if (isbound(stri) /识别界符 i = recog_bound(str, i);界符 Token码 + Convert.ToString(gettoken(token, 3) + else if (isoperator(stri) i = recog_Operator(st
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1