已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
词法分析代码。 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 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 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.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(); public 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 Element(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; 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 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 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 Element(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!=(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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 绿色能源材料市场需求与供应链分析报告
- 企业生产管理手册:从选型到评估设备指南
- 2025中专会计高三题库及答案
- 2025四级考试墨西哥真题及答案
- 企业项目管理方法
- 企业管理-申请物业费打折(因漏水导致装修损失)的申请报告模板
- 企业培训师培训计划表
- Web开发工程师团队协作指南
- 企业员工培训体系建立与课程开发
- (2025年)篮球理论考试题库及答案
- 延长石油6S管理制度宣贯12.8
- (正式版)HGT 6285-2024 甲基丙烯醛氧化制甲基丙烯酸催化剂活性试验方法
- 临床医学导论习题与答案2
- 第九章 - 软开关技术
- 海南警察学院规划方案
- 新冀教版冀人版科学四年级上册 教案(全册教学设计)
- 2024年航空货运行业培训资料
- 药理学:抗菌药氨基糖苷类抗生素课件
- 免养生水稳基层施工技术规程
- 税法(第二版)课件全套 高萍 第1-13章 税收、税法的概念及分类-印花税和烟叶税
- 3到5分钟的朗诵稿
评论
0/150
提交评论