《词法分析器设计实验报告.docx》由会员分享,可在线阅读,更多相关《词法分析器设计实验报告.docx(38页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、实验一词法分析器设计一、实验目的通过本实验的编程实践,使学生了解词法分析的任务,掌握词法分析程序设计的原理和构造方法,使学生对编译的基本概念、原理和方法有完整的和清楚的理解,并能正确地、熟练地运用。二、实验内容用 VC+/VB/JAVA 语言实现对 C 语言子集的源程序进行词法分析。通过输入源程序从左到右对字符串进行扫描和分解,依次输出各个单词的内部编码及单词符号自身值;若遇到错误则显示“Error”,然后跳过错误部分继续显示 ;同时进行标识符登记符号表的管理。以下是实现词法分析设计的主要工作:(1) 从源程序文件中读入字符。(2) 统计行数和列数用于错误单词的定位。(3) 删除空格类字符,包
2、括回车、制表符空格。(4) 按拼写单词,并用(内码,属性)二元式表示。(属性值token 的机内表示)(5) 如果发现错误则报告出错7(6) 根据需要是否填写标识符表供以后各阶段使用。三、实验流程图四、实验步骤1、根据流程图编写出各个模块的源程序代码上机调试。2、 编制好源程序后,设计若干用例对系统进行全面的上机测试,并通过所设计的词法分析程序;直至能够得到完全满意的结果。3、书写实验报告 ;实验报告正文的内容:五、实验结果已 I执行 ma in()if(a 2 )b=3;l单词二元序列来六开位愚(行,列)maln(9, main)保留字(L 1)v((53, ()定界符(L 2))(54,)
3、定界符(L 3)(55, )定界符(1, 4)if(1, i f)保留字(2, 1)((53, ()定界符(2, 2)a(40, a)标识符(2, 3)((47, 46* printf847* main948* scanf10=50*=51*;52*(53*)54*55*56*+57*-58*出错符-1*#0*/public partial class Form1 : Form/*成员变量*/String text = ;Char text_is;/输入流int type_code=10;/单词种别码int number;/整形常量int line=1;/行号int column = 1;/列
4、号char ch;int p = 0;Stringkeyword=newStringif,else,then, begin,end,printf,main,scanf,return;public Form1()while,do,InitializeComponent();/* 对输入流进行操作*/private void readIo() text = ; trych = text_isp+;catch (Exception);/*预处理*/ while (ch = )ch = text_isp; p+;if (ch = a & ch = A & ch = Z)/标识符或变量名检测= 0 &
5、ch = a & ch = A & chtext = text + ch; ch = text_isp+;p-;type_code = 40;for (int i = 0; i= 0 & ch = a & text_isp = A & text_isp = 0 & ch 32767) number = -1;else switch (ch)/其他字符case )type_code = 48; text = text + ch;else if (ch = =)elsetype_code = 49; text = text + ch;type_code = 47; p-;break;case :
6、text = text + ch; ch = text_isp+; if (ch = =)elsetype_code = 50; text = text + ch;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;elseelse p-;type_code = 57;type_code = 44; p-;break;case -: t
7、ext = text + ch; ch = text_isp+; if (ch = -)text = text + ch; ch = text_isp+;if (ch = + | ch = -)text = text + ch; type_code = -1;elsetype_code = 58;elsep-;type_code = 45; p-;break;case *: type_code = 42; text = +ch; break; case /: type_code = 43; text = + ch; break; case =: type_code = 51; text = +
8、 ch; break; case ;: type_code = 52; text = + ch; break; case (: type_code = 53; text = + ch; break; case ): 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; bre
9、ak;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.ReadAllText(path, Encoding.Defau
10、lt);private String getTextIo()return richTextBox1.Text+#;private void addData(String data1,String data2,String data3,String data4) tryDataGridViewRow dgr = new DataGridViewRow(); DataGridViewTextBoxCell dt1 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dt2 = new DataGridViewTextBoxCell();
11、 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.Cells.Add(dt4);dataGridView1.Rows.Add(
12、dgr);catch (Exception);private void button2_Click(object sender, EventArgs e)type_code = 10;p = 0;line = 1;column = 1; trydataGridView1.Rows.Clear();catch (Exception);text_is = getTextIo().ToCharArray(); doreadIo();switch (type_code)case 41:addData(number+, (+ type_code+ , + number + ),常量,(+line+,+c
13、olumn+);column+; break;case -1:addData(text + , ( + type_code + , + text + ), 有错误, (+ line + , + column + );column+; break;case -2:line+;column = 1; break;case -3:break; default: if(type_code0&type_code52&type_code41&type_codeTGFOP FUSH(GT)S-)FS|汀52#GSFi 计 i 廿 T-FSPOP PUSH(SF) S-)F-(E)F-)i兀 RSr 茶E的正
14、r st 策为:(1 T 的 Fm t 某 为 (, 啪 F 江 st 集为:气F的F立st.il, 为 (i渺 F 立 st 某 为 膘冲I唱I3451 DllllW案 6E酌Foll ow集为: )?T 的 Foll ow 某 为 ) 七如 Foll ow集为: )8F的 Foll ow兰为 廿)幻95 的 Foll ew 某 为 尸10111213()114E-)TGE-TG15T-FST-FS16#GS,GS #GSF*GSr #GSi #GSG #GT+GT #GSF #GS1 #GS #G1+:1 +i:11 F沁灶i#杠 i# S-*FS i +iO,过 F-i+1.+i 廿沌+
15、i :11 仓 TG i #讨 T- FS1:11 F沁廿 邓炉邓POP PUSH(1)GETNEXT(I) POP PUSH(SFI:) GETNEXT(I) POP PUSH(i) GET 邯 XT (I ) POPPOP PUSH(GT+) GETNEXT(I) POP PUSH(SF) FOP PUSH(立lGETNEXT(I)POP POPGG-)+TGFsS-)G-)-TG5邓S-*FS$)/FSF-(E)仓)F-i仓 )0S) .听_文法韵入串E-TGG-)+TGI-TG G-)T-FSS-)*FS I/FS S-)F-(E)F- )ii*i+11叫FI RST栠FOUD YI
16、栠E的Fi r s t 焦为:( i T的Fi r s t 焦为:( i 啪 Fi r s t 焦为:+- F的Fi r s t 焦为:( iS 的Fi r s t 焦为: E的Foll ow焦为: )T的Foll ow焦为: )+-啪 Foll ow焦为: )F的Foll ow焦为: )+-:+:/S的Foll ow焦为: )+-畔石卞寸I(),l目E-TGE-TGTT-FST-FSGG-+TGG-TGG- G- FF-(E)F-isS-)S-)S-*FSS- )/F SS-)S-)六、实验代码( C#)using System;using System.Collections.Generi
17、c; 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_1_分析法public partial class Form1 : Formpublic Production production; public char terminator;/终结符public char nonterminal;/非终
18、结符public int flag = 0;public String Terminator = ; String STACK = ;/符号栈String stack = ;/输入串public struct Production public Char noter;public String production;/产生式结构体public struct FIRST public Char noter; public String first;/FIRST 集结构体public struct FOLLOW public Char noter; public String follow;/FO
19、LLOW 集结构体public struct PLACE public bool flag; public int x; public int y;public Form1()InitializeComponent();private Char getNonterminal(Char s)String nonterminal = ;for (int i = 0; i = A & si = Z) if (!nonterminal.Contains(si) nonterminal = nonterminal + si;return nonterminal.ToCharArray();private
20、 Char getTerminator(Char s)String terminator = ;for (int i = 0; i = a & si = z) | si = ( | si = ) | si = + | si = - | si = * | si = /|si=)if (!terminator.Contains(si) terminator = terminator + si;Terminator = terminator.Replace(, ) + #; return terminator.ToCharArray();private Production getProductio
21、ns(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 = $)/-productionsindex.noter = si; i = i + 3;while (si != &si!=n)if (flag = 0)+si;+ si;elseproductionsindex.production =productionsindex.production i
22、+;productionsindex.noter = productionsindex - 1.noter; productionsindex.production = productionsindex.productioni+;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 +
23、si;return str;private FIRST getFirst(Char c) FIRST First = new FIRST(); First.noter = c;for (int i = 0; i 20; i+)if (productioni.noter = c)if (terminator.Contains(productioni.production0)First.first = First.first + productioni.production0; if (productioni.production0 = )flag = 1;else if (nonterminal
24、.Contains(productioni.production0)First.first = First.first + getFirst(productioni.production0).first; if(flag=1)flag = 0;First.first getFirst(productioni.production1).first;=First.first+First.first = getSingleString(First.first); return First;private int getIndexOfString(String s,Char c) for (int i
25、 = 0; i s.Length; i+)if (si = c)return i;return 0;private FOLLOW getFollow(Char c)FOLLOW Follow = new FOLLOW(); Follow.noter = c;if (nonterminal0 = c)Follow.follow = Follow.follow + #; for (int i = 0; i 20; i+)if (productioni.production != null) if (productioni.production.Contains(c)int index = getI
26、ndexOfString(productioni.production,c); if (index+1)productioni.production.Length)if (terminator.Contains(productioni.productionindex + 1) Follow.follow=Follow.follow+productioni.productionindex + 1.ToString().Replace(,);else if (nonterminal.Contains(productioni.productionindex + 1)if(getFirst(produ
27、ctioni.productionindex+ 1).first.Contains() & productioni.noter!=c)getFollow(productioni.noter).follow;Follow.follow=Follow.follow+Follow.follow= getFirst(productioni.productionindex + 1).first.Replace(, );Follow.follow+else if (productioni.noter!=c) Follow.follow=Follow.follow+getFollow(productioni
28、.noter).follow;Follow.follow = getSingleString(Follow.follow); return Follow;private String getStringFirst(String s) String str = ;if (s=null|s.Length1);else if (terminator.Contains(s0) str = str + s0;else if (nonterminal.Contains(s0)if (getFirst(s0).first.Contains()str=str+getFirst(s0).first.Replac
29、e(,)+ getStringFirst(s.Replace(s0.ToString(), );elsestr = str + getFirst(s0).first;return str;private String ,getAnalysis_Sheet(FIRST First, FOLLOW Follow) String ,sheet=new String20,20;for (int i = 0; i nonterminal.Length; i+)for (int j = 0; j Terminator.Length; j+)if (Firsti.first.Contains(Terminatorj)for (int m = 0; m +productionm.production;if(Firsti.first.Contains()&Followi.follow.Contains(Terminatorj)sheeti, j = nonterminali + - + ;return sheet;private void addData(String, sheet) tryfor (int i = 0; i Terminator.Length; i+)DataGridViewTextBoxColumndgc