编译原理-实验报告实验三--语法分析(LR分析程序_第1页
编译原理-实验报告实验三--语法分析(LR分析程序_第2页
编译原理-实验报告实验三--语法分析(LR分析程序_第3页
编译原理-实验报告实验三--语法分析(LR分析程序_第4页
编译原理-实验报告实验三--语法分析(LR分析程序_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上华北水利水电学院 编译原理 实验报告20122013学年 第 一 学期 2011 级 计算机科学与技术 专业班级: 学号: 姓名: 一、 实验题目:语法分析(LR分析程序)(1)选择最有代表性的语法分析方法LR分析法;(2)选择对各种常见程序语言都用的语法结构,如赋值语句(尤指表达式)作为分析对象,并且与所选语法分析方法要比较贴切。二、 实验内容(1)根据给定文法,先求出FirstVt和LastVt集合,构造算符优先关系表(要求算符优先关系表输出到屏幕或者输出到文件);(2)根据算法和优先关系表分析给定表达式是否是该文法识别的正确的算术表达式(要求输出归约过程)(3)

2、给定表达式文法为:G(E): E#E#EE+T | TTT*F |FF(E)|i(4) 分析的句子为: (i+i)*i和i+i)*i三、 根据以上文法构造出的LR(1)分析表为:+*()I#ETF0S4S51231S6Acc2R2S7R2R23R4R4R4R44S4S58235R6R6R6R66S4S5937S4S5108S6S119R1S7R1R110R3R3R3R311R5R5R5R5四、程序源代using System;using System.Text;using System.IO;namespace Syntax_Analyzerclass SyntaxStreamReader m

3、yStreamReader;int t;int lengh;int l =0;string grammar;int s=0;string Word;int w=0;int wordNum ;int n =0;int, LR;public Syntax() lengh = new int7; grammar=new string7; Word=new string100; wordNum = new int100; LR=new int30,30;public void analyzer()/读入grammarSyntax myTextRead=new Syntax();Console.Writ

4、eLine("-语法分析开始-n");/*/循环读取文法/*string strStart;strStart="grammar.txt"myTextRead.myStreamReader=new StreamReader(strStart);string strBufferStart;int uu=0;dostrBufferStart =myTextRead.myStreamReader.ReadLine();if(strBufferStart=null)break;foreach (String subString in strBufferStart.

5、Split()grammaruu=subString; /每行文法存入grammaruu+;while (strBufferStart!=null);myTextRead.myStreamReader.Close();/*/循环读取lengh/*strStart="lengh.txt"myTextRead.myStreamReader=new StreamReader(strStart);uu=0;dostrBufferStart =myTextRead.myStreamReader.ReadLine();if(strBufferStart=null)break;forea

6、ch (String subString in strBufferStart.Split()lenghuu=Convert.ToInt32(subString); /每行文法存入grammaruu+;while (strBufferStart!=null);myTextRead.myStreamReader.Close();/*/ 读入文件,进行语法分析/*string strReadFile;strReadFile="input.txt"myTextRead.myStreamReader=new StreamReader(strReadFile);string strBu

7、fferText;int wid =0;Console.WriteLine("分析读入程序(记号ID):n");dostrBufferText =myTextRead.myStreamReader.ReadLine();if(strBufferText=null)break;foreach (String subString in strBufferText.Split()if(subString!="")int ll;if(subString!=null)ll= subString.Length; /每一个长度elsebreak;int a=ll+1;

8、char b = new chara;StringReader sr = new StringReader(subString);sr.Read(b, 0, ll); /把substring 读到char数组里int sort=(int)b0;/ wordi 和wordNumi对应/先识别出一整个串,再根据开头识别是数字还是字母Wordwid=subString;if(subString.Equals("+")wordNumwid=0;else if(subString.Equals("*")wordNumwid=1;else if(subString.

9、Equals("(")wordNumwid=2;else if(subString.Equals(")")wordNumwid=3;else if(subString.Equals("i")wordNumwid=4;Console.Write(subString+"("+wordNumwid+")"+" ");wid+;Console.WriteLine("n");while (strBufferText!=null);wordNumwid=5;myTex

10、tRead.myStreamReader.Close();/*/读入LR分析表/*string strLR;strLR="LR-table.txt"myTextRead.myStreamReader=new StreamReader(strLR);string strBufferLR;int pp=0;dostrBufferLR =myTextRead.myStreamReader.ReadLine();if(strBufferLR=null)break;elseint j=0;foreach (String subString in strBufferLR.Split()

11、if(subString!=null)int lllr=Convert.ToInt16(subString);LRpp,j=lllr; /把行与列读入数组j+;pp+;while (strBufferLR!=null);myTextRead.myStreamReader.Close();int state = new int100;string symbol =new string100;state0=0;symbol0="#"int p1=0;int p2=0;Console.WriteLine("n按文法规则归约顺序如下:n");/*/归约算法/*w

12、hile(true)int j,k;j=statep2;k=wordNump1;t=LRj,k; /当出现t为的时候if(t=0)/错误类型 string error = "" ; if (k = 0) error = "+" else if (k = 1) error = "*" else if (k = 2) error = "(" else if (k = 3) error = ")" else if (k = 4) error = "i" else error = &

13、quot; 其它错误!"Console.WriteLine("n检测结果:");Console.WriteLine("代码中存在语法错误");Console.WriteLine("错误状况:错误状态编号为"+j+" 读头下符号为"+error);break;elseif(t=-100) /-100为达到接受状态Console.WriteLine("n");Console.WriteLine("n检测结果:");Console.WriteLine("代码通过

14、语法检测");break;if(t<0&&t!=-100) /归约string m=grammar-t;Console.Write(m+" "); /输出开始符int length=lengh-t; p2=p2-(length-1); Search mySearch=new Search();int right=mySearch.search(m);if(right=0)Console.WriteLine("n");Console.WriteLine("代码中有语法错误");break;int a=st

15、atep2-1;int LRresult= LRa,right;statep2=LRresult;symbolp2=m;if(t>0)p2=p2+1;statep2=t;symbolp2=Convert.ToString(wordNump1);p1=p1+1;myTextRead.myStreamReader.Close();Console.WriteLine("-语法分析结束-n");Console.Read();class Searchpublic int search(string x)string mysymbol=new string3;mysymbol0="E"mysymbol1="T"mysymbol2="F" int r = 0;for(int s=0;s<=2;s+)if(mysymbols.Equals(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论