已阅读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年公文撰写测试题目及答案
- 法律基础考试试题及参考答案(一)
- 注册测绘师GIS空间分析功能应用考试题目及答案
- 2026年社区团购供应链供货协议
- 深信服安服笔试题和答案
- 湖北省会计从业资格考试会计基础模拟试题及答案1
- 湖南幼儿师范高等专科学校工作人员招聘考试试题及答案
- 溆浦事业单位招聘2022考试真题及答案解析
- 焊工证考试常见试题及答案解析
- 2026年界面交互设计外包合同
- 2025年大学《伦理学-商业伦理学》考试参考题库及答案解析
- 扁桃体周围炎护理查房
- 防冻应急预案演练
- 物理培训机构招生宣传
- 合肥市轨道交通集团有限公司招聘笔试题库及答案2025
- hse应急预案下载
- 雨课堂在线学堂《运动与健康》课后章节测试答案
- 银行普惠金融2025年冲刺模拟试卷(含答案)
- 2025高三英语概要写作专项练习
- 2025税法考试题库及答案详解
- 2025至2030全球及中国绝缘体上硅(SOI)行业产业运行态势及投资规划深度研究报告
评论
0/150
提交评论