《编译原理-词法分析实验报告-java版(共7页).doc》由会员分享,可在线阅读,更多相关《编译原理-词法分析实验报告-java版(共7页).doc(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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,begin, end, const, var, procedure, odd; private static boolean isKeyWord(String
2、 s)/是否是关键字boolean b=false;for(int i=0;iKEY_WORD.length;i+)if(s.equals(KEY_WORDi)b=true;return b; class Element String s; String kind; String gets()return s; String getkind()return kind; Element(String s,String kind) this.s = s; this.kind = kind; static Vector sheet = new Vector(8); File result = new
3、 File(result.txt); File result1 = new File(result1.txt); boolean work() System.out.println(输入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
4、.readLine(); label1: while(s!=null) for(int i = 0;i=|=)|d+.d*|w+|.+|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(); pu
5、blic void checkScan(String s)/扫描文件 if(Character.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 Ele
6、ment(s,小数); else CompilerOut(s); sheet.add(new Element(s,整数); private void checkCharCompiler(char c)/检测单个字符 switch (c) 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+,减号); break;
7、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+,右小括号); break; case : sheet.add(new Element(c+,左中括号); break; case : sheet.add(new Element(c+,右中括号); break; case : sheet.add(new Ele
8、ment(c+,左大括号); 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 Element(c+,取反); break; case .: sheet.add(new Element(c+,点号); break; case : sheet.add(new Element(c+,冒号); case ?:sheet.ad
9、d(new Element(c+,问号); break; default: sheet.add(new Element(c+,标识符); break; private void checkStringCompiler(String s)/检测多个字符 CompilerOut( s); if(isKeyWord(s) sheet.add(new Element(s,关键字); else sheet.add(new Element(s,标识符); private void CompilerOut(Object o)/向显示器输出结果预编译结果 if(o instanceof String) str
10、ingCompiler=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(预处理失败!)
11、; System.out.println(输入 y开始词法分析); char k = y; while(k!=(char)System.in.read() System.out.println(请输入y); c.analyser(); System.out.println( ); /System.out.println(n类型 单词符号); / System.out.println( 0 标识符); / System.out.println( 1 常数 ); / System.out.println( 2 关键字); for(int i=0;i0 i=1;输入 y开始词法分析 y单词: if 类型: 关键字单词: i 类型: 标识符单词: 类型: 大于号单词: 0 类型: 整数单词: i 类型: 标识符单词: = 类型: 等于号单词: 1 类型: 整数单词: ; 类型: 分号 专心-专注-专业