java 词法分析器
2017-07-23 16:39
190 查看
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.border.Border; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableCellRenderer; public class Analyzer extends JFrame { /** * */ private static final long serialVersionUID = 1L; private String keyWords[] = {"char","struct","do","if","break","const","typedef", "int","union","long","while","else","continue","static", "sizeof","float","enum","unsigned","for","switch","return", "register","extern","double","void","signed","case","goto", "volatile","default","auto","then"}; // 关键字数组 private String id[]=new String[100];//存放标识符数组 private String ci[]=new String[100];//保留数字 private int countchar=1; private int countnumber=1; private char operators[] = {'+', '-', '*', '/','=','<','>'}; // 运算符数组 private String Rea_operators[]={"<=",">="}; private char separators[] = {';',',','(',')','{','}','#'}; // 分隔符数组 private StringBuffer buffer = new StringBuffer(); // 缓冲区 private char ch; // 字符变量,存放最新读进的源程序字符 private static int i = 0; private String strToken; // 字符数组,存放构成单词符号的字符串 private String strTokentemp; private static int rows=1; private int columns=0; // 建立表格 private Object[] colname = {"单词","二元序列","类型","位置(行,列)"}; private Object[][] data=new Object[50][4]; private int k=0; private JTable table; private JLabel j8; private JLabel j9; private JLabel j10; private JTextField j3; private JTextArea j5; private JTextArea j12; private JTextArea j13; private JScrollPane jsPan; private JScrollPane jsPane; private JScrollPane jsPan1; private JScrollPane jsPan2; private File imgFile = null;// 声明所选择文件 private BufferedReader in; private BufferedReader in1; private GridBagLayout layout; private GridBagConstraints s; private int ONE_SECOND = 1000; private String DEFAULT_TIME_FORMAT = "HH:mm:ss"; private String time; /** * 将下一个输入字符读到ch中,搜索指示器前移一个字符 */ public void GetChar() { ch = buffer.charAt(i); i++; } /** * 检查ch中的字符是否为空白,若是则调用getChar() 直至ch中进入一个非空白字符 */ public void GetBc() { while(i<buffer.length() && (ch==' '|| ch=='\n')) { if(ch=='\n') { rows++; columns=0; } GetChar(); } } public void Print() { int i=0; while(i<buffer.length()) { ch = buffer.charAt(i); if(ch=='\n') { rows++; } i++; } System.out.print(buffer.length()); } /** * 将ch连接到strToken之后 */ public void Concat() { strToken += ch; } /** * 判断字符是否为字母 */ boolean IsLetter() { return Character.isLetter(ch); } /** * 判断字符是否为数字 */ boolean IsDigit() { return Character.isDigit(ch); } /** * 将搜索指示器回调一个字符位置,将ch值为空白字 */ public void Retract() { i--; ch = ' '; } /** * 判断单词是否为关键字 * */ public int Reserve() { int location=0; for (int j = 0; j < keyWords.length; j++) { if(keyWords[j].equals(strToken)) { location = j+1; break; } else { if(j==keyWords.length-1) { location=0; } } } return location; } /** * 判断是否为运算符 * */ public int IsOperator() { int location=0; for (int j = 0; j < operators.length; j++) { if(operators[j]==ch) { location=j+1; break; } else { if(j==operators.length-1) { location=0; } } } return location; } /** * 判断是否为关系运算符 * */ public int IsRea_Operator() { int location=0; for (int j = 0; j < Rea_operators.length; j++) { if(Rea_operators[j].equals(strToken)) { location=j+1; break; } else { if(j==Rea_operators.length-1) { location=0; } } } return location; } /** * 判断是否为分隔符 * */ public int IsSeparator() { int location=0; for (int j = 0; j < separators.length; j++) { if (separators[j]==ch) { location=j+1; break; } else { if(j==separators.length-1) { location=0; } } } return location; } /** * 将strToken插入到符号表 */ public int InsertId(String strToken) { int location=0; for(int j=0;j<countchar;j++) { if(strToken.equals(id[j])) { location=j; break; } else { if(j==countchar-1) { id[countchar-1]=strToken; location=countchar-1; countchar++; break; } } } return location; } /** * 将strToken中的常数插入到常数表中 */ public int InsertConst(String strToken) { int location=0; for(int j=0;j<countnumber;j++) { if(strToken.equals(ci[j])) { location=j; break; } else { if(j==countnumber-1) { id[countnumber-1]=strToken; location=countnumber-1; countnumber++; break; } } } return location; } /** * 将源程序读入到缓冲区中 */ public void readFile() { try { String temp = null; while ((temp = in.readLine()) != null) { buffer.append(temp); buffer.append("\n"); } } catch (FileNotFoundException e) { System.out.println("源文件未找到!"); e.printStackTrace(); } catch (IOException e) { System.out.println("读写文件出现异常!"); e.printStackTrace(); } } /** * 词法分析 */ public void analyse() { int code; strToken = ""; // 置strToken为空串 while (i < buffer.length()) { GetChar(); GetBc(); if(i<buffer.length()) { if (IsLetter()) { // 如果ch为字母 while (IsLetter() || IsDigit()) { Concat(); GetChar(); } Retract(); // 回调 code=Reserve(); if (code!=0) { // 如果是为关键字,则插入到1.保留字表中 columns++; System.out.println(strToken+" "+rows+" "+columns); data[k++]=new Object[]{strToken,"("+"1"+","+strToken+")","关键字","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; } else { // 否则插入到2.符号表中 InsertId(strToken); columns++; j12.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"6"+","+strToken+")","标识符","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); } strToken = ""; } else if (IsDigit()) { // 如果ch为数字 if(ch=='0') {//如果是16进制数 Concat(); GetChar(); if(ch=='X'||ch=='x') { Concat(); GetChar(); while((ch<='9'&&ch>='0')||(ch<='F'&&ch>='A')||(ch<='f'&&ch>='a')) { Concat(); GetChar(); } if(ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else {//是常数,满足要求 Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } } else { while 4000 (IsDigit()) { Concat(); GetChar(); } if(ch=='.') {//小数点 Concat(); GetChar(); if(IsDigit()) {//小数点接下来也是数字 while(IsDigit()) { Concat(); GetChar(); } if(ch!='e'&&ch!='E'&&ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else { if(ch=='e'||ch=='E') { Concat(); GetChar(); if (IsDigit()||ch=='-') { // 如果ch为数字 Concat(); GetChar(); while (IsDigit()) { Concat(); GetChar(); } if(ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else {//是常数,满足要求 Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } else {//e后面的不满足要求 if(ch==' '||(IsSeparator()!=0)) { Retract(); // 回调 columns++; System.out.println(strToken+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strToken,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } } else { if(ch==' '||(IsSeparator()!=0)) { Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } //////////// } } else {//小数点后面不是数字 strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } else {//整数后面不是小数点 if(ch!='e'&&ch!='E'&&ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else { if(ch=='e'||ch=='E') { Concat(); GetChar(); if (IsDigit()||ch=='-') { // 如果ch为数字 Concat(); GetChar(); while (IsDigit()) { Concat(); GetChar(); } if(ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else {//是常数,满足要求 Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } else {//e后面的不满足要求 if(ch==' '||(IsSeparator()!=0)) { Retract(); // 回调 columns++; System.out.println(strToken+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strToken,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } } else { if(ch==' '||(IsSeparator()!=0)) { Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } //////////// } } strToken = ""; } } else { if((IsOperator()!=0)&&(ch=='-'||ch=='+')) {//如果是运算符 Concat(); GetChar(); //可能表示正负数 if (IsDigit()) { // 如果ch为数字 if(ch=='0') {//如果是16进制数 Concat(); GetChar(); if(ch=='X'||ch=='x') { Concat(); GetChar(); while((ch<='9'&&ch>='0')||(ch<='F'&&ch>='A')||(ch<='f'&&ch>='a')) { Concat(); GetChar(); } if(ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else {//是常数,满足要求 Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } } else { while (IsDigit()) { Concat(); GetChar(); } if(ch=='.') {//小数点 Concat(); GetChar(); if(IsDigit()) {//小数点接下来也是数字 while(IsDigit()) { Concat(); GetChar(); } if(ch!='e'&&ch!='E'&&ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else { if(ch=='e'||ch=='E') { Concat(); GetChar(); if (IsDigit()||ch=='-') { // 如果ch为数字 Concat(); GetChar(); while (IsDigit()) { Concat(); GetChar(); } if(ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else {//是常数,满足要求 Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } else {//e后面的不满足要求 if(ch==' '||(IsSeparator()!=0)) { Retract(); // 回调 columns++; System.out.println(strToken+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strToken,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } } else { if(ch==' '||(IsSeparator()!=0)) { Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } //////////// } } else {//小数点后面不是数字 strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } else {//整数后面不是小数点 if(ch!='e'&&ch!='E'&&ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else { if(ch=='e'||ch=='E') { Concat(); GetChar(); if (IsDigit()||ch=='-') { // 如果ch为数字 Concat(); GetChar(); while (IsDigit()) { Concat(); GetChar(); } if(ch!=' '&&(IsSeparator()==0)) { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else {//是常数,满足要求 Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } else {//e后面的不满足要求 if(ch==' '||(IsSeparator()!=0)) { Retract(); // 回调 columns++; System.out.println(strToken+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strToken,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } else { strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } } else { if(ch==' '||(IsSeparator()!=0)) { Retract(); // 回调 columns++; InsertConst(strToken); j13.append(strToken+"\n"); data[k++]=new Object[]{strToken,"("+"5"+","+strToken+")","常数","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } } //////////// } } strToken = ""; } } else { if(ch==' '||IsSeparator()!=0) {//后面是空格或者是分界符 Retract(); // 回调 columns++; data[k++]=new Object[]{strToken,"("+"3"+","+strToken+")","算数运算符","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken=""; } else { //后面的不符合要求 strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } } else { if(ch=='*'||ch=='/') {//是算术运算符 Concat(); GetChar(); if(ch==' '||IsSeparator()!=0) {//后面是空格或者是分界符 Retract(); // 回调 columns++; data[k++]=new Object[]{strToke e3d6 n,"("+"3"+","+strToken+")","算数运算符","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken=""; } else { //后面的不符合要求 strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } else {//如果是<或> if(ch=='<'||ch=='>') { Concat(); GetChar(); if(ch=='=') { Concat(); GetChar(); if(ch==' '||IsSeparator()!=0) {//后面是空格或者是分界符 Retract(); // 回调 columns++; data[k++]=new Object[]{strToken,"("+"4"+","+strToken+")","关系运算符","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken=""; } else { //后面的不符合要求 strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } else { if(ch==' '||IsSeparator()!=0) {//后面是空格或者是分界符 Retract(); // 回调 columns++; data[k++]=new Object[]{strToken,"("+"4"+","+strToken+")","关系运算符","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken=""; } else { //后面的不符合要求 strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } } else { if(ch=='=') { Concat(); GetChar(); if(ch==' '||IsSeparator()!=0) {//后面是空格或者是分界符 Retract(); // 回调 columns++; data[k++]=new Object[]{strToken,"("+"4"+","+strToken+")","关系运算符","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken=""; } else { //后面的不符合要求 strTokentemp=strToken; strToken=""; while (ch!=' '&&(IsSeparator()==0)) { Concat(); GetChar(); } Retract(); // 回调 strTokentemp+=strToken; columns++; System.out.println(strTokentemp+"ERROR"+rows+" "+columns); data[k++]=new Object[]{strTokentemp,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; strToken = ""; } } else {//如果是分界符 if(IsSeparator()!=0) {//如果是分隔符 Concat(); columns++; data[k++]=new Object[]{strToken,"("+"2"+","+strToken+")","分界符","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+" "+rows+" "+columns); strToken = ""; } else { Concat(); columns++; data[k++]=new Object[]{strToken,"ERROR","ERROR","("+String.valueOf(rows)+","+String.valueOf(columns)+")"}; System.out.println(strToken+"2Error"+rows+" "+columns); strToken = ""; } } } } } } } strToken = ""; } } //将表格清空 public void Get_Clear() { int i; for(i=0;i<50;i++) { data[i]=new Object[]{"","","",""}; } } //获取系统的日期 public String Get_Date() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateNowStr = sdf.format(date); return dateNowStr; } public void Get_Time() { Timer tmr = new Timer(); tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND); } public Analyzer() { setTitle("词法分析器"); final JButton j4 = new JButton("浏览"); j4.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { JFileChooser fileChooser = new JFileChooser();// 创建文件选择器 FileFilter filter = new FileNameExtensionFilter( "文件(txt/docx/doc/java/cpp/asm)", "txt","docx","doc","java","cpp","asm");// 创建过滤器 fileChooser.setFileFilter(filter);// 设置过滤器 int flag = fileChooser.showOpenDialog(null);// 显示打开对话框 if (flag == JFileChooser.APPROVE_OPTION) { imgFile = fileChooser.getSelectedFile(); // 获取选中文件的File对象 } if (imgFile != null) { j3.setText(imgFile.getAbsolutePath());// 文件完整路径 try { in=new BufferedReader(new FileReader (imgFile.getAbsoluteFile()));//获得绝对路径 } catch (IOException ex) { ex.printStackTrace(); } } } }); final JButton j1 = new JButton("显示"); j1.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { try { in1=new BufferedReader(new FileReader (imgFile.getAbsoluteFile())); } catch (FileNotFoundException e2) { // TODO Auto-generated catch block e2.printStackTrace(); }//获得绝对路径 String s; try { while((s=in1.readLine())!=null) { j5.append(s); j5.append("\n"); } in1.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); final JButton j2 = new JButton("分析"); j2.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { Get_Clear(); k=0;//将设置为初态 rows=1; columns=0; readFile(); analyse(); jsPane.validate(); jsPane.repaint(); } }); j3 = new JTextField(); j5 = new JTextArea(); j12=new JTextArea(); j13=new JTextArea(); j8=new JLabel("LL(1)",JLabel.CENTER); j8.setBorder(BorderFactory.createEtchedBorder()); j9=new JLabel(Get_Date(),JLabel.CENTER); j9.setBorder(BorderFactory.createEtchedBorder()); j10=new JLabel("54",JLabel.CENTER); j10.setBorder(BorderFactory.createEtchedBorder()); table = new JTable(data,colname); //表头居中 ((DefaultTableCellRenderer)table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER); //表格内容居中 DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();// 设置table内容居中 renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER); table.setDefaultRenderer(Object.class, renderer); jsPan = new JScrollPane(j5); Border titleBorder=BorderFactory.createTitledBorder("程序"); jsPan.setBorder(titleBorder); jsPane = new JScrollPane(table); Border titleBorder4=BorderFactory.createTitledBorder("分析过程"); jsPane.setBorder(titleBorder4); jsPan1=new JScrollPane(j12); Border titleBorder1=BorderFactory.createTitledBorder("标识符"); jsPan1.setBorder(titleBorder1); jsPan2=new JScrollPane(j13); Border titleBorder2=BorderFactory.createTitledBorder("常数"); jsPan2.setBorder(titleBorder2); layout = new GridBagLayout(); this.setLayout(layout); this.add(j1); this.add(j2); this.add(j3); this.add(j4); this.add(jsPan); this.add(jsPan1); this.add(jsPan2); this.add(jsPane); this.add(j8); this.add(j9); this.add(j10); s= new GridBagConstraints();//定义一个GridBagConstraints, //是用来控制添加进的组件的显示位置 s.fill = GridBagConstraints.BOTH; //该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况 //NONE:不调整组件大小。 //HORIZONTAL:加宽组件,使它在水平方向上填满其显示区域,但是不改变高度。 //VERTICAL:加高组件,使它在垂直方向上填满其显示区域,但是不改变宽度。 //BOTH:使组件完全填满其显示区域。 s.gridwidth=2;//该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个 s.weightx = 0;//该方法设置组件水平的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间 s.weighty=0;//该方法设置组件垂直的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间 layout.setConstraints(j1, s);//设置组件 s.gridwidth=2; s.weightx = 0; s.weighty=0; layout.setConstraints(j2, s); s.gridwidth=4; s.weightx = 1; s.weighty=0; layout.setConstraints(j3, s); s.gridwidth=0;//该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个 s.weightx = 0; s.weighty=0; layout.setConstraints(j4, s); s.gridwidth=4; s.weightx = 1; s.weighty=1; layout.setConstraints(jsPan, s); s.gridwidth=0; s.weightx = 1; s.weighty=1; layout.setConstraints(jsPan1, s); s.gridwidth=4; s.weightx = 1; s.weighty=1; layout.setConstraints(jsPan2, s); s.gridwidth=0; s.weightx = 1; s.weighty=1; layout.setConstraints(jsPane, s); s.gridwidth=4; s.weightx = 1; s.weighty=0; layout.setConstraints(j8, s); s.gridwidth=3; s.weightx = 1; s.weighty=0; layout.setConstraints(j9, s); s.gridwidth=0; s.weightx = 1; s.weighty=0; layout.setConstraints(j10, s); } protected class JLabelTimerTask extends TimerTask{ SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT); @Override public void run() { time = dateFormatter.format(Calendar.getInstance().getTime()); j10.setText(time); } } public static void main(String[] args) { try {//使用默认窗口形式 UIManager .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { Analyzer alr = new Analyzer();//文件路径 alr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); alr.setBounds(300,200,400,400); alr.setVisible(true); alr.Get_Time();//动态显示时间 } catch (Exception e) { e.printStackTrace(); } } }); } }
相关文章推荐
- java 词法分析器
- 词法分析器编译预处理程序 java版 修改
- 大学编译实验--词法分析器(Java实现)
- java 词法分析器
- 词法分析器Java
- java 词法分析器
- java实现词法分析器
- 简单词法分析器java版本
- 这学期的课程设计,用java写的词法分析器
- Java编写的小语言词法分析器(含文档,visio流程图,源码等)
- java简单地实现Tiny语言的词法分析器
- Java 实现词法分析器(编译原理)
- 一个简单词法分析器的实现代码(java实现)
- 一个简单词法分析器的实现代码(java实现)
- 利用Java实现简单的词法分析器实例代码
- java实现简单的词法分析器
- 编译原理―词法分析器(Java)
- Java简单词法分析器
- java写的词法分析器
- Java从Excel中读取数据