Jm2.java
上传用户:xiaozhuqw
上传日期:2021-11-10
资源大小:21k
文件大小:8k
源码类别:

词法分析

开发平台:

Java

  1. import java.awt.BorderLayout;
  2. import javax.swing.JFrame;
  3. import javax.swing.JMenuBar;
  4. import javax.swing.JMenu;
  5. import javax.swing.JMenuItem;
  6. import javax.swing.JToolBar;
  7. import javax.swing.JButton;
  8. import javax.swing.JSlider;
  9. import javax.swing.JSplitPane;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTextPane;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JFileChooser;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.Stack;
  19. import javax.swing.tree.DefaultMutableTreeNode;
  20. import javax.swing.JTree;
  21. import javax.swing.JPanel;
  22. import java.awt.Dimension;
  23. import javax.swing.JTabbedPane;
  24. import javax.swing.event.AncestorEvent;
  25. import javax.swing.event.AncestorListener;
  26. /**
  27.  * <p>Title: </p>
  28.  *
  29.  * <p>Description: </p>
  30.  *
  31.  * <p>Copyright: Copyright (c) 2007</p>
  32.  *
  33.  * <p>Company: </p>
  34.  *
  35.  * @author not attributable
  36.  * @version 1.0
  37.  */
  38. public class Jm2 extends JFrame {
  39.         String filename = new String();
  40.     BorderLayout borderLayout1 = new BorderLayout();
  41.     JMenuBar jMenuBar1 = new JMenuBar();
  42.     JMenu jMenuFile = new JMenu();
  43.     JMenu jMenuRun = new JMenu();
  44.     JMenu jMenuHelp = new JMenu();
  45.     JMenuItem jMenuOpen = new JMenuItem();
  46.     JSplitPane jSplitPane1 = new JSplitPane();
  47.     JScrollPane jScrollPane1 = new JScrollPane();
  48.     JMenuItem jMenuItem1 = new JMenuItem();
  49.     JTabbedPane jTabbedPane1 = new JTabbedPane();
  50.     public Jm2() {
  51.         try {
  52.             setDefaultCloseOperation(EXIT_ON_CLOSE);
  53.             jbInit();
  54.         } catch (Exception exception) {
  55.             exception.printStackTrace();
  56.         }
  57.     }
  58.     private void jbInit() throws Exception {
  59.         getContentPane().setLayout(borderLayout1);
  60.         this.setJMenuBar(jMenuBar1);
  61.         this.setTitle("句法分析器");
  62.         jMenuFile.setText("File");
  63.         jMenuRun.setText("Run");
  64.         jMenuHelp.setText("Help");
  65.         jMenuOpen.setText("Open");
  66.         jMenuOpen.addActionListener(new Jm2_jMenuOpen_actionAdapter(this));
  67.         jMenuItem1.setText("Run");
  68.         jMenuItem1.addActionListener(new Jm2_jMenuItem1_actionAdapter(this));
  69.         jMenuBar1.add(jMenuFile);
  70.         jMenuBar1.add(jMenuRun);
  71.         jMenuBar1.add(jMenuHelp);
  72.         jMenuFile.add(jMenuOpen);
  73.         this.getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
  74.         jSplitPane1.add(jScrollPane1, JSplitPane.LEFT);
  75.         jSplitPane1.add(jTabbedPane1, JSplitPane.RIGHT);
  76.         jMenuRun.add(jMenuItem1);
  77.     }
  78.     public static void main(String[] args){
  79.         Jm2 f = new Jm2();
  80.         f.setSize(640,480);
  81.         f.setVisible(true);
  82.     }
  83.     public void jMenuOpen_actionPerformed(ActionEvent e) {
  84.         JFrame f = new JFrame();
  85.         JFileChooser jFileChooser1 = new JFileChooser("c:\Documents and Settings\");
  86.         int result = jFileChooser1.showOpenDialog(f);
  87.         if(result == JFileChooser.APPROVE_OPTION){
  88.             File file = jFileChooser1.getSelectedFile();
  89.             //是文件而非目录则读
  90.             if (file.isFile()) {
  91.                 FileTest ft = new FileTest();
  92.                 try {
  93.                     //System.out.println("nn" + file.getPath());
  94.                         filename = file.getPath();
  95.                     ft.ReadFile(filename);
  96.                     JTextPane jTextPane = new JTextPane();
  97.                     jTextPane.setText(ft.getBuffer());
  98.                     //添加JTEXTPANE
  99.                     jScrollPane1.getViewport().add(jTextPane);
  100.                   } catch (IOException ioe) {
  101.                     JOptionPane.showMessageDialog(this, "FILE ERROR",
  102.                                                   "FILE ERROR",
  103.                                                   JOptionPane.ERROR_MESSAGE);
  104.                   } //catch
  105.                   try{
  106.                   //调用PARSING
  107.                   }catch(IllegalArgumentException err){
  108.                     JOptionPane.showMessageDialog(this, "FILE IS NULL", "FILE ERROR",
  109.                                          JOptionPane.ERROR_MESSAGE);
  110.                  }
  111.               }//IF ifFile()
  112.               else
  113.                   JOptionPane.showMessageDialog(this, e.getActionCommand()
  114.                      + "The type of File is not allowed", "ERROR", JOptionPane.ERROR_MESSAGE);
  115.         }//if result
  116.     }
  117.     public DefaultMutableTreeNode getRoot(String result){
  118.         DefaultMutableTreeNode root = new  DefaultMutableTreeNode();
  119.         DefaultMutableTreeNode e, e1;
  120.         Stack s1 = new Stack();
  121.         Stack s2 = new Stack();
  122.         String a;
  123.         String lB= new String("(");
  124.         String rB = new String(")");
  125.         String p = new String("+");
  126.         String[] ss = result.split(" ");
  127.         //s1.push(new DefaultMutableTreeNode(ss[0]));
  128.         for(int i = 0;i < ss.length;i++){
  129.                 a = ss[i];
  130.                 //左括
  131.                 if(a.equals(lB)){
  132.                         s2.push(a);
  133.                 }
  134.                 //"+"
  135.                 /*符号不出栈*/
  136.                 else if(a.equals(p)){
  137.                         //出栈
  138.                         e = (DefaultMutableTreeNode)s1.pop();
  139.                         e1 = (DefaultMutableTreeNode)s1.pop();
  140.                         e1.add(e);
  141.                         s1.push(e1);
  142.                 }
  143.                 //右括,都出栈
  144.                 else if(a.equals(rB)){
  145.                         s2.pop();
  146.                         e = (DefaultMutableTreeNode)s1.pop();
  147.                         e1 = (DefaultMutableTreeNode)s1.pop();
  148.                         e1.add(e);
  149.                         s1.push(e1);
  150.                 }
  151.                 //树的结点内容
  152.                 else 
  153.                 s1.push(new  DefaultMutableTreeNode(a));
  154.         }
  155.         root = (DefaultMutableTreeNode)s1.pop();
  156.         return root;
  157.     }
  158.     public void jMenuItem1_actionPerformed(ActionEvent e) {
  159.        /* DefaultMutableTreeNode root = this.getRoot();
  160.        JTree tree = new JTree(root);
  161.        //tree.setPreferredSize(new Dimension(200, 300));
  162.        jScrollPane2.getViewport().add(tree);*/
  163.         Parsing p = new Parsing();
  164.         String result = new String();
  165.         JTree[] tree = new JTree[20];
  166.         JScrollPane[] js = new JScrollPane[20];
  167.         String[] ss; //存放每种分析结果的字符串
  168.         String a;
  169.                 try {
  170.                         result = p.BottomUp(filename);
  171.                         ss = result.split("nn");
  172.                         for(int i = 0;i<ss.length;i++){
  173.                                 a = ss[i];
  174.                                 int k = a.indexOf("S");
  175.                                 result = a.substring(k);
  176.                                 tree[i] = new JTree(this.getRoot(result));
  177.                                 js[i] = new JScrollPane();
  178.                                 js[i].getViewport().add(tree[i]);
  179.                                 jTabbedPane1.addTab((new Integer(i + 1)).toString(), js[i]);
  180.                         }
  181.                         //result = new String("S ( vp ( vp ( v(穿) ) + np ( ap ( a(好) ) + np ( n(衣服) ) ) ) )");
  182.                 } catch (IOException e1) {
  183.                         // TODO Auto-generated catch block
  184.                         e1.printStackTrace();
  185.                 }
  186.     }
  187. }
  188. class Jm2_jMenuItem1_actionAdapter implements ActionListener {
  189.     private Jm2 adaptee;
  190.     Jm2_jMenuItem1_actionAdapter(Jm2 adaptee) {
  191.         this.adaptee = adaptee;
  192.     }
  193.     public void actionPerformed(ActionEvent e) {
  194.         adaptee.jMenuItem1_actionPerformed(e);
  195.     }
  196. }
  197. class Jm2_jMenuOpen_actionAdapter implements ActionListener {
  198.     private Jm2 adaptee;
  199.     Jm2_jMenuOpen_actionAdapter(Jm2 adaptee) {
  200.         this.adaptee = adaptee;
  201.     }
  202.     public void actionPerformed(ActionEvent e) {
  203.         adaptee.jMenuOpen_actionPerformed(e);
  204.     }
  205. }