词法分析代码。
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", "procedure", "odd"};
private static boolean isKeyWord(String s) //是否是关键字 {
boolean b=false;
for(int i=0;i<KEY_WORD.length;i++)
if(s.equals(KEY_WORD[i]))
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<Element> sheet = new Vector<Element>(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); , "var"
String s = br1.readLine();
label1:
while(s!=null)
{
for(int i = 0;i<s.length();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;
}
else 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!=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;
}
else
{
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.printStackTrace();
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 =
Pattern.compile("/\\*.*\\*/|\'.+\'|(\\++|\\--|\\>=|\\<=)|\\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(new Character(c).toString() ,"小于号
")); //扫描文件
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+"","点号")); 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 常数 ");
// System.out.println(" 2 关键字");
for(int i=0;i<sheet.size();i++)
{
Element element = (Element) sheet.elementAt(i);
System.out.println("单词: "+element.s+"\t"+"\t"+"\t"+" 型: "+element.getkind());
}
}
}
运行结果。 *****************************************************
输入PL/O语言为:
if i>0 i=1;
输入 y开始词法分析 y
单词: if 类型: 关键字
单词: i 类型: 标识符
单词: > 类型: 大于号
单词: 0 类型: 整数
单词: i 类型: 标识符
单词: = 类型: 等于号
单词: 1 类型: 整数
单词: ; 类型: 分号 类
实验报告计算机与信息工程学院实验中心学期20xx20xx课程名称Java程序设计实验班级信息1202姓名方逸梅学号12121002…
Java语言程序设计实验报告实验序号3一实验目的及要求1进一步熟练Java语法和常用方法的使用2进一步熟练Java程序结构3进一步…
信息工程学院Java程序设计实习报告JAVA图形用户界面实验六Java图形用户界面1实验目的1掌握图形用户界面基本组件2了解如何使…
JAVA课程标准实验报告学号1040501211姓名陈嘉生江苏科技大学计算机科学与工程学院基础部1实验一流程控制语句一实验目的12…
浙江大学城市学院实验报告课程名称面向对象程序设计实验项目名称Java语言基础实验学生姓名专业班级学号一实验目的和要求1掌握Java…
辽宁建筑职业学院Java高级应用实训报告课题名称婚庆公司网站专业软件技术班级xxxxxxxxxx学号姓名xxxx指导教师xxx二O…
徐州工程学院管理学院实验报告实验课程名称数据结构与算法实验地点经济管理教学实验中心20xx年3月至20xx年6月专业信息管理与信息…
第7章常用实用类实验1比较日期的大小1实验目的本实验的目的是让学生掌握Date类和Calendar类的常用方法2实验要求编写一个J…
学生实验报告册20xx20xx学年第1学期项目名称JavaWeb学院信电班级11计算机科学与技术一班学号155140007姓名伍振…
实验报告模板Java课程实验报告实验程序如下实验任务一publicclasssumpublicstaticvoidmainStri…