Jm2.java
资源名称:Parsing.rar [点击查看]
上传用户:xiaozhuqw
上传日期:2021-11-10
资源大小:21k
文件大小:8k
源码类别:
词法分析
开发平台:
Java
- import java.awt.BorderLayout;
- import javax.swing.JFrame;
- import javax.swing.JMenuBar;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import javax.swing.JToolBar;
- import javax.swing.JButton;
- import javax.swing.JSlider;
- import javax.swing.JSplitPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextPane;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JOptionPane;
- import javax.swing.JFileChooser;
- import java.io.File;
- import java.io.IOException;
- import java.util.Stack;
- import javax.swing.tree.DefaultMutableTreeNode;
- import javax.swing.JTree;
- import javax.swing.JPanel;
- import java.awt.Dimension;
- import javax.swing.JTabbedPane;
- import javax.swing.event.AncestorEvent;
- import javax.swing.event.AncestorListener;
- /**
- * <p>Title: </p>
- *
- * <p>Description: </p>
- *
- * <p>Copyright: Copyright (c) 2007</p>
- *
- * <p>Company: </p>
- *
- * @author not attributable
- * @version 1.0
- */
- public class Jm2 extends JFrame {
- String filename = new String();
- BorderLayout borderLayout1 = new BorderLayout();
- JMenuBar jMenuBar1 = new JMenuBar();
- JMenu jMenuFile = new JMenu();
- JMenu jMenuRun = new JMenu();
- JMenu jMenuHelp = new JMenu();
- JMenuItem jMenuOpen = new JMenuItem();
- JSplitPane jSplitPane1 = new JSplitPane();
- JScrollPane jScrollPane1 = new JScrollPane();
- JMenuItem jMenuItem1 = new JMenuItem();
- JTabbedPane jTabbedPane1 = new JTabbedPane();
- public Jm2() {
- try {
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- jbInit();
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- private void jbInit() throws Exception {
- getContentPane().setLayout(borderLayout1);
- this.setJMenuBar(jMenuBar1);
- this.setTitle("句法分析器");
- jMenuFile.setText("File");
- jMenuRun.setText("Run");
- jMenuHelp.setText("Help");
- jMenuOpen.setText("Open");
- jMenuOpen.addActionListener(new Jm2_jMenuOpen_actionAdapter(this));
- jMenuItem1.setText("Run");
- jMenuItem1.addActionListener(new Jm2_jMenuItem1_actionAdapter(this));
- jMenuBar1.add(jMenuFile);
- jMenuBar1.add(jMenuRun);
- jMenuBar1.add(jMenuHelp);
- jMenuFile.add(jMenuOpen);
- this.getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
- jSplitPane1.add(jScrollPane1, JSplitPane.LEFT);
- jSplitPane1.add(jTabbedPane1, JSplitPane.RIGHT);
- jMenuRun.add(jMenuItem1);
- }
- public static void main(String[] args){
- Jm2 f = new Jm2();
- f.setSize(640,480);
- f.setVisible(true);
- }
- public void jMenuOpen_actionPerformed(ActionEvent e) {
- JFrame f = new JFrame();
- JFileChooser jFileChooser1 = new JFileChooser("c:\Documents and Settings\");
- int result = jFileChooser1.showOpenDialog(f);
- if(result == JFileChooser.APPROVE_OPTION){
- File file = jFileChooser1.getSelectedFile();
- //是文件而非目录则读
- if (file.isFile()) {
- FileTest ft = new FileTest();
- try {
- //System.out.println("nn" + file.getPath());
- filename = file.getPath();
- ft.ReadFile(filename);
- JTextPane jTextPane = new JTextPane();
- jTextPane.setText(ft.getBuffer());
- //添加JTEXTPANE
- jScrollPane1.getViewport().add(jTextPane);
- } catch (IOException ioe) {
- JOptionPane.showMessageDialog(this, "FILE ERROR",
- "FILE ERROR",
- JOptionPane.ERROR_MESSAGE);
- } //catch
- try{
- //调用PARSING
- }catch(IllegalArgumentException err){
- JOptionPane.showMessageDialog(this, "FILE IS NULL", "FILE ERROR",
- JOptionPane.ERROR_MESSAGE);
- }
- }//IF ifFile()
- else
- JOptionPane.showMessageDialog(this, e.getActionCommand()
- + "The type of File is not allowed", "ERROR", JOptionPane.ERROR_MESSAGE);
- }//if result
- }
- public DefaultMutableTreeNode getRoot(String result){
- DefaultMutableTreeNode root = new DefaultMutableTreeNode();
- DefaultMutableTreeNode e, e1;
- Stack s1 = new Stack();
- Stack s2 = new Stack();
- String a;
- String lB= new String("(");
- String rB = new String(")");
- String p = new String("+");
- String[] ss = result.split(" ");
- //s1.push(new DefaultMutableTreeNode(ss[0]));
- for(int i = 0;i < ss.length;i++){
- a = ss[i];
- //左括
- if(a.equals(lB)){
- s2.push(a);
- }
- //"+"
- /*符号不出栈*/
- else if(a.equals(p)){
- //出栈
- e = (DefaultMutableTreeNode)s1.pop();
- e1 = (DefaultMutableTreeNode)s1.pop();
- e1.add(e);
- s1.push(e1);
- }
- //右括,都出栈
- else if(a.equals(rB)){
- s2.pop();
- e = (DefaultMutableTreeNode)s1.pop();
- e1 = (DefaultMutableTreeNode)s1.pop();
- e1.add(e);
- s1.push(e1);
- }
- //树的结点内容
- else
- s1.push(new DefaultMutableTreeNode(a));
- }
- root = (DefaultMutableTreeNode)s1.pop();
- return root;
- }
- public void jMenuItem1_actionPerformed(ActionEvent e) {
- /* DefaultMutableTreeNode root = this.getRoot();
- JTree tree = new JTree(root);
- //tree.setPreferredSize(new Dimension(200, 300));
- jScrollPane2.getViewport().add(tree);*/
- Parsing p = new Parsing();
- String result = new String();
- JTree[] tree = new JTree[20];
- JScrollPane[] js = new JScrollPane[20];
- String[] ss; //存放每种分析结果的字符串
- String a;
- try {
- result = p.BottomUp(filename);
- ss = result.split("nn");
- for(int i = 0;i<ss.length;i++){
- a = ss[i];
- int k = a.indexOf("S");
- result = a.substring(k);
- tree[i] = new JTree(this.getRoot(result));
- js[i] = new JScrollPane();
- js[i].getViewport().add(tree[i]);
- jTabbedPane1.addTab((new Integer(i + 1)).toString(), js[i]);
- }
- //result = new String("S ( vp ( vp ( v(穿) ) + np ( ap ( a(好) ) + np ( n(衣服) ) ) ) )");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- class Jm2_jMenuItem1_actionAdapter implements ActionListener {
- private Jm2 adaptee;
- Jm2_jMenuItem1_actionAdapter(Jm2 adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jMenuItem1_actionPerformed(e);
- }
- }
- class Jm2_jMenuOpen_actionAdapter implements ActionListener {
- private Jm2 adaptee;
- Jm2_jMenuOpen_actionAdapter(Jm2 adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jMenuOpen_actionPerformed(e);
- }
- }