采用java图形化界面编写了java语言的词法分析器,该分析器可识别所有java关键字。软件工程课程中编译原理实验。
Keyword.jvav
package org.kyc.test1;public class Keyword {private String keyword; //存储关键字字符private int keywordindex;//存储关键字的下标Keyword[] keyW;//存储关键字对象的数组public Keyword(){//构造数组keyW=setKeywordList();}public Keyword(String str,int t){setKeyword(str);setKeywordindex(t);}public String getKeyword() {return keyword;}public void setKeyword(String keyword) {this.keyword = keyword;}public int getKeywordindex() {return keywordindex;}public void setKeywordindex(int keywordindex) {this.keywordindex = keywordindex;}/*** 创建关键字列表* @return*/public Keyword[] setKeywordList(){final intPRIVATE=30;// private私有的final intPROTECTED=31;// protected受保护的final intPUBLIC=32 ;//public 公共的//类、方法和变量修饰符final intABSTRACT=33;//abstract 声明抽象final intClASS=34;// class类final intEXTEND=35;//extends 扩允,继承final int FINAL=36;//final 终极,不可改变的final intIMPLEMENTS=37;//implements实现final intINTERFACE=38;//interface 接口final intNATIVE=39;//native 本地final intNEW=40;//new 新,创建final intSTATIC=41;//static 静态 final intSTRICTFP=42;//strictfp 严格,精准final intSYNCHRONIZED=43;//synchronized 线程,同步final intTRANSIENT=44;//transient 短暂final intVOLATILE=45;//volatile 易失//程序控制语句final intBREAK=46;//break 跳出循环final intCONTINUE=47;//continue 继续final intRETURN=48;//return 返回final intDO=49;//do 运行final intWHILE=50;//while 循环final intIF=51;//if 如果final intELSE=52;//else 反之final intFOR=53;//for 循环final intINSTANCEOF=54;//instanceof 实例final intSWITCH=55;//switch 开关final intCASE=56;//case 返回开关里的结果final intDEFAULT=57;//default 默认//错误处理final intCATCH=58;//catch 处理异常final intFINALLY=59;//finally 有没有异常都执行final int THROW=60;//throw 抛出一个异常对象final intTHROWS=61;//throws 声明一个异常可能被抛出final intTRY=62;//try 捕获异常//包相关final intIMPORT=63;//import 引入final int PACKAGE=64;//package 包//基本类型final intBOOLEAN=65;//boolean 布尔型final int BYTE=66;//byte 字节型final int CHAR=67;//char 字符型final intDOUBLE=68;//double 双精度,final intFLOATE=69;//float 浮点final intINT=70;//int 整型final intLONG=71;//long 长整型final intSHORT=72;//short 短整型final intNULL=73;//null 空final intTRUE=74;//true 真final intFALSE=75;//false 假//变量引用final intSUPER=76;//;super 父类,超类final intTHIS=77;//this 本类final intVOID=78;//void 无返回值final int CONST=80;//constfinal int GOTO=81;//gotoKeyword kwTable[] = { new Keyword("private", PRIVATE),new Keyword("protected", PROTECTED),new Keyword("public",PUBLIC),//类、方法和变量修饰符new Keyword("abstract",ABSTRACT), // 声明抽象new Keyword("class",ClASS), // 类new Keyword("extends",EXTEND), // 扩允,继承new Keyword("final" ,FINAL), // 终极,不可改变的new Keyword("implements",IMPLEMENTS), //实现new Keyword("interface",INTERFACE), // 接口new Keyword("native",NATIVE), // 本地new Keyword("new",NEW),// 新,创建new Keyword("static",STATIC), // 静态 new Keyword("strictfp" ,STRICTFP), //严格,精准new Keyword("synchronized",SYNCHRONIZED), // 线程,同步new Keyword("transient",TRANSIENT), // 短暂new Keyword("volatile",VOLATILE), // 易失//程序控制语句new Keyword("break",BREAK), // 跳出循环new Keyword("continue",CONTINUE), // 继续new Keyword("return" ,RETURN), //返回new Keyword("do",DO), // 运行"new Keyword("while",WHILE), //循环new Keyword("if",IF), // 如果new Keyword("else",ELSE), // 反之new Keyword("for",FOR), // 循环new Keyword("instanceof",INSTANCEOF), // 实例new Keyword("switch ",SWITCH), //开关new Keyword("case",CASE), // 返回开关里的结果new Keyword("default",DEFAULT), // 默认//错误处理new Keyword("catch", CATCH), //处理异常new Keyword("finally",FINALLY), // 有没有异常都执行new Keyword("throw", THROW), //抛出一个异常对象new Keyword("throws",THROWS), // 声明一个异常可能被抛出new Keyword("try",TRY), // 捕获异常//包相关new Keyword("import",IMPORT), // 引入new Keyword("package", PACKAGE), // 包//基本类型new Keyword("boolean",BOOLEAN), // 布尔型new Keyword("byte", BYTE), // 字节型new Keyword("char", CHAR), // 字符型new Keyword("double",DOUBLE), // 双精度,new Keyword("float",FLOATE), // 浮点new Keyword("int",INT),// 整型new Keyword("long",LONG), // 长整型new Keyword("short",SHORT), // 短整型new Keyword("null",NULL), // 空new Keyword("true",TRUE), // 真new Keyword("false",FALSE), // 假//变量引用new Keyword("super",SUPER), //, 父类,超类new Keyword("this",THIS), // 本类new Keyword("void",VOID), // 无返回值new Keyword("const" , CONST),new Keyword("goto", GOTO), };return kwTable;}/** 判断字符串是否为关键字*/public boolean isKeyWord(String str){boolean b=false;for(int i=0;i<keyW.length;i++){if(keyW[i].getKeyword().equals(str)||keyW[i].getKeyword()==str){b=true;break;}}return b;}}
package org.kyc.test1;public class Operators {private String strOperators;private int flagOperators;private Operators[] oper;public Operators(){oper=setOperators();}public Operators(String str,int t){strOperators=str;flagOperators=t;}public String getStrOperators() {return strOperators;}public void setStrOperators(String strOperators) {this.strOperators = strOperators;}public int getFlagOperators() {return flagOperators;}public void setFlagOperators(int flagOperators) {this.flagOperators = flagOperators;}/*** 创建操作符列表* @return Operators[]*/public Operators[] setOperators(){Operators operators[]={new Operators(".",101),new Operators("(",102),new Operators(")",103),new Operators("[",104),new Operators("]",105),new Operators("+",106),new Operators("-",107),new Operators("++",108),new Operators("--",109),new Operators("~",110),new Operators("!",111),new Operators("*",112),new Operators("/",113),new Operators("//",114),new Operators("<<",115),new Operators(">>",116),new Operators(">>>",117),new Operators(">",118),new Operators(">=",119),new Operators("<=",120),new Operators("==",121),new Operators("!=",122),new Operators("&",123),new Operators("|",124),new Operators("?",125),new Operators(":",126),new Operators("=",127),new Operators("+=",128),new Operators("-=",129),new Operators("/=",130),new Operators("%=",131),new Operators("&=",132),new Operators("|=",133),new Operators("^=",134),new Operators("<<=",135),new Operators(">>=",136),new Operators(">>>=",137),new Operators(";",140),new Operators("{",141),new Operators("}",142),new Operators("\"",143),new Operators("\'",144),new Operators("@",145),new Operators("$",146),new Operators("\\",147),new Operators(",",148)};return operators;}public boolean isOperator(String str){// 判断字符是否是操作符boolean b=false;for(int i=0;i<oper.length;i++){if(oper[i].getStrOperators().equals(str)||oper[i].getStrOperators()==str){b=true;break;}}return b;}public boolean isOperator(char c){// 判断字符是否是操作符boolean b=false;for(int i=0;i<oper.length;i++){if(oper[i].getStrOperators().equals(c)||oper[i].getStrOperators().charAt(0)==c){b=true;break;}}return b;}public int getNumber(String str){//返回与字符对应的数字int f=0;for(int i=0;i<oper.length;i++){if(oper[i].getStrOperators().equals(str)||oper[i].getStrOperators()==str){f=oper[i].getFlagOperators();break;}}return f;}public int getNumber(char c){int f=0;for(int i=0;i<oper.length;i++){if(oper[i].getStrOperators().equals(c)||oper[i].getStrOperators().charAt(0)==c){f=oper[i].getFlagOperators();break;}}return f;}}
package org.kyc.test1;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class CharacterAnalysis {final int ERROR=0;//错误final int LETTER=1;//字母final int NUM=2;//数字final int NOTE=3;//注释的private char[] prog;//存储所扫描的程序private int progIndex;//扫描的下标private String token;//获取到的字符串final int PROG_SIZE = 1000000;// 将关键字的外部表示,和内部表示保存在一个名为KeyWordTable的表中Keyword keyword=new Keyword();Keyword keywordTable[] =keyword.setKeywordList();Operators operator=new Operators();Operators operatorsTable[]=operator.setOperators();Object [][] list=new Object[65635][2];int List_index=0;public CharacterAnalysis(File file_choosed) throws IOException {//实现将文件中的字符存到数组中char tempbuf[] = new char[PROG_SIZE];int size=0;try {FileReader fr = new FileReader(file_choosed);BufferedReader br = new BufferedReader(fr);size = br.read(tempbuf, 0, PROG_SIZE);fr.close();} catch (FileNotFoundException exc) {System.out.print("没有找到该文件!");}if (size != -1) {prog = new char[size];System.arraycopy(tempbuf, 0, prog, 0, size);}}public void Analyse(String str){this.prog=str.toCharArray();try {Analyse();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void Analyse() throws IOException {// 对代码扫描do {token = "";// 跳过空格符while(isSpaceOrTab(prog[progIndex])||isnextline(prog[progIndex])){while (isSpaceOrTab(prog[progIndex])){progIndex++;if(progIndex>=prog.length)break;}// 处理换行while (isnextline(prog[progIndex])) {progIndex ++;if(progIndex>=prog.length)break;}if(progIndex>=prog.length)break;}if(progIndex>=prog.length){break;}/** 判断是否为注释,并作处理*/else if(isMark()){System.out.println("注释:"+token+" "+NOTE);AddTolist("注释:"+token,NOTE);continue;}/** 是一个操作符,或者是运算符*/else if (operator.isOperator(prog[progIndex])) {//System.out.println("是个操作符");token += prog[progIndex];progIndex++;//继续判断是否为两个符号组成在一起的操作符if (operator.isOperator(prog[progIndex])){//判端接下来的操作符是否和前面的操作符能够组成在一起token += prog[progIndex];progIndex++;if (operator.isOperator(token)){//说明组成在一起了System.out.println(token+" "+operator.getNumber(token));AddTolist(token,operator.getNumber(token));continue;}else{//说明组不到一起System.out.println(token.charAt(0)+" "+operator.getNumber(token.charAt(0)));System.out.println(token.charAt(1)+" "+operator.getNumber(token.charAt(1)));AddTolist(token.charAt(0),operator.getNumber(token.charAt(0)));AddTolist(token.charAt(1),operator.getNumber(token.charAt(1)));continue;}}else{System.out.println(token+" "+operator.getNumber(token));AddTolist(token,operator.getNumber(token));continue;}} /** 是一个字符变量*/else if (Character.isLetter(prog[progIndex])) {while (Character.isLetterOrDigit(prog[progIndex])||prog[progIndex]=='_') {//System.out.println("是个字符变量");token += prog[progIndex];progIndex++;if (progIndex >= prog.length)break;}if (keyword.isKeyWord(token)){//说明是关键字for(int i=0;i<keywordTable.length;i++){if(token.equals(keywordTable[i].getKeyword())||token==keywordTable[i].getKeyword()){System.out.println(token+" "+keywordTable[i].getKeywordindex());AddTolist(token,keywordTable[i].getKeywordindex());break;}}}else{System.out.println(token + " " + LETTER);AddTolist(token, LETTER);}} /** 是一个数字字符*/else if (Character.isDigit(prog[progIndex])) {while (Character.isDigit(prog[progIndex])) {token += prog[progIndex];progIndex++;if (progIndex >= prog.length)break;}System.out.println(token + " " +NUM );AddTolist(token, NUM);}else {System.out.println(token+" "+ERROR);AddTolist(token, ERROR);progIndex++;continue;}} while (progIndex != prog.length);System.out.println("程序分析完毕");}private boolean isMark() {// TODO Auto-generated method stub//判断是否为注释部分if(prog[progIndex]=='/'&&prog[progIndex+1]=='/'){while(!isnextline(prog[progIndex])){token += prog[progIndex];progIndex++;}return true;}return false;}//判断是否为空格private boolean isSpaceOrTab(char c) {if (c == ' ' || c == '\t')return true;return false;}//判断是否为换行private boolean isnextline(char c){if(c== '\r'||c == '\n'){return true;}else{return false;}}public char[] getProg() {return prog;}public void AddTolist(String str,int num){list[List_index][0]=str;list[List_index][1]=num;List_index++;}private void AddTolist(char charAt, int num) {// TODO Auto-generated method stublist[List_index][0]=charAt;list[List_index][1]=num;List_index++;}}
package org.kyc.test1;
package org.kyc.test1;import java.awt.EventQueue;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.IOException;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.border.EmptyBorder;import java.awt.Font;/*** 编译原理实验一——词法分析器* @author 康雨城* /6/4*/public class View extends JFrame {private JPanel contentPane;private JFileChooser fc;private JTextArea textArea;private CharacterAnalysis ob;private JTable table;private File file_choosed;String [] cloumnNames= {"词语","分析结果"};/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {View frame = new View();frame.setVisible(true);frame.setTitle("词法分析器");} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public View() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 750, 400);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(new GridLayout(1, 2, 0, 0));fc = new JFileChooser();fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);JPanel panel = new JPanel();contentPane.add(panel);panel.setLayout(null);JPanel panel_Left = new JPanel();panel_Left.setBounds(0, 297, 362, 54);panel.add(panel_Left);panel_Left.setLayout(new GridLayout(1, 2, 0, 0));JScrollPane Left_ScrollPane = new JScrollPane();Left_ScrollPane.setBounds(0, 0, 362, 297);panel.add(Left_ScrollPane);textArea = new JTextArea();Left_ScrollPane.setViewportView(textArea);JScrollPane RightScrollPane = new JScrollPane();contentPane.add(RightScrollPane);JButton btnSelectFile = new JButton("选取文件");btnSelectFile.setFont(new Font("宋体", Font.BOLD, 18));btnSelectFile.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int i=fc.showOpenDialog(null);if(i==fc.APPROVE_OPTION){file_choosed = fc.getSelectedFile();try {ob = new CharacterAnalysis(file_choosed);textArea.setText(String.valueOf(ob.getProg()));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}else {System.out.println("没有选择文件");}}});panel_Left.add(btnSelectFile);JButton btnAnalysis = new JButton("分析");btnAnalysis.setFont(new Font("宋体", Font.BOLD, 18));btnAnalysis.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {try {if(file_choosed==null){System.out.println("请选择文件!");}else{ob.Analyse();table=new JTable(ob.list,cloumnNames);RightScrollPane.setViewportView(table);table.invalidate();}} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});panel_Left.add(btnAnalysis);}}
如果觉得《编译原理——词法分析器》对你有帮助,请点赞、收藏,并留下你的观点哦!