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

📄 cmm.java

📁 JAVA的CMM编译器(词法+语法+语义+输出结果)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Cmm.java
 *
 * Created on 2007年10月4日, 下午6:39
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package cmm;
import java.awt.*;
import java.util.StringTokenizer;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.util.LinkedList;
//import java.lang.Runtime;


/**
 *
 * @author lx
 */




public class Cmm extends JFrame implements ActionListener{
    //定义变量
    private TextArea input = new TextArea();
    
    private JButton open = new JButton("打开文件");
    private JButton clean = new JButton("清空");
    private JButton analyse = new JButton("分析");
    JButton show = new JButton("语法结果");
    JButton showIns = new JButton("目标代码");
    JButton showWord = new JButton("词法结果");
    private  JFileChooser fileChooser= new JFileChooser();
    TextArea output = new TextArea();
    TextArea output1 = new TextArea();
    public TextArea output2 = new TextArea();
    //public JTextField input1 = new JTextField();
    private int start,done,note,bracket,error,num,id,keyWord;
    private int state1=0;
    private boolean goon=true;
    private int col=1;
    private int pos =0;
    private int inputSize=0;
    private String inPut;
    private boolean finish=false;
    private int begin ,end;
    private String currentToken;
    //private boolean decimal=false;
    private boolean once=true;
    private boolean back=false;
    private String[] line;
    private boolean frist=true;
    private LinkedList tokens,ID;
    private boolean errorA =false;
    private Analyse ana;
    private Execution execution;
    private String userInput;
     private boolean flag = false;
    /** Creates a new instance of Cmm */
    public Cmm() {
        tokens = new LinkedList();
        ID  = new LinkedList();
        initial();
        setLayout(null);
        setSize(600,500);
        setTitle("CMM");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-getSize().width)/2,(Toolkit.getDefaultToolkit().getScreenSize().height-getSize().height)/2);
        setVisible(true);
        
    }
    //初始化界面
    public void initial(){
        //输入文本框
        input.setEditable(true);
        input.setBounds(10,10,480,200);
        //用户输入
        //input1.setEditable(true);
        //input1.setBounds(10, 430, 480, 30);
        //input1.setRows(1);
        //input1.setVisible(false);
        //input1.setActionCommand("getC");
        //打开文件按钮
        open.setBounds(490,50,100,30);
        open.addActionListener(this);
        open.setActionCommand("open");
        //清空按钮
        clean.setBounds(490,100,100,30);
        clean.setActionCommand("clean");
        clean.addActionListener(this);
        //分析按钮
        analyse.setBounds(490,150,100,30);
        analyse.setActionCommand("analyse");
        analyse.addActionListener(this);
        //查看词法分析按钮
        showWord.setBounds(490, 200, 100, 30);
        showWord.setActionCommand("showWord");
        showWord.addActionListener(this);
        showWord.setVisible(false);
        //查看语法分析按钮
        show.setBounds(490,250,100,30);
        show.setActionCommand("show");
        show.addActionListener(this);
        show.setVisible(false);
        //查看目标代码按钮
        showIns.setBounds(490,300,100,30);
        showIns.setActionCommand("showIns");
        showIns.addActionListener(this);
        showIns.setVisible(false);
        //输出词法分析结果
        output.setBounds(10,225,480,235);
        output.setEditable(false);
        //输出语法分析结果
        output1.setBounds(10,225,480,235);
        output1.setEditable(false);
        output1.setVisible(false);
        //输出程序运行结果
        output2.setBounds(10,225,480,235);
        output2.setEditable(false);
        output2.setVisible(false);
        //文件读入
        BFileFilter   filter   =   new   BFileFilter();
        filter.addExtension("cmm");
        fileChooser.setFileFilter(filter);
        fileChooser.setAcceptAllFileFilterUsed(false);
        //添加组键
        this.add(clean);
        this.add(open);
        this.add(input);
        //this.add(input1);
        this.add(output);
        this.add(output1);
        this.add(output2);
        this.add(analyse);
        this.add(show);
        this.add(showIns);
        this.add(showWord);
      
        this.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent mouseEvent) {
                output1.setVisible(false);
            }
            
            public void mousePressed(MouseEvent mouseEvent) {
            }
            
            public void mouseReleased(MouseEvent mouseEvent) {
            }
            
            public void mouseEntered(MouseEvent mouseEvent) {
            }
            
            public void mouseExited(MouseEvent mouseEvent) {
            }});
            inPut ="";
            start=0;
            done=1;
            note=2;
            bracket=3;
            error=4;
            num=5;
            id=6;
            keyWord=7;
    }
    private Character getNext(){
        try{
            if(col==1&&frist){
                output.setText("第"+col+ "行:"+line[0]);
                frist=false;
            }
            if(pos<inputSize-1){
                return inPut.charAt(pos++);
                
            }else if (pos==inputSize-1){
                finish=true;
                return inPut.charAt(pos++);
            }else{
                return null;
            }
        }catch(Exception e){
            return null;
        }
    }
    
    //分析方法
    private void read() {
        
        ID.clear();
        tokens.clear();
        
        if(input.getText().equalsIgnoreCase("")){
            output.setText("输入为空");
        }else{
            inPut = input.getText();
            BufferedReader bufferedreader = new BufferedReader(new StringReader(inPut));
            int i=1;
            try{
                for (String s1 = bufferedreader.readLine(); s1 != null; s1 = bufferedreader.readLine()) {
                    
                    i++;
                }
                BufferedReader bufferedreader2 = new BufferedReader(new StringReader(inPut));
                line= new String[i];
                
                int j=0;
                for (String s1 = bufferedreader2.readLine(); s1 != null; s1 = bufferedreader2.readLine()) {
                    line[j]=s1;
                    j++;
                }
            }catch(Exception e){
            }
            inputSize =inPut.length();
            output.setText("");
            pos=0;
            finish=false;
            begin =0;
            end=0;
           // decimal=false;
            col=1;
            once=true;
            frist=true;
            
            while(!finish){
                
                analyse();
            }
            tokens.offer("#");
            ID.offer("#");
            
            if(!errorA){
                show.setVisible(true);
                output1.setText("");
                ana = new Analyse(tokens,ID,this);
                ana.an();
                ana.printTree();
                if(ana.getTo().equals("#")){
                    output1.append("语法分析结束!");
                    if(ana.isErr()==false){
                        showIns.setVisible(true);
                        output.setVisible(false);
                        output1.setVisible(false);
                        output2.setVisible(true);
                        execution = new Execution(ana,this);
                        execution.Run();
                    }else{
                        output1.append("未通过语义分析!");
                    }
                    
                }else{
                    output1.append("调用中有异常,退出语法分析! ");
                }
                
            }else{
                output.append("\n未通过词法分析!");
            }
            
        }
    }
    
    //判断是否为整数
    public   boolean   isInt   (String   str){
        try{
            Integer   I   =   new   Integer(str);
            return   true;
        }catch(Exception   e){
            return   false;
        }
        
    }
    
    private void isKW(String to){
        if(to.equalsIgnoreCase("real")||to.equalsIgnoreCase("int")||to.equalsIgnoreCase("Write")||to.equalsIgnoreCase("If")||to.equalsIgnoreCase("Else")||to.equalsIgnoreCase("Read")||to.equalsIgnoreCase("while")){
            tokens.offer(currentToken);
            ID.offer("KW");
            
            output.append("\n"+"保留字:  "+currentToken);
        }else {
            tokens.offer(currentToken);
            ID.offer("ID");
            output.append("\n"+"标示符:  "+currentToken);
        }
    }
    private void backup(){
        pos--;
        finish=false;
    }
    private void analyse(){
        boolean decimal = false;
        int state=start;
        begin=pos;
        while(state!=done){
            char a =getNext();
            
            switch (state) {
                case 0:
                    if(a=='+'){
                        tokens.offer(String.valueOf(a));
                        ID.offer("Com");
                        output.append("\n"+"特殊符号: +");
                        state=done;
                    }else if(a=='-'){
                        tokens.offer(String.valueOf(a));
                        ID.offer("Com");
                        output.append("\n"+"特殊符号: -");
                        state=done;
                    }else if(a=='*'){
                        tokens.offer(String.valueOf(a));
                        ID.offer("Com");
                        output.append("\n"+"特殊符号: *");
                        state=done;
                    }else if(a=='('){
                        tokens.offer(String.valueOf(a));
                        ID.offer("Com");
                        output.append("\n"+"特殊符号: (");
                        state=done;
                    }else if(a==')'){
                        tokens.offer(String.valueOf(a));
                        ID.offer("Com");
                        output.append("\n"+"特殊符号: )");
                        state=done;
                    }else if(a=='['){
                        tokens.offer(String.valueOf(a));
                        ID.offer("FG");
                        output.append("\n"+"特殊符号: [");
                        state=done;
                    }else if(a=='{'){
                        tokens.offer(String.valueOf(a));
                        ID.offer("FG");
                        output.append("\n"+"特殊符号: {");
                        state=done;
                    }else if(a=='}'){
                        tokens.offer(String.valueOf(a));
                        ID.offer("FG");
                        output.append("\n"+"特殊符号: }");
                        state=done;
                    }else if(a==']'){
                        tokens.offer(String.valueOf(a));
                        ID.offer("FG");
                        output.append("\n"+"特殊符号: ]");
                        state=done;
                    }else if(a=='\n'){
                        tokens.offer(String.valueOf(a));
                        ID.offer("NL");
                        col++;
                        output.append("\n"+"第"+col+"行:"+line[col-1]);
                        state=done;
                        

⌨️ 快捷键说明

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