版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算机与信息学院(信息工程系) 编译原理 实验报告专 业 班 级 12级计算机二班 学生姓名及学号 孙铭银2012217166 课程教学班号 计算机二班 任 课 教 师 李宏芒 实验指导教师 李宏芒 实验地点 计算机中心第四机房 2014 2015学年 第一学期实验一 词法分析器设计一、实验目的 通过本实验的编程实践,使学生了解词法分析的任务,掌握词法分析程序设计的原理和构造方法,使学生对编译的基本概念、原理和方法有完整的和清楚的理解,并能正确地、熟练地运用。 二、实验内容 用 VC+/VB/JAVA 语言实现对 C 语言子集的源程序进行词法分析。通过输入源程序从左到右对字符串进行扫描和分解,
2、依次输出各个单词的内部编码及单词符号自身值;若遇到错误则显示“Error”,然后跳过错误部分继续显示 ;同时进行标识符登记符号表的管理。 以下是实现词法分析设计的主要工作: (1)从源程序文件中读入字符。 (2)统计行数和列数用于错误单词的定位。 (3)删除空格类字符,包括回车、制表符空格。 (4)按拼写单词,并用(内码,属性)二元式表示。(属性值token 的机内表示) (5)如果发现错误则报告出错 7(6) 根据需要是否填写标识符表供以后各阶段使用。 三、实验流程图四、实验步骤1、根据流程图编写出各个模块的源程序代码上机调试。 2、 编制好源程序后,设计若干用例对系统进行全面的上机测试,并
3、通过所设计的词法分析程序;直至能够得到完全满意的结果。 3、 书写实验报告 ;实验报告正文的内容: 五、实验结果六、实验代码(C#)using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 词法分析器 /*单词符号 种别码 单词
4、符号 种别码 * if 1 标识符 40 * else 2 常量 41 * then 3 * 42 * while 4 / 43 * do 5 + 44 * begin 6 - 45 * end 7 > 46 * printf 8 < 47 * main 9 <> 48 * scanf 10 <= 49 * return 11 >= 50 * = 51 * ; 52 * ( 53 * ) 54 * 55 * 56 * + 57 * - 58 * 出错符 -1 * # 0 * */ public partial class Form1 : Form /* *成
5、员变量 */ String text = "" Char text_is;/输入流 int type_code=10;/单词种别码 int number;/整形常量 int line=1;/行号 int column = 1;/列号 char ch; int p = 0; String keyword=new String"if", "else","then", "while", "do", "begin","end","pr
6、intf","main","scanf","return" public Form1() InitializeComponent(); /* * 对输入流进行操作 */ private void readIo() text = "" try ch = text_isp+; catch (Exception) ; /*预处理*/ while (ch = ' ') ch = text_isp; p+; if (ch >= 'a' && ch <= &
7、#39;z') | (ch >= 'A' && ch <= 'Z') /标识符或变量名检测 while (ch >= '0' && ch <= '9') | (ch >= 'a' && ch <= 'z') | (ch >= 'A' && ch <= 'Z') text = text + ch; ch = text_isp+; p-; type_co
8、de = 40; for (int i = 0; i< keyword.Length; i+) /保留字检测 if (text=keywordi) type_code = i + 1; break; else if (ch >= '0' && ch <= '9') /数字检测 if (text_isp >= 'a' && text_isp <= 'z') | (text_isp >= 'A' && text_isp <=
9、39;Z') while (ch!=' '&&ch!=''&&ch!='#') text = text + ch; ch = text_isp+; type_code = -1; else number = 0; while (ch >= '0' && ch <= '9') number = number * 10 + ch - '0' ch = text_isp+; p-; type_code = 41; if (number &
10、gt; 32767) number = -1; else switch (ch) /其他字符 case '<':text=text+ch; ch = text_isp+; if (ch = '>') type_code = 48; text = text + ch; else if (ch = '=') type_code = 49; text = text + ch; else type_code = 47; p-; break; case '>': text = text + ch; ch = text_is
11、p+; if (ch = '=') type_code = 50; text = text + ch; else type_code= 46; p-; break; case '+': text = text + ch; ch = text_isp+; if (ch = '+') text = text + ch; ch = text_isp+; if(ch='+'|ch='-') text = text + ch; type_code = -1; else type_code = 57; p-; else typ
12、e_code = 44; p-; break; case '-': text = text + ch; ch = text_isp+; if (ch = '-') text = text + ch; ch = text_isp+; if (ch = '+' | ch = '-') text = text + ch; type_code = -1; else type_code = 58; p-; else type_code = 45; p-; break; case '*': type_code = 42; te
13、xt = ""+ch; break; case '/': type_code = 43; text = "" + ch; break; case '=': type_code = 51; text = "" + ch; break; case '': type_code = 52; text = "" + ch; break; case '(': type_code = 53; text = "" + ch; break; case
14、 ')': type_code = 54; text = "" + ch; break; case '': type_code = 55; text = "" + ch; break; case '': type_code = 56; text = "" + ch; break; case '#': type_code = 0; text = "" + ch; break; case 'n': type_code = -2; break
15、; case 'r': type_code = -3; break; default: type_code = -1; break; private void button1_Click(object sender, EventArgs e) String path = "" OpenFileDialog op = new OpenFileDialog(); if (op.ShowDialog() = DialogResult.OK) path = op.FileName; richTextBox1.Text = System.IO.File.ReadAll
16、Text(path, Encoding.Default); private String getTextIo() return richTextBox1.Text+'#' private void addData(String data1,String data2,String data3,String data4) try DataGridViewRow dgr = new DataGridViewRow(); DataGridViewTextBoxCell dt1 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCel
17、l dt2 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dt3 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dt4 = new DataGridViewTextBoxCell(); dt1.Value = data1; dt2.Value = data2; dt3.Value = data3; dt4.Value = data4; dgr.Cells.Add(dt1); dgr.Cells.Add(dt2); dgr.Cells.Add(dt3); dgr
18、.Cells.Add(dt4); dataGridView1.Rows.Add(dgr); catch (Exception) ; private void button2_Click(object sender, EventArgs e) type_code = 10; p = 0; line = 1; column = 1; try dataGridView1.Rows.Clear(); catch (Exception) ; text_is = getTextIo().ToCharArray(); do readIo(); switch (type_code) case 41: addD
19、ata(number+"", "("+ type_code+ "," + number + ")","常量","("+line+","+column+")"); column+; break; case -1: addData(text + "", "(" + type_code + "," + text + ")", "有错误", &q
20、uot;(" + line + "," + column + ")"); column+; break; case -2:line+; column = 1; break; case -3: break; default: if(type_code>0&&type_code<40) addData(text + "", "(" + type_code + "," + text + ")", "保留字", "(
21、" + line + "," + column + ")"); column+; else if(type_code=40) addData(text + "", "(" + type_code + "," + text + ")", "标识符", "(" + line + "," + column + ")"); column+; else if(type_code>52&
22、;&type_code<57) addData(text + "", "(" + type_code + "," + text + ")", "定界符", "(" + line + "," + column + ")"); column+; else if(type_code=52) addData(text + "", "(" + type_code + ","
23、 + text + ")", "句尾符", "(" + line + "," + column + ")"); column+; else if (type_code = 0) addData(text + "", "(" + type_code + "," + text + ")", "结束符", "(" + line + "," + column + &
24、quot;)"); column+; else if (type_code >41&&type_code<52)|type_code=57|type_code=58) addData(text + "", "(" + type_code + "," + text + ")", "运算符", "(" + line + "," + column + ")"); column+; break; while
25、 (type_code!=0); private void Form1_Load(object sender, EventArgs e) private void richTextBox1_TextChanged(object sender, EventArgs e) 七、实验总结此次实验让我了解了如何设计、编制并调试词法分析程序,并加深了我对词法分析器原理的理解;熟悉了直接构造词法分析器的方法和相关原理,并学会使用C#语言直接编写词法分析器;同时更熟练的掌握用C#语言编写程序,实现一定的实际功能。实验二 LL(1)分析法一、实验目的 通过完成预测分析法的语法分析程序,了解预测分析法和递归子程
26、序法的区别和联系。使学生了解语法分析的功能,掌握语法分析程序设计的原理和构造方法,训练学生掌握开发应用程序的基本方法。有利于提高学生的专业素质,为培养适应社会多方面需要的能力。 二、实验内容 u 根据某一文法编制调试 LL ( 1 )分析程序,以便对任意输入的符号串进行分析。 u 构造预测分析表,并利用分析表和一个栈来实现对上述程序设计语言的分析程序。 u 分析法的功能是利用 LL(1)控制程序根据显示栈栈顶内容、向前看符号以及 LL(1)分析表,对输入符号串自上而下的分析过程。 三、实验流程图四、实验步骤1、根据流程图编写出各个模块的源程序代码上机调试。 2、 编制好源程序后,设计若干用例对
27、系统进行全面的上机测试,并通过所设计的 LL(1)分析程序;直至能够得到完全满意的结果。 3、 书写实验报告 ;实验报告正文的内容:五、实验结果六、实验代码(C#)using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace LL_
28、1_分析法 public partial class Form1 : Form public Production production; public char terminator;/终结符 public char nonterminal;/非终结符 public int flag = 0; public String Terminator = "" String STACK = ""/符号栈 String stack = ""/输入串 public struct Production public Char noter; pub
29、lic String production; /产生式结构体 public struct FIRST public Char noter; public String first; /FIRST集结构体 public struct FOLLOW public Char noter; public String follow; /FOLLOW集结构体 public struct PLACE public bool flag; public int x; public int y; public Form1() InitializeComponent(); private Char getNont
30、erminal(Char s) String nonterminal = "" for (int i = 0; i < s.Length; i+) if (si >= 'A' && si <= 'Z') if (!nonterminal.Contains(si) nonterminal = nonterminal + si; return nonterminal.ToCharArray(); private Char getTerminator(Char s) String terminator = &quo
31、t;" for (int i = 0; i < s.Length; i+) if (si >= 'a' && si <= 'z') | si = '(' | si = ')' | si = '+' | si = '-' | si = '*' | si = '/'|si='') if (!terminator.Contains(si) terminator = terminator + si; Terminat
32、or = terminator.Replace("", "") + '#' return terminator.ToCharArray(); private Production getProductions(Char s) int index = 0; Production productions=new Production20; for (int i = 0; i < s.Length - 1; i+) int flag = 0; if (nonterminal.Contains(si) && si + 1 =
33、 '$')/- productionsindex.noter = si; i = i + 3; while (si != ' '&&si!='n') if (flag = 0) duction =duction +si; i+; else productionsindex.noter = productionsindex - 1.noter; duction = duction + si; i+; if (si = '|') flag = 1; index+; i+; index+; return productions; /获取单一字符串 private String getSingleString(String s) String str = "" for (int i = 0; i < s.Length; i+) if (!str.Contains(si) str = str + si; re
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年兴安盟辅警招聘考试真题含答案详解(典型题)
- 2023年肇庆辅警招聘考试真题含答案详解(培优b卷)
- 2023年百色辅警协警招聘考试真题附答案详解(综合卷)
- 2023年葫芦岛辅警协警招聘考试真题附答案详解(考试直接用)
- 2023年酒泉辅警招聘考试真题及答案详解(网校专用)
- 2024年临沧辅警招聘考试题库含答案详解(典型题)
- 2023年金华辅警协警招聘考试真题附答案详解(b卷)
- 2024年南阳辅警协警招聘考试备考题库及答案详解(各地真题)
- 2024年娄底辅警招聘考试真题及答案详解(易错题)
- 2024年安康辅警协警招聘考试备考题库及答案详解(考点梳理)
- 2024年国家开放大学电大开放英语考试题题库
- 《涡流检测》课件
- 数电票商品税收分类编码表
- MOOC 光学发展与人类文明-华南师范大学 中国大学慕课答案
- 设备安装监理细则
- 大创申报答辩ppt
- 《活出最乐观的自己》读书笔记思维导图PPT模板下载
- 高中地理 人教版 选修二《资源、环境与区域发展》第五课时:玉门之变-玉门市的转型发展
- 催化加氢技术(药物合成技术课件)
- 近三年(2023-2023年)广西物理学业水平考试试题
- 建筑结构检测与加固课程复习考试试题及答案B
评论
0/150
提交评论