资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
设计界面如上图所示:代码如下: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 = 打开文件; 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 = openFileDialog1.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.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.ShowEffects = 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 = 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) textBox2.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 = ; /记录识别出的单词 string 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 string 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+,-,*,/,%,=,=,=, -=,+=,*=,/=, /运算符 !=,=,%=,&,&,|,|,!,+,-,?:; 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+; /跳过无意义的字符
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号