编译原理 词法分析实验报告 java版_第1页
编译原理 词法分析实验报告 java版_第2页
编译原理 词法分析实验报告 java版_第3页
编译原理 词法分析实验报告 java版_第4页
编译原理 词法分析实验报告 java版_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

1、词法分析代码。 import java.io.*;import java.util.Vector;import java.util.regex.*;public class Compiler public static String stringCompiler=""private static final String KEY_WORD="if", "then", "while"," do"," read"," write","call&quo

2、t;,"begin", "end", "const", "var", "procedure", "odd" private static boolean isKeyWord(String s)/是否是关键字boolean b=false;for(int i=0;i<KEY_WORD.length;i+)if(s.equals(KEY_WORDi)b=true;return b; class Element String s; String kind; String ge

3、ts()return s; String getkind()return kind; Element(String s,String kind) this.s = s; this.kind = kind; static Vector<Element> sheet = new Vector<Element>(8); File result = new File("result.txt"); File result1 = new File("result1.txt"); boolean work() System.out.printl

4、n("输入PL/O语言为:"); try FileWriter fw1 = new FileWriter(result1); BufferedWriter bw1 = new BufferedWriter(fw1); FileReader fr1 = new FileReader("input.txt"); BufferedReader br1 = new BufferedReader(fr1); String s = br1.readLine(); label1: while(s!=null) for(int i = 0;i<s.length()

5、;i+) if(s.indexOf("/")!=-1) /两个'/'符 s=s.substring(0, s.indexOf("/"); s=s.replaceAll("t", " "); s=s.replaceAll("n", " "); s=s.toLowerCase(); bw1.write(s); System.out.println(s); bw1.newLine(); s = br1.readLine(); continue label1; els

6、e if(s.indexOf("/*")!=-1) / '/'符和'*'符 s=s.substring(0, s.indexOf("/*"); s.replaceAll("t", " "); s.replaceAll("n", " "); s.toLowerCase(); bw1.write(s.substring(0, i); System.out.println(s); s=s.substring(i,s.length(); while(s

7、!=null) if(s.indexOf("*/")!=-1) s=s.substring(s.lastIndexOf("*/"),s.length(); s=s.replaceAll("t", " "); s=s.replaceAll("n", " "); s=s.toLowerCase(); bw1.write(s); System.out.println(s); bw1.newLine(); s = br1.readLine(); continue label1; el

8、se s = br1.readLine(); else s=s.replaceAll("t", " "); s=s.replaceAll("n", " "); s=s.toLowerCase(); bw1.write(s); System.out.println(s); bw1.newLine(); s = br1.readLine(); continue label1; br1.close(); bw1.close(); return true; catch(IOException e) e.printStack

9、Trace(); return false; finally void analyser() try BufferedReader br = new BufferedReader(new FileReader(result1); BufferedWriter bw = new BufferedWriter(new FileWriter(result); String a = br.readLine(); while(a!=null) Matcher m = Ppile("/*.*/|'.+'|(+|-|>=|<=)|d+.d*|w+|".+&qu

10、ot;|S").matcher(a); while (m.find() checkScan(m.group(); String s=Compiler.stringCompiler; System.out.flush(); bw.write(s); Compiler.stringCompiler="" a = br.readLine(); br.close(); bw.close(); catch(IOException e) e.printStackTrace(); public void checkScan(String s)/扫描文件 if(Character

11、.isDigit(s.charAt(0) checkDigitCompiler(s); else if(s.length()=1) checkCharCompiler(s.charAt(0); else checkStringCompiler(s); private void checkDigitCompiler(String s)/检测数字 if(s.indexOf(".")!=-1) CompilerOut(Double.valueOf(s).toString(); sheet.add(new Element(s,"小数"); else Compil

12、erOut(s); sheet.add(new Element(s,"整数"); private void checkCharCompiler(char c)/检测单个字符 switch (c) case '<': sheet.add(new Element(new Character(c).toString() ,"小于号"); break; case '>': sheet.add(new Element(c+"","大于号"); break; case '=&

13、#39;: sheet.add(new Element(c+"","等于号"); break; case '+': sheet.add(new Element(c+"","加号"); break; case '-': sheet.add(new Element(c+"","减号"); break; case '*': sheet.add(new Element(c+"","乘号"); br

14、eak; case '/': sheet.add(new Element(c+"","除号"); break; case '(': sheet.add(new Element(c+"","左小括号"); break; case ')': sheet.add(new Element(c+"","右小括号"); break; case '': sheet.add(new Element(c+"",

15、"左中括号"); break; case '': sheet.add(new Element(c+"","右中括号"); break; case '': sheet.add(new Element(c+"","左大括号"); break; case '': sheet.add(new Element(c+"","右大括号"); break; case ',': sheet.add(new El

16、ement(c+"","逗号"); break; case '': sheet.add(new Element(c+"","分号"); break; case '!': sheet.add(new Element(c+"","取反"); break; case '.': sheet.add(new Element(c+"","点号"); break; case ':': s

17、heet.add(new Element(c+"","冒号"); case '?':sheet.add(new Element(c+"","问号"); break; default: sheet.add(new Element(c+"","标识符"); break; private void checkStringCompiler(String s)/检测多个字符 CompilerOut( s); if(isKeyWord(s) sheet.add(new E

18、lement(s,"关键字"); else sheet.add(new Element(s,"标识符"); private void CompilerOut(Object o)/向显示器输出结果预编译结果 if(o instanceof String) stringCompiler=stringCompiler+(String)o+" " else stringCompiler=stringCompiler+(Character)o).charValue()+" " public static void main(String args) throws Exception System.out.println("*"); Compiler c = new Compiler(); if(c.work() /System.out.println("已将处理后代码放入 result1.txt 中!"); else System.out.println("预处理失败!"); System.out.println("输入 y开始词法分析"); char k = 'y' while(k!=(ch

温馨提示

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

评论

0/150

提交评论