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

📄 frame1.java

📁 我用java写的词法分析器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          type = 100;
          column = column + 4;
          locate = locate + 1;
        }

        //首字符为字母
        else if (this.isLetter(sourceChar[locate]))
        {
          type = 10;
          //如果不是结束字母,则继续下找,再判断是不是保留字,如果是,返回类型,如果不是,则为标识符,type=10;
          tempString = String.valueOf(sourceChar[locate]);

          column++;
          locate++;

          doFirstLetter();

          if (rwt.getKeyWordType(tempString) != -1)
          {
            type = rwt.getKeyWordType(tempString);
          }
        }
        //首字符为数字
        else if (this.isDigit(sourceChar[locate]))
        {
          type = 11;
          //如果不是结束字母,则继续下找,再判断是不是保留字,如果是,返回类型,如果不是,则为标识符,type=10;
          tempString = String.valueOf(sourceChar[locate]);

          column++;
          locate++;

          doFirstDigit();
        }
        //非法标识符
        else
        {
          type = 1000;
          tempString = String.valueOf(sourceChar[locate]);
          locate++;
          column++;
        }
      }
    }
    if (type == 1000)
    {
      MessageBox.setText(MessageBox.getText() + "\n 行 " + (row) + ",列 " +
                         (column - tempString.length()) +
                         " 不能识别的标识符 " + tempString);
      //如果有错误信息,则没有通过词法分析,更不能进行语法分析
      passWordAnalyse=false;
    }
    else if (type != 100)
    {
      MessageBox.setText(MessageBox.getText() + "\n [" + type + "," +
                         tempString +
                         "]");
    }
  }

  void doFirstLetter()
  {
    if (locate != sourceChar.length - 1)
    {
      if (this.isLetter(sourceChar[locate]) || this.isDigit(sourceChar[locate]))
      {
        tempString = tempString + sourceChar[locate];
        locate++;
        column++;
        doFirstLetter();
      }
    }
    else
    {
      tempString = tempString + sourceChar[locate];
      locate++;
      column++;
    }
  }

  void doFirstDigit()
  {
    if (locate != sourceChar.length - 1)
    {
      if (this.isDigit(sourceChar[locate]))
      {
        tempString = tempString + sourceChar[locate];
        locate++;
        column++;
        doFirstDigit();
      }
      else if (this.isLetter(sourceChar[locate]))
      {
        type = 1000;
        tempString = tempString + sourceChar[locate];
        locate++;
        column++;
        doFirstDigit();
      }
    }
    else
    {
      if (this.isDigit(sourceChar[locate]))
      {
        tempString = tempString + sourceChar[locate];
        locate++;
        column++;
      }
      else if (this.isLetter(sourceChar[locate]))
      {
        type = 1000;
        tempString = tempString + sourceChar[locate];
        locate++;
        column++;
      }
    }
  }

  //保存词法分析的结果
  void saveResult()
  {
    try
    {
      String fileName = "词法分析结果.rtf";
      FileOutputStream fos = null;
      String str = MessageBox.getText();
      fos = new FileOutputStream(fileName);
      fos.write(str.getBytes());
      fos.close();
    }
    catch (IOException ev)
    {
      JOptionPane.showMessageDialog(this, "保存结果时出错");
    }
  }

  //判断是不是字母
  public static boolean isLetter(char ch)
  {
    return java.lang.Character.isLetter(ch);
  }

//判断是不是数字
  public static boolean isDigit(char ch)
  {
    return java.lang.Character.isDigit(ch);
  }

  void jMenuItem2_actionPerformed(ActionEvent e)
  {
    JOptionPane.showMessageDialog(this, "本软件为编译之词法分析器,版权属 谭正辉", "信息提示",
                                  JOptionPane.OK_OPTION);
  }

  void jMenuItem1_actionPerformed(ActionEvent e)
  {
    JOptionPane.showMessageDialog(this, "", "类别码表", JOptionPane.OK_OPTION,
                                  image);
  }

  //打开已经写好的源程序
  void jMenuItem3_actionPerformed(ActionEvent e)
  {
    JFileChooser jfc = new JFileChooser();
    try
    {
      jfc.showOpenDialog(this);
      if (jfc.getSelectedFile().toString() != "")
      {
        String fileName = jfc.getSelectedFile().toString();
        FileInputStream fis = null;
        try
        {
          fis = new FileInputStream(fileName); //创建打开文件输入流
          int size = fis.available();

          byte[] bytes = new byte[size]; //建立字节数组对象
          fis.read(bytes);
          str = new String(bytes);
        }
        catch (IOException ex)
        {
        }
        finally
        {
          try
          {
            fis.close();
          }
          catch (IOException ev)
          {
          }
        }
        if (str != "")
        {
          sourceArea.setText(str);
        }
      }
    }
    catch (Exception ex1)
    {
    }
  }

  public void doButton2_actionPerformed(ActionEvent e)
  {
    if(passWordAnalyse==false)
    {
      JOptionPane.showMessageDialog(this,"没有通过词法分析,请先按提示的错误修正源\n程序,没有词法错误之后,才能进行语法分析","这是不允许的",JOptionPane.OK_OPTION);
    }
    else
    {
      //进行语法分析
      startLanguageAnalyse();
    }
  }

  //语法分析方法块
  void startLanguageAnalyse()
  {

  }
}

class Frame1_jButton1_actionAdapter
    implements ActionListener
{
  private Frame1 adaptee;
  Frame1_jButton1_actionAdapter(Frame1 adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {

    adaptee.doButton2_actionPerformed(e);
  }
}

class Frame1_quitButton_actionAdapter
    implements java.awt.event.ActionListener
{
  Frame1 adaptee;

  Frame1_quitButton_actionAdapter(Frame1 adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.quitButton_actionPerformed(e);
  }
}

class Frame1_doButton_actionAdapter
    implements java.awt.event.ActionListener
{
  Frame1 adaptee;

  Frame1_doButton_actionAdapter(Frame1 adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.doButton_actionPerformed(e);
  }
}

class Frame1_jMenuItem2_actionAdapter
    implements java.awt.event.ActionListener
{
  Frame1 adaptee;

  Frame1_jMenuItem2_actionAdapter(Frame1 adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.jMenuItem2_actionPerformed(e);
  }
}

class Frame1_jMenuItem1_actionAdapter
    implements java.awt.event.ActionListener
{
  Frame1 adaptee;

  Frame1_jMenuItem1_actionAdapter(Frame1 adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.jMenuItem1_actionPerformed(e);
  }
}

class Frame1_jMenuItem3_actionAdapter
    implements java.awt.event.ActionListener
{
  Frame1 adaptee;

  Frame1_jMenuItem3_actionAdapter(Frame1 adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.jMenuItem3_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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