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

📄 frame1.java~263~

📁 我用java写的词法分析器
💻 JAVA~263~
字号:
package 编译技术;

import java.io.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * <p>Title: </p>
 * <p>Description: 词法分析</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 边缘开发室</p>
 * @author 谭正辉
 * @version 1.0
 */

public class Frame1
    extends JFrame
{
  JPanel contentPane;
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea sourceArea = new JTextArea();
  JButton doButton1 = new JButton();
  JButton quitButton = new JButton();
  //源文件
  String str = null;
  JLabel jLabel1 = new JLabel();
  JScrollPane jScrollPane2 = new JScrollPane();
  JTextArea MessageBox = new JTextArea();
  JMenuBar jMenuBar1 = new JMenuBar();
  JMenu jMenu1 = new JMenu();
  JMenuItem jMenuItem1 = new JMenuItem();
  JMenuItem jMenuItem2 = new JMenuItem();
  ImageIcon image = new ImageIcon("src/image/letterTable.jpg");
  JMenuItem jMenuItem3 = new JMenuItem();
  int row, column, locate; //row为行,column为列
  char[] sourceChar; //要分析的字符数组
  int type; //类型
  String tempString; //用于存储当前字符串单元

  ReservedWordTable rwt;
  JButton doButton2 = new JButton();

  //Construct the frame
  public Frame1()
  {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try
    {
      jbInit();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception
  {
    contentPane = (JPanel)this.getContentPane();
    contentPane.setLayout(null);
    this.setJMenuBar(jMenuBar1);
    this.setResizable(false);
    this.setSize(new Dimension(615, 459));
    this.setTitle("词法分析");
    jScrollPane1.setBounds(new Rectangle(8, 14, 448, 222));
    sourceArea.setToolTipText("输入要词法分析的程序源码");
    sourceArea.setLineWrap(true);
    sourceArea.setTabSize(4);
    doButton1.setBounds(new Rectangle(475, 23, 102, 36));
    doButton1.setText("词法分析");
    doButton1.addActionListener(new Frame1_doButton_actionAdapter(this));
    quitButton.setText("退出");
    quitButton.addActionListener(new Frame1_quitButton_actionAdapter(this));
    quitButton.setBounds(new Rectangle(475, 343, 102, 36));
    jLabel1.setText("编译信息如下");
    jLabel1.setBounds(new Rectangle(8, 237, 446, 21));
    MessageBox.setBorder(BorderFactory.createLoweredBevelBorder());
    jScrollPane2.setBounds(new Rectangle(8, 256, 448, 126));
    MessageBox.setBorder(null);
    MessageBox.setText("");
    MessageBox.setEditable(false);
    jMenu1.setText("系统信息");
    jMenuItem1.setText("类别码表");
    jMenuItem1.addActionListener(new Frame1_jMenuItem1_actionAdapter(this));
    jMenuItem2.setText("关于本软件");
    jMenuItem2.addActionListener(new Frame1_jMenuItem2_actionAdapter(this));
    jMenuItem3.setText("打开文件");
    jMenuItem3.addActionListener(new Frame1_jMenuItem3_actionAdapter(this));
    doButton2.setBounds(new Rectangle(475, 177, 102, 36));
    doButton2.setText("语法分析");
    doButton2.addActionListener(new Frame1_jButton1_actionAdapter(this));
    contentPane.add(jScrollPane1, null);
    contentPane.add(jLabel1, null);
    contentPane.add(jScrollPane2, null);
    contentPane.add(quitButton, null);
    contentPane.add(doButton2);
    contentPane.add(doButton1, null);
    jScrollPane2.getViewport().add(MessageBox, null);
    jScrollPane1.getViewport().add(sourceArea, null);
    jMenuBar1.add(jMenu1);
    jMenu1.add(jMenuItem3);
    jMenu1.add(jMenuItem1);
    jMenu1.add(jMenuItem2);
  }

  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e)
  {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING)
    {
      System.exit(0);
    }
  }

  void quitButton_actionPerformed(ActionEvent e)
  {
    //退出程序
    System.exit(0);
  }

  void doButton_actionPerformed(ActionEvent e)
  {
    MessageBox.setText(" 词法分析结果如下");
    column = row = 1;
    locate = 0;

    //实例化构建Hash表存入保留字的类
    rwt = new ReservedWordTable();
    //获取待编译的字符串并存放在一个字符数组里面
    String source = sourceArea.getText().toString();
    sourceChar = new char[source.length()];
    sourceChar = source.toCharArray();

    while (locate < sourceChar.length)
    {
      //对源程序进行词法分析
      startWordAnalyse();
    }
    MessageBox.setText(MessageBox.getText() + "\n 词法分析完成");
    //保存结果
    saveResult();
  }

  public void startWordAnalyse()
  {
    switch (sourceChar[locate])
    {
      case '+':
      {
        type = 13;
        tempString = "+";
        locate++;
        column++;
        break;
      }
      case '-':
      {
        type = 14;
        tempString = "-";
        locate++;
        column++;
        break;
      }
      case '*':
      {
        type = 15;
        tempString = "*";
        locate++;
        column++;
        break;
      }
      case '/':
      {
        type = 16;
        tempString = "/";
        locate++;
        column++;
        break;
      }
      //:=
      case ':':
      {
        if (locate != sourceChar.length - 1)
        {
          if (sourceChar[locate + 1] == '=')
          {
            locate++;
            column++;
            type = 17;
            tempString = ":=";
          }
          //:单独出现在中间位置
          else
          {
            type = 1000;
            tempString = ":";
          }
        }
        //:出现在结束位置
        else if (locate == sourceChar.length - 1)
        {
          type = 1000;
          tempString = ":";
        }
        locate++;
        column++;
        break;
      }
      //<=,<>,<
      case '<':
      {
        if (locate != sourceChar.length - 1)
        {
          if ( (sourceChar[locate + 1] != '=') &&
              (sourceChar[locate + 1] != '>'))
          {
            //识别<
            type = 20;
            tempString = "<";
          }
          else if (sourceChar[locate + 1] == '=')
          {
            //识别<=
            locate++;
            column++;
            type = 22;
            tempString = "<=";
          }
          else if (sourceChar[locate + 1] == '>')
          {
            //识别<>
            locate++;
            column++;
            type = 21;
            tempString = "<>";
          }
        }
        locate++;
        column++;
        break;
      }

      case '>':
      {
        if (locate != sourceChar.length - 1)
        {
          if (sourceChar[locate + 1] != '=')
          {
            //识别>
            type = 23;
            tempString = ">";
          }
          else
          {
            locate++;
            column++;
            type = 24;
            tempString = ">=";
          }
        }
        locate++;
        column++;
        break;
      }

      case '=':
      {
        type = 25;
        tempString = "=";
        locate++;
        column++;
        break;
      }
      case '#':
      {
        type = 0;
        tempString = "#";
        column++;
        locate++;
        break;
      }
      case ';':
      {
        type = 18;
        tempString = ";";
        column++;
        locate++;
        break;
      }
      default:
      {
        //如果是空格
        if (sourceChar[locate] == ' ')
        {
          //对type=100为不做任何处理,只移位置
          type = 100;
          locate++;
          column++;
        }
        //如果是换行符
        else if (sourceChar[locate] == '\n' || sourceChar[locate] == '\r')
        {
          //对type=100为不做任何处理
          tempString = "";
          type = 100;
          column = 1;
          row++;
          locate++;
        }
        else if (sourceChar[locate] == '\t')
        {
          //tempString="";
          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);
    }
    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);
      jfc.setFileFilter(new fileFilter());
      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)
  {

  }
}

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 + -