1、设计界面如上图所示:代码如下:namespace WindowsApplication1 public partial class Form1 : Form public Form1() InitializeComponent(); string Str = , text2 = ; private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) string Fname = ; openFileDialog1.Filter = 文本文件(*.txt)|*.txt; openFileDialog1.Title = 打开文件;
2、openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() = DialogResult.OK) Fname = openFileDialog1.FileName; richTextBox1.LoadFile(Fname, RichTextBoxStreamType.PlainText); Str = richTextBox1.Text; private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) string Fname = ope
3、nFileDialog1.FileName; richTextBox1.SaveFile(Fname, RichTextBoxStreamType.PlainText); private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e) string Fname; saveFileDialog1.Filter = 文本文件(*.txt)|*.txt; saveFileDialog1.Title = 保存文件 ; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.
4、ShowDialog(); Fname = saveFileDialog1.FileName; if (Fname != ) richTextBox1.SaveFile(Fname, RichTextBoxStreamType.PlainText); private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e) Application.Exit(); private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) fontDialog1.ShowEffe
5、cts = true; fontDialog1.Font = richTextBox1.SelectionFont; if (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
6、 = richTextBox1.Text + 0; Gets getstring = new Gets(); text2 = getstring.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) text
7、Box2.Text = *符号表信息*+rn; textBox2.Text += Gets.text4; private void 错误详细信息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 = ; /记录识别出的单词 stri
8、ng text2 = ; string text3 = ; /用来记录错误信息 string wrong = ; /记录异常信息 public static int errors = 0; /错误字段的个数 public static string text5 = ; / 记录错误的详细信息 int flag1=0, flag2=0,flag3,flag4,flag5,flag6 = 0; /标记成对出现的界符 public static string text4 = 入口:单词名称 长度 类型 种属 值 内存地址+rn; /用来记录符号表 string m_KeyWords =new str
9、ing 32auto, double, int,struct, break, else,long,switch,case,enum, register,typedef,char,extern,return,union,const,float,short,unsigned,continue,for,signed,void,default, goto,sizeof,volatile,do,if,while,static ; string m_operator =new string 28+,-,*,/,%,=,=,=, -=,+=,*=,/=, /运算符 !=,=,%=,&,&,|,|,!,+,-
10、,?:; string m_bound = new string15 , , , , ;, , ., (, ), :, , #, , ,; /界符 public string GetString(string str) /读入字符串 string space = ; /空格数 if (str.Length = 0) /判断字符串是否为空 return ; try while (stri!= 0) /读入字符判断,空格、字母、数字、界符 if (stri = | stri = t | stri = r) i+; /跳过无意义的字符 else if (stri = n)/如果是换行符,则行号加1
11、LineNo+; i+; 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.ToString() + : + token + space+标识符 Token码 75 + rn; ; text4 = text4 + LineNo.ToString() + : + token + + token.Leng
12、th + space + 标识符 + + 简单变量 + + 未知 + + 未知 + rn; token = ; space=; if (j m_KeyWords.Length)/是保留字 for (int m = 0; m 12- token.Length; m+) space = space + ; text2 = text2 + LineNo.ToString() + : + m_KeyWordsj +space+ 保留字 Token码 + Convert.ToString(gettoken(token, 1) + rn; ; token = ; space = ; else if (is
13、digit(stri)/如果是数字 i = recog_dig(str, i); for (int m = 0; m 12 - token.Length; m+) space = space + ; text2 = text2 + LineNo.ToString() + : + token + space+常量 Token码 76 + rn; text4 = text4 + LineNo.ToString() + : + token + + token.Length + space + 整数 + + 简单变量 + + 未知 + + 未知 + rn; token = ; space=; else
14、 if (isbound(stri) /识别界符 i = recog_bound(str, i); for (int m = 0; m 12 - token.Length; m+) space = space + ; text2 = text2 + LineNo.ToString() + : + token + space+界符 Token码 + Convert.ToString(gettoken(token, 3) + rn; ; token = ; space=; else if (isoperator(stri) i = recog_Operator(str, i); for (int
15、m = 0; m= a & ch = A & ch = 0 & ch = 9) return true; else return false; public bool issign(char ch) /识别下划线 if (ch = _) return true; else return false; public bool isbound(char ch) /判断是否为界符 for(int j=0;jm_bound.Length;j+) if (ch.CompareTo(m_boundj0)=0) return true; return false; public bool isoperator(char ch) for (int i = 0; i m_operator.Length; i+) if (ch = m_operatori0) return true; return false; private int recog_Operator(string str, int i) char state = 0; string sstr = ; while (state != 2) switch (state)
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2