词法分析器用C实现

上传人:人*** 文档编号:431749709 上传时间:2023-11-05 格式:DOC 页数:13 大小:135.51KB
返回 下载 相关 举报
词法分析器用C实现_第1页
第1页 / 共13页
词法分析器用C实现_第2页
第2页 / 共13页
词法分析器用C实现_第3页
第3页 / 共13页
词法分析器用C实现_第4页
第4页 / 共13页
词法分析器用C实现_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《词法分析器用C实现》由会员分享,可在线阅读,更多相关《词法分析器用C实现(13页珍藏版)》请在金锄头文库上搜索。

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+,-,*,/,%,=,=,=, -=,+=,*=,/=, /运算符 !=,=,%=,&,&,|,|,!,+,-,?:; 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+; /跳过无意义的字符

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号