资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
设计界面如上图所示:代码如下:namespace WindowsApplication1public partial class Form1 : Formpublic 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 Getsint 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 , , , , ;, , ., (, ), :, , #, , = m_KeyWords.Length) /不是保留字for (int m = 0; m = a & ch = A & ch = 0 & ch | str.Substring(i - 1, 2) = += | str.Substring(i - 1, 2) = -= | str.Substring(i - 1, 2) = *= | str.Substring(i - 1, 2) = /= | str.Substring(i - 1, 2) = != | str.Substring(i - 1, 2) = %=)sstr += stri;i+;state = 2;if (stri - 1 = ? & stri = :) /三目运算符sstr += stri;i+;state = 2;else state = 2; /运算符break;token=sstr;return i;public int recog_id(string str, int i) /识别单词char state = 0;string sstr=; /记录单词while (state != 2)switch (state)case 0: if (isalpha(stri) state = 1; sstr = sstr + stri; i+; / else error(1);break; case 1:if (isalpha(stri) | isdigit(stri) | issign(stri) state = 1; sstr = sstr + stri; i+; else state = 2;break;token = sstr; /记录识别的字符串return i;public int recog_dig(string str, int i) /识别常数char state = 0;string sstr = ;while (state != 2)switch (state)case 0:if (isdigit(stri)sstr += stri;state = 1;i+;break;case 1:if (isdigit(stri)sstr += stri;state = 1;i+;else if (stri = . & isdigit(stri + 1) /实数sstr += stri;state = 1;i+;else state = 2; break;token = sstr;return i;public int recog_bound(string str, int i)string sstr = ;for (int k = 0; k )flag4+;else if (token = )flag5+;else if (token = )flag6+;return i;public int gettoken(string str,int k) /获得单词的token值switch(k)case 1:for (int i = 0;
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号