⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lalr_creator.java

📁 上海复旦大学计算机系学子写的JAVA版本编译原理LALR1完整分析表生成程序,Eclipse开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         }
      }
      catch(IOException e){
         JOptionPane.showMessageDialog(this, "未找到KeyWord.txt文件", "Error",
                                       JOptionPane.ERROR_MESSAGE);
      }
      result = new ResultDialog("词法分析结果");
      tabDial = new TableDialog("LALR分析表");
    
      setVisible(true);
      source = new TextArea("", 15, 25);
      filedial = new FileDialog(this, "选择.txt文件", FileDialog.LOAD);
      filedial.setVisible(false);
      filedial.setFile("*.txt");
      Panel p1 = new Panel();
      p1.setLayout(new BorderLayout());
  
      p1.add(source, BorderLayout.CENTER);
      this.setBackground(Color.LIGHT_GRAY);
     
      Menu tool = new Menu("工具");
      Menu view = new Menu("查看");
      
      makeTable = new MenuItem("生成分析表");
      tabChecker = new MenuItem("查看/刷新分析表");
       
    
      tool.add(makeTable);
      view.add(tabChecker);
 
 		
      bar.add(tool);
      bar.add(view);
     
     
      
      makeTable.addActionListener(this);
      
     
      tabChecker.addActionListener(this);
      setMenuBar(bar);
      js = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p1, errList);
      js.setDividerSize(5);
      js.setDividerLocation(this.getHeight() - 120);
      add(js, BorderLayout.CENTER);
      this.validate();
      addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent e){
            System.exit(0);
         }
      });
   }

   String getTokenName(int sign){
      int i;
      for(i = 0; i < tokenSet.size(); i++){
         KeyWord kw = (KeyWord)tokenSet.get(i);
         if(sign == kw.code){
            return kw.word;
         }
      }
      return null;
   }

   

   public void actionPerformed(ActionEvent e){
   
      
     if(e.getSource() == makeTable){

         String dir = null, n = null;
      
         
         /************************************************/
         n = "tokens.par";
         /************************************************/
         
        
        if(n == null){
            return;
         }
         KeyWord t;
         try{
            tokenSet.clear();
            BufferedReader in = new BufferedReader(new FileReader(new File(
                     n)));
            String st;
            while((st = in.readLine()) != null){
               StringTokenizer parse = new StringTokenizer(st, " ");
               String temp = parse.nextToken();
               int i = Integer.parseInt(parse.nextToken());
               t = new KeyWord(temp, i);
               tokenSet.add(t);
            }
         }
         catch(IOException ex){
            JOptionPane.showMessageDialog(this, "读取符号映射表读取失败", "Error",
                                          JOptionPane.ERROR_MESSAGE);
            return;
         }
        
         n = null;
       
         /************************************************/
         
         n = "L 文法.txt";
         /************************************************/
       
         if(n == null){
            return;
         }
         TableMaker tm = new TableMaker();
         boolean isSuc = tm.readFormulaSet(dir, n);
      
         boolean tableMaked;
       
         try{
            tm.makeItemClosure();
            System.out.println("@#");
            formulaSet = tm.getFormulaSet();
            tm.makeTable();
            gto = tm.getGOTO();
            action = tm.getACTION();
            C = tm.getC();
            ts = tm.getTSign();
            unts = tm.getUTSign();
            stateNum = tm.getStateNum();
            tableMaked = true;
         }
         catch(Exception ex){
            gto = null;
            action = null;
            ts = null;
            unts = null;
            stateNum = -1;
            tableMaked = false;
         }
         if(isSuc && tableMaked){
       
            JOptionPane.showMessageDialog(this, "分析表生成完毕", "",
                                          JOptionPane.YES_OPTION);
            
         }
         else{
            if(!isSuc){
               JOptionPane.showMessageDialog(this, "无法解析文法文件", "失败!",
                                             JOptionPane.ERROR_MESSAGE);
            }
            if(!tableMaked){
               JOptionPane.showMessageDialog(this, "产生式生成失败", "失败!",
                                             JOptionPane.ERROR_MESSAGE);
            }
            tm.clearFormulaSet();
         }
      }
      
      
     
      else if(e.getSource() == tabChecker){
         System.out.println("StatNum: " + stateNum);
         if(stateNum < 40){
            tabDial.update(gto, action, ts, unts, stateNum, tokenSet);
            tabDial.setVisible(true);
         }
         else{
            int n = JOptionPane.showConfirmDialog(this,
                                                  "文件过大, 是否输出到.txt文件中?",
                                                  "Warning!",
                                                  JOptionPane.YES_NO_OPTION);
            if(n == JOptionPane.NO_OPTION){
               return;
            }
            else if(n == JOptionPane.YES_OPTION){
             
               this.tableToFile();
               return;
            }
         }
      }
      
      
     
     
   }


   

   void tableToFile(){
      FileDialog dial = new FileDialog(this, "保存LALR分析表", FileDialog.SAVE);
      dial.setVisible(true);
     
      String dir = null, n = null;
      dir = dial.getDirectory();
      n = dial.getFile();
   
      if(dir != null && n != null){
         try{
            BufferedWriter out = new BufferedWriter(new FileWriter(new File(dir,
                  n)));
            if(gto == null || action == null){
             
               return;
            }
            out.write("ACTION", 0, 6);
            out.newLine();
    
            out.write("State  |  ", 0, 10);
       
            int i, j;
            int tab = 15;

            for(i = 0; i < ts.size(); i++){
               Integer tI = (Integer)ts.get(i);
               String str = this.getTokenName(tI.intValue());
               int len;

               len = str.length();

               out.write(str, 0, str.length());
               out.write("  |  ", 0, 5);
              
            }
            out.newLine();
            int index = 0;
            for(i = 0; i < stateNum; i++){
               str = index + "";
               out.write(str, 0, str.length());
               for(j = 0; j < 7 - (String.valueOf(index)).length(); j++){
                  out.write(' ');
               }
               out.write('|');
               index++;
               for(j = 0; j < ts.size(); j++){
                  String token = this.getTokenName(((Integer)ts.get(j)).
                        intValue());
                  char ch = token.charAt(0);
                  int add;
                  int l = token.length();
                  if(l > 1){
                     add = 2;
                  }
                  else{
                     add = 4;
                  }
                  if(ch < 21 || ch > 125){
                     tab = token.length() * 2 + 4;
                  }
                  else{
                     tab = token.length() + 4;
                  }
                  int k;
                  String str = action[i][j];
                  int len;
                  len = str.length();
                  for(k = 0; k < (tab - len) / 2; k++){
                     out.write(' ');
                  }
                  out.write(str, 0, str.length());
                  for(k = 0; k < tab - len - (tab - len) / 2; k++){
                     out.write(' ');
                  }
                  out.write('|');
               }
               out.newLine();
            }
            out.write("GOTO", 0, 4);
            out.newLine();
            out.write("State  |  ", 0, 10);
            for(i = 0; i < unts.size(); i++){
               Integer tI = (Integer)unts.get(i);
               String str = this.getTokenName(tI.intValue());
               int len;
               len = str.length();
               out.write(str, 0, str.length());
               out.write("  |  ", 0, 5);
            }
            out.newLine();
            index = 0;
            for(i = 0; i < stateNum; i++){
               str = index + "";
               out.write(str, 0, str.length());
               for(j = 0; j < 7 - (String.valueOf(index)).length(); j++){
                  out.write(' ');
               }
               out.write('|');
               index++;
               for(j = 0; j < unts.size(); j++){
                  String token = this.getTokenName(((Integer)unts.get(j)).
                        intValue());
                  char ch = token.charAt(0);
                  if(ch < 21 || ch > 125){
                     tab = token.length() * 2 + 4;
                  }
                  else{
                     tab = token.length() + 4;
                  }
                  int k;
                  String str = String.valueOf(gto[i][j]);
                  int len;
                  len = str.length();
                  for(k = 0; k < (tab - len) / 2; k++){
                     out.write(' ');
                  }
                  out.write(str, 0, str.length());
                  for(k = 0; k < tab - len - (tab - len) / 2; k++){
                     out.write(' ');
                  }
                  out.write('|');
               }
               out.newLine();
            }
            out.close();
            out = null;
         }
         catch(IOException e){
            JOptionPane.showMessageDialog(this, "文件存储失败", "Error",
                                          JOptionPane.ERROR_MESSAGE);
         }
      }
   }

  
 
}

public class LALR_Creator{
   public static void main(String argv[]){
      Mywin win = new Mywin("LALR(1)分析表生成器");
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -