《编译方法》实验指导书.doc_第1页
《编译方法》实验指导书.doc_第2页
《编译方法》实验指导书.doc_第3页
《编译方法》实验指导书.doc_第4页
《编译方法》实验指导书.doc_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

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

文档简介

目录实验一 词法分析器设计1【实验目的】1【实验内容】1【流程图】1【源代码】2【程序部分截图】18实验二 LL(1)语法分析程序设计20【实验目的】20【实验内容】20【实验步骤和要求】20【流程图】20【源代码】21【程序截图】32实验一 词法分析器设计【实验目的】1熟悉词法分析的基本原理,词法分析的过程以及词法分析中要注意的问题。2复习高级语言,进一步加强用高级语言来解决实际问题的能力。3通过完成词法分析程序,了解词法分析的过程。【实验内容】用JAVA语言编写一个PL/0词法分析器,为语法语义分析提供单词,使之能把输入的字符串形式的源程序分割成一个个单词符号传递给语法语义分析,并把分析结果(基本字,运算符,标识符,常数以及界符)输出。初始化词法分析器【流程图】调用语法分析函数判断文字是否读完YYN从文本中读入一行字符,存于一个字符串中输出二元组及常数表,标识符表N从字符串中读出一个字符程序结束字母符号判断为何种字符不断读入,直到出现非数字符号看是否有算符或运算符与之匹配不断读入,直到出现非字母或数字符号YN判断是否为保留字将该项插入常数表中报错,出现非法字符增加一个对应的二元组YN将该项插入标识符表中增加一个对应的二元组判断是否到行尾33【源代码】package accidence_analyse;import java.io.*;import java.util.*;import buffer.*;public class AccidenceAnalyser private java.io.File SourceFile; private java.io.File ReserveFile; private java.io.File ClassFile; private java.io.File OutputFile; public Pretreatment pretreatment; public KeyWordTable keyWordTable; public ClassIdentity classIdentity; public Scaner scaner; public ConcreteScanBufferFactory csbFactory; /* * 2) 词法分析器主程序 */ public AccidenceAnalyser() System.out.println(INFOR已经建立词法分析器!); public void initAA() /创建缓冲工厂 this.csbFactory=newConcreteScanBufferFactory(); /创建字符串扫描对象 scaner = new Scaner(this); /创建pre处理对象 pretreatment=newPretreatment(SourceFile, this); /创建关键字表对象 keyWordTable= new KeyWordTable(ReserveFile); /创建对象种别码表对象 classIdentity = new ClassIdentity(ClassFile); System.out.println(INFOR已经初始化词法分析器!); public void setFilesPath(String reserveFileName, String ClassFileName,String sourceFileName, String outputFileName) /创建文件对象 SourceFile = new java.io.File(sourceFileName); /创建文件对象 ReserveFile = new java.io.File(reserveFileName); /创建文件对象 ClassFile = new java.io.File(ClassFileName); /创建文件对象 OutputFile = new java.io.File(outputFileName); /如果文件已经存在,先删除,然后建立新文件if (OutputFile.exists() OutputFile.delete();try OutputFile.createNewFile(); catch(Exceptione)e.printStackTrace(System.err); try /创建文件随机读取对象java.io.RandomAccessFile ROutputFile = new java.io.RandomAccessFile(this. OutputFile, rw); /提示信息ROutputFile.write(/n.getBytes(); ROutputFile.write( (/JAccidenceAnalyser version + getVersion() + design by yellowicq/n).getBytes(); ROutputFile.write(/java词法分析器/n.getBytes(); ROutputFile.write(/使用java语言开发/n.getBytes();ROutputFile.write(n.getBytes(); ROutputFile.write(词法分析结果如下:n.getBytes(); /关闭文件流 ROutputFile.close(); catch (Exception e) e.printStackTrace(System.err); public void startAA() /从预处理开始词法分析 this.pretreatment.startPretreatment(); public void outputAccidence(String outputString) /把分析出来的单词写入文件 outputString=n第 + this.pretreatment.fileRow + 行n + outputString; try /创建文件随机读取对象 java.io.RandomAccessFile ROutputFile = new java.io.RandomAccessFile(this.OutputFile, rw); /移动指针到文件末尾 ROutputFile.seek(ROutputFile.length(); /Start appending! ROutputFile.write(outputString.getBytes(); /关闭文件流 ROutputFile.close(); catch (Exception e) e.printStackTrace(System.err); /将分析的单词结果输出到终端 System.out.print(outputString); public void controlThread() /控制扫描器启动扫描 scaner.controlThread(); /获得版本号 public String getVersion() return 1.0; package accidence_analyse;import java.util.*;import java.io.*;public class ClassIdentity private Hashtable ClassHash; private File ClassFile; private FileReader classFileReader; /读文件对象 private int TMP_BUFFER_SIZE = 30; /* * 6) 类型种别码程序 */ public ClassIdentity(java.io.File ClassFile) System.out.println(INFOR类型种别码表已创建!); this.ClassFile = ClassFile; /查找类型种别码 public int findKey(String classWord) int KEY; for (Enumeration e = this.ClassHash.keys(); e.hasMoreElements(); ) KEY=Integer.parseInt(String)e.nextElement();if( ( (String)this.ClassHash.get(Integer.toString(KEY).equalsIgnoreCase(classWord) return KEY; return -1; public void initClassIdentityTable() ClassHash = new Hashtable(); /创建hash表 int intLength; char chrBuffer = new charTMP_BUFFER_SIZE; String classWord; int classCounter = 0; try if (ClassFile.exists() /文件存在 /创建读文件对象 classFileReader=newjava.io.FileReader(ClassFile); /读文件内容到hash表 while(intLength=classFileReader.read(chrBuffer) != -1) classCounter+; /填写hash表 classWord = String.valueOf(chrBuffer).trim(); System.out.println(INFOR读取类型种别码: KEY: + classCounter + VALUE: + classWord + ); this.ClassHash.put(Integer.toString(classCounter), classWord); /关闭读文件对象 classFileReader.close(); else /文件不存在 System.err.println(ERROR类型种别码文件不存在!); catch (Exception e) e.printStackTrace(System.err); package accidence_analyse;import java.util.*;import java.io.*;public class KeyWordTable private Hashtable KWHash; private File ReserveFile; private FileReader resFileReader; /读文件对象 private int TMP_BUFFER_SIZE = 30; /* * 5) 表留字表程序 */ public KeyWordTable(java.io.File ReserveFile) System.out.println(INFOR关键字表已创建!); this.ReserveFile = ReserveFile; public boolean isKeyWord(String inw) String resWord; /查找hash表 for (Enumeration e = this.KWHash.elements(); e.hasMoreElements(); ) resWord = (String) e.nextElement(); if (resWord.equalsIgnoreCase(inw) return true; return false; public void initKeyWordTable() KWHash = new Hashtable(); /创建hash表 int intLength; char chrBuffer = new charTMP_BUFFER_SIZE; String resWord; int resCounter = 0; try if (ReserveFile.exists() /文件存在 /创建读文件对象 resFileReader = new java.io.FileReader(ReserveFile); /读文件内容到hash表while(intLength = resFileReader.read(chrBuffer)!= -1) resCounter+;/填写hash表 resWord = String.valueOf(chrBuffer).trim(); System.out.println(INFOR读取关键字: INDEX: + resCounter +VALUE: + resWord + );this.KWHash.put(Integer.toString(resCounter), resWord); /关闭读文件对象 resFileReader.close(); else /文件不存在 System.err.println(ERROR保留字文件不存在!); catch (Exception e) e.printStackTrace(System.err); package accidence_analyse;import javax.xml.parsers.*;import org.w3c.dom.*;public class main /* * 1) 词法分析器引导文件 */ public static void main(String args) /读取配置文件,得到系统属性 String cfgString = new String4; try cfgString = main.loadAACfg(d:aaCfg.xml); catch (Exception e) e.printStackTrace(System.err); /设置待读文件名 /保留字表文件 String reserveFileName = cfgString0; /类型种别码表文件 String classFileName = cfgString1; /需要分析的源文件 String sourceFileName = cfgString2; /输出文件 String outputFileName = cfgString3; /创建词法分析器 AccidenceAnalyser aa=new AccidenceAnalyser();aa.setFilesPath(reserveFileName, classFileName, sourceFileName,outputFileName); /建立所需要的文件对象 /初始化词法分析器 aa.initAA(); /初始化关键字表 aa.keyWordTable.initKeyWordTable(); /初始化类型种别码表 aa.classIdentity.initClassIdentityTable(); /开始进行词法分析 aa.startAA(); /分析完毕 /读取配置文件 private static String loadAACfg(String name) throws Exception String cfgString = new String4; /*解析xml配置文件*/ try /*创建文档工厂*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); /*创建文档解析器*/ DocumentBuilder builder = factory.newDocumentBuilder(); /*解析配置文件*/ Document doc = builder.parse(name); /*规范化文档*/ doc.normalize(); /*查找接点表*/ NodeList nlists = doc.getElementsByTagName(FilePath); for (int i = 0; i nlists.getLength(); i+) Element item = (Element) nlists.item(i); /取得需要的配置属性 cfgString0 = item.getElementsByTagName(ReserveFileName).item(0).getFirstChild().getNodeValue().trim();cfgString1 = item.getElementsByTagName(ClassFileName).item(0). getFirstChild().getNodeValue().trim(); cfgString2 = item.getElementsByTagName(SourceFileName).item(0). getFirstChild().getNodeValue().trim(); cfgString3 = item.getElementsByTagName(OutputFileName).item(0). getFirstChild().getNodeValue().trim(); catch (Exception e) e.printStackTrace(); throw new Exception(ERROR加载配置文件 + name + 错误!); /返回属性数组 return cfgString; package accidence_analyse;import java.io.*;import buffer.*;public class Pretreatment private String tmpString; private String outputString; private int BUFFER_SIZE = 100; private AccidenceAnalyser aa; public InputBuffer inputBuffer; /输入缓冲区-共享 private java.io.File SourceFile; /文件对象 private java.io.RandomAccessFile randomAFile; /随机文件对象 public static int fileRow = 0; /* * 3) 预处理子程序 */ public Pretreatment(File SourceFile, AccidenceAnalyser aa) try this.SourceFile = SourceFile; this.randomAFile = new java.io.RandomAccessFile(this.SourceFile, r); catch (FileNotFoundException e) e.printStackTrace(System.err); this.aa = aa; inputBuffer = aa.csbFactory.createInputBuffer(BUFFER_SIZE); System.out.println(INFOR预处理器已经创建!); public void putSourceToINBuffer(String tmpString) this.inputBuffer.Data = tmpString.toCharArray(); public void putFinToSCBuffer(String filtratedString) aa.scaner.scanBuffer.Data = filtratedString.toCharArray(); public void controlThread() int intLength; int resCounter = 0; String tmpString; String filtratedString; System.out.println(INFOR开始单词分析/); try if (SourceFile.exists() /文件存在 /读文件内容到缓冲区 while ( (tmpString = this.randomAFile.readLine() != null) +fileRow; /分割符 System.out.println(.begin row + this.fileRow + .); /开始这一行分析 System.out.println(INFOR正在处理行: + String.valueOf(fileRow); /放入输入缓冲区 this.putSourceToINBuffer(tmpString); /处理字符串 filtratedString = this.filtrateSource(this.inputBuffer.Data); System.out.println(INFOR已过滤句子: + filtratedString); /放入扫描缓冲区 this.putFinToSCBuffer(filtratedString); aa.controlThread(); System.out.println( INFOR分析完毕/); else /文件不存在 System.err.println(ERROR源文件不存在!); catch (Exception e) e.printStackTrace(System.err); public String filtrateSource(char Data) String filtratedString = String.valueOf(Data).trim(); return filtratedString; public void startPretreatment() this.controlThread(); package accidence_analyse;import buffer.*;public class Scaner public ScanBuffer scanBuffer; /扫描缓冲区-共享 private String finalAccidence; private AccidenceAnalyser aa; private int BUFFER_SIZE = 100; private String toDelString; private int senLength = 0; private char sentenceChar = new char1000; private String TOKEN; private char CHAR; private int index = 0; private String IDENTITY = identity; private String DIGIT = digit; private String WORD_ERROR_INF = 在此行发现不能识别的单词,此行分析终止!; private boolean ASTATE = true; /* * 4) 扫描子程序 */ public Scaner(AccidenceAnalyser aa) this.aa = aa; initBuffer(); this.finalAccidence = ; System.out.println(INFOR扫描处理器已经创建!); public String readFromBuffer(char Data) String toDelString = String.valueOf(Data); return toDelString; public String scan(String toDelString) sentenceChar = toDelString.toCharArray(); this.senLength = sentenceChar.length; int i = 0; /分析单词 while (this.index = this.senLength) /state0: this.TOKEN = ; this.CHAR = GETBC(sentenceChar); if (this.CHAR = ;) break; /;表示这一行结束 /进入状态判断 switch (this.CHAR) /judge lettercase a:case b:case c:case d:case e:case f:case g:case h:case i:case j:case k:case l:case m:case n:case o:case p:case q:case r:case s:case t:case u:case v:case w:case x:case y:case z:case A:case B:case C:case D:case E:case F:case G:case H:case I:case J:case K:case L:case M:case N:case O:case P:case Q:case R:case S:case T:case U:case V:case W:case X:case Y:case Z: /do this.TOKEN = this.CONTACT(TOKEN, CHAR); /state1 CHAR = this.GETCHAR(sentenceChar); while (this.ISLETTER(CHAR) | this.ISDIGIT(CHAR) this.TOKEN = this.CONTACT(this.TOKEN, CHAR); CHAR = this.GETCHAR(sentenceChar); this.RETRACT(); /state2 if (aa.keyWordTable.isKeyWord(TOKEN) this.finalAccidence = this.finalAccidence + 保留字 + this.returnAWord(TOKEN) + n; else this.finalAccidence = this.finalAccidence + 标识符 + this.returnAWord(TOKEN) + 种别码 + String.valueOf(aa.classIdentity.findKey(IDENTITY) + n; /clear up token this.TOKEN = ; break; /judge ditital case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8: case 9: /do this.TOKEN = this.CONTACT(TOKEN, CHAR); /state3 CHAR = this.GETCHAR(sentenceChar); while (this.ISDIGIT(CHAR) this.TOKEN = this.CONTACT(TOKEN, CHAR); CHAR = this.GETCHAR(sentenceChar); this.RETRACT(); /state4 this.finalAccidence = this.finalAccidence + 数字 + this.returnAWord(TOKEN) + 种别码 + String.valueOf(aa.classIdentity.findKey(DIGIT) + n; /clear up token this.TOKEN = ; break; case =: /state5 this.TOKEN = this.CONTACT(TOKEN, CHAR); this.finalAccidence = this.finalAccidence + 等号 + this.returnAWord(TOKEN) + 种别码 + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + n; /clear up token this.TOKEN = ; break; case +: /state6 this.TOKEN = this.CONTACT(TOKEN, CHAR); this.finalAccidence = this.finalAccidence + 加号 + this.returnAWord(TOKEN) + 种别码 + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + n; /clear up token this.TOKEN = ; break; case *: /state7 this.TOKEN = this.CONTACT(TOKEN, CHAR); CHAR = this.GETCHAR(sentenceChar); /state8 if (CHAR = *) this.TOKEN = this.CONTACT(TOKEN, CHAR); this.finalAccidence = this.finalAccidence + 乘方 + this.returnAWord(TOKEN) + 种别码 + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + n; /state9 else this.finalAccidence = this.finalAccidence + 乘号 + this.returnAWord(TOKEN) + 种别码 + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + n; /clear up token this.TOKEN = ; break; case ,: /state10 this.TOKEN = this.CONTACT(TOKEN, CHAR); this.finalAccidence = this.finalAccidence + 逗号 + this.returnAWord(TOKEN) + 种别码 + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + n; /clear up token this.TOKEN = ; break; case (: /state11 this.TOKEN = this.CONTACT(TOKEN, CHAR); this.finalAccidence = this.finalAccidence + 左括号 + this.returnAWord(TOKEN) + 种别码 + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + n; /clear up token this.TOKEN = ; break; case ): /state12 this.TOKEN = this.CONTACT(TOKEN, C

温馨提示

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

评论

0/150

提交评论