打印

[学习心得] 词法分析,语法分析(编译原理)

词法分析,语法分析(编译原理)

都大三了,还没发过自己的写的东西呢,今天发一下我做的编译原理的上机报告题目,与大家一起学习学习,如有不好的地方,希望大家提醒.
  一共有5个java文件,其中的cifa.java是main(),cifaFrame.java是主要的代码部分,其它三个是用来建栈的

[ 本帖最后由 天下第一疯 于 2008-5-6 10:20 编辑 ]
到天涯海角等我!
也游互助游论坛!

TOP

cifa

package com.zhida;

import javax.swing.*;

public class cifa {
    public static void main(String[] args){
        cifaFrame frame = new cifaFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
到天涯海角等我!
也游互助游论坛!

TOP

cifaFrame.java

package com.zhida;

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

class cifaFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JLabel jLabel = null;
    private JTextField jTextField = null;
    private JPanel jPanel = null;
    private JPanel jPanel1 = null;
    private JButton jButton = null;
    private JLabel jLabel1 = null;
    private String st1;
    private String st;
    private JPanel jPanel21 =  null;
    private JLabel jLabel2 = null;
    private JButton jButton1 = null;
    //定义的一个比较运算符优先级大小的数组: + - * / ( ) #
    String sign[]= {"+","-","*","/","(",")","#"};  
    char op[][]={{'>','>','<','<','<','>','>'},
                  {'>','>','<','<','<','>','>'},
                  {'>','>','>','>','<','>','>'},
                  {'>','>','>','>','<','>','>'},
                  {'<','<','<','<','<','=',' '},
                  {'>','>','>','>',' ','>','>'},
                  {'<','<','<','<','<',' ','='}};
    private JEditorPane jEditorPane = null;
    private JScrollPane jScrollPane = null;
    private JEditorPane jEditorPane1 = null;
    public cifaFrame() {
        super();
        initialize();
    }
    private void initialize() {
        this.setSize(435, 336);
        this.setContentPane(getJContentPane());
        
        this.setTitle("词法分析    作者:陈奉斌 QQ:402027721");
    }
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jLabel2 = new JLabel();
            jLabel2.setText(" 语法分析过程 ");
            jLabel2.setFont(new Font("Dialog", Font.BOLD, 14));
            FlowLayout flowLayout = new FlowLayout();
            flowLayout.setAlignment(FlowLayout.LEFT);
            jLabel = new JLabel();
            jLabel.setText("   请输入表达式");
            jLabel.setFont(new Font("Dialog", Font.BOLD, 14));
            jLabel.setPreferredSize(new Dimension(110, 20));
            jContentPane = new JPanel();
            jContentPane.setBackground(new Color(238, 238, 238));
            jContentPane.setLayout(flowLayout);
            jContentPane.setPreferredSize(new Dimension(350, 275));
            jContentPane.add(jLabel, null);
            jContentPane.add(getJTextField(), null);
            jContentPane.add(getJButton(), null);
            jContentPane.add(getJPanel(), null);
            jContentPane.add(jLabel2, null);
            jContentPane.add(getJButton1(), null);
            jContentPane.add(getJScrollPane(), null);
        }
        return jContentPane;
    }
    private JTextField getJTextField() {
        if (jTextField == null) {
            jTextField = new JTextField();
            jTextField.setPreferredSize(new Dimension(210, 22));
            jTextField.setText("以像\"V=i....#\"以'#'结束");
        }
        return jTextField;
    }
    private JPanel getJPanel() {
        if (jPanel == null) {
            FlowLayout flowLayout1 = new FlowLayout();
            flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
            flowLayout1.setHgap(8);
            jPanel = new JPanel();
            jPanel.setRequestFocusEnabled(true);
            jPanel.setBackground(new Color(237, 235, 241));
            jPanel.setLayout(flowLayout1);
            jPanel.setPreferredSize(new Dimension(415, 85));
            jPanel.add(getJPanel1(), null);
            jPanel.add(getJPanel21(), null);
        }
        return jPanel;
    }
    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.setText("词法分析");
            jButton.setPreferredSize(new Dimension(86, 22));
            jButton.setFont(new Font("Dialog", Font.BOLD, 12));
            jButton.setVerticalAlignment(SwingConstants.CENTER);
            jButton.setVerticalTextPosition(SwingConstants.CENTER);
            jButton.setActionCommand("");
            jButton.setHorizontalAlignment(SwingConstants.LEFT);
            jButton.setHorizontalTextPosition(SwingConstants.LEFT);
            jButton.setMinimumSize(new Dimension(50, 25));
            jButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    try{               
                        st1=jTextField.getText(0,jTextField.getText().length()).trim();
                        String st2=jTextField.getText(0,jTextField.getText().length()-1).trim();
                        char[] str=st1.toCharArray();
                        int tr=0;
                        if(str[0]=='V'&&str[1]=='='&&tr==0){
                            tr=1;
                            for(int i=2,n=0;i<st1.length();i++){
                                int RT=0;
                                if(str=='i'&&RT==0){
                                    RT=1;
                                    int rt=0;
                                    if(str[i+1]==')'&&n>0&&rt==0){
                                        rt=1;
                                        if(str[i+2]=='#'){
                                            st="合法  "+st2;
                                            i=st1.length();
                                        }
                                        else if(str[i+2]=='+'||str[i+2]=='-'||str[i+2]=='*'||str[i+2]=='/'){
                                            i=i+2;
                                            --n;
                                        }
                                        else {
                                            st="不合法";
                                            i=st1.length();
                                        }
                                    }
                                    else if(str[i+1]=='+'||str[i+1]=='-'||str[i+1]=='*'||str[i+1]=='/'&&rt==0){
                                        rt=1;
                                        i+=1;
                                    }
                                    else if (str[i+1]=='#'&&rt==0&&n==0){
                                        rt=1;
                                        st="合法  "+st2;
                                        i=st1.length();
                                    }
                                    else if(rt==0){
                                        rt=1;
                                        st="不合法";
                                        i=st1.length();
                                    }
                                }
                                else if(str=='('&&RT==0){
                                    RT=1;
                                    ++n;
                                }
                                else if(RT==0){
                                    RT=1;
                                    st="不合法";
                                    i=st1.length();
                                }                           
                            }   
                        }
                        else if(tr==0){
                            st="不合法";
                        }
                        jEditorPane.setText("");
                        jEditorPane.setText(st);
                    }
                    catch(Exception e){}               
                }
            });
        }
        return jButton;
    }
    private JPanel getJPanel1() {
        if (jPanel1 == null) {
            FlowLayout flowLayout2 = new FlowLayout();
            flowLayout2.setAlignment(java.awt.FlowLayout.LEFT);
            jLabel1 = new JLabel();
            jLabel1.setText("显示判断结果");
            jLabel1.setFont(new Font("Dialog", Font.BOLD, 14));
            jLabel1.setBackground(Color.white);
            jLabel1.setPreferredSize(new Dimension(95, 20));
            jPanel1 = new JPanel();
            jPanel1.setBackground(new Color(237, 238, 239));
            jPanel1.setLayout(flowLayout2);
            jPanel1.setPreferredSize(new Dimension(105, 75));
            jPanel1.add(jLabel1, null);
        }
        return jPanel1;
    }
    private JPanel getJPanel21() {
        if (jPanel21 == null) {
            GridLayout gridLayout = new GridLayout();
            gridLayout.setRows(1);
            jPanel21 = new JPanel();
            jPanel21.setLayout(gridLayout);
            jPanel21.setPreferredSize(new Dimension(285, 75));
            jPanel21.add(getJEditorPane(), null);
        }
        return jPanel21;
    }
    private JEditorPane getJEditorPane() {
        if (jEditorPane == null) {
            jEditorPane = new JEditorPane();
            jEditorPane.setPreferredSize(new Dimension(260, 72));
            jEditorPane.setText("");
        }
        return jEditorPane;
    }
    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setPreferredSize(new Dimension(410, 145));
            jScrollPane.setViewportView(getJEditorPane1());
        }
        return jScrollPane;
    }
    private JEditorPane getJEditorPane1() {
        if (jEditorPane1 == null) {
            jEditorPane1 = new JEditorPane();
        }
        return jEditorPane1;
    }


[ 本帖最后由 天下第一疯 于 2008-5-6 17:02 编辑 ]
到天涯海角等我!
也游互助游论坛!

TOP

private JButton getJButton1() {
        if (jButton1 == null) {
            jButton1 = new JButton();
            jButton1.setText("语法分析");
            jButton1.setPreferredSize(new Dimension(86, 22));
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    Stack();
                }
            });
        }
        return jButton1;
    }
    private void Stack(){
        try{
            StackArrayBased stack1 = new StackArrayBased();
            StackArrayBased stack2 = new StackArrayBased();
            //定义一个方法判断是不是运算符,是的话进栈stack1,不是的就去栈stack2(放i)
            
            String[] sta = new String[3];
            String[] stA = new String[50];
            String[] stR = new String[jTextField.getText().length()];
            int m=0;
            String hc="#";
            stack1.push(hc);
            st1=jTextField.getText(0,jTextField.getText().length()).trim();
            char[] str=st1.toCharArray();
            for(int i=0;i<jTextField.getText().length();i++){
                stR=String.valueOf(str);
            }
            for(int j=2;j<jTextField.getText().length();j++){
                if(js(stR[j])==1){
                    stack2.push(stR[j]);
                }
                else if(js(stR[j])==0){  //为0时表示是运算符,为1时表示是操作数
                    char char1;
                    String stA1="";
                    char1=jd(stR[j],stack1.peek());
                    switch(char1){
                    case '>':
                        sta[0]=stack2.pop();
                        sta[1]=stack2.pop();
                        sta[2]=stack1.pop();
                        stA[m++]=stA1+sta[1]+sta[2]+sta[0];
                        String E="E"+m;
                        stack2.push(E);
                        j--;
                        break;
                    case '<':
                        stack1.push(stR[j]);
                        break;
                    case '=':
                        stack1.pop();
                        break;
                    }
                }
            }
            String stri="";
            String stri1="第";
            String stri2="次的操作E";   
            for(int a=0;a<m;a++){
                stri=stri+stri1+(a+1)+stri2+(a+1)+"="+stA[a]+"\n";
            }
            jEditorPane1.setText(stri);
        }
        catch(Exception e){}   
    }
    //js是判断是运算符还是操作数,0表示是运算符,1表示是操作数
    private int js(String ch){
        if(ch.equals("i")){
            return 1;
        }
        elseif(ch.equals("+")||ch.equals("-")||ch.equals("/")||ch.equals("*")||ch.equals("(")||ch.equals(")")||ch.equals("#"))return0;
        else return -1;
    }
    //jd是判断两个运算符的优先级的大小,0表示相等,1表示大于,-1表示小于
    private char jd(String ch0,String ch1){
        int i=0,j=0;
        while(!ch0.equals(sign))i++;
        while(!ch1.equals(sign[j]))j++;
        return op[j];
  }
}
到天涯海角等我!
也游互助游论坛!

TOP

StackException.java

package com.zhida;

public class StackException extends java.lang.RuntimeException {
    public StackException(String s){
        super(s);
    }
}
到天涯海角等我!
也游互助游论坛!

TOP

StackInterface.java

package com.zhida;

public interface StackInterface {
    public boolean isEmpty();
    public void popAll();
    public void push(String newItem)throws StackException;
    public String pop()throws StackException;
    public String peek()throws StackException;
}
到天涯海角等我!
也游互助游论坛!

TOP

StackArrayBased.java

package com.zhida;

public class StackArrayBased implements StackInterface{
    final int MAX_STACK=50;
    private String items[];
    private int top;
   
    public StackArrayBased(){
        items = new String[MAX_STACK];
        top=-1;
    }
    public boolean isEmpty(){
        return top<0;
    }
    public boolean isFull(){
        return top==MAX_STACK-1;
    }
   
    //进栈
    public void push(String newItem)throws StackException{
        if(!isFull()){
            items[++top] = newItem;
        }
        else{
            throw new StackException("StackException on push:stack full");
        }
    }
    public void popAll(){
        items = new String[MAX_STACK];
        top = -1;
    }
    public String pop() throws StackException{
        if(!isEmpty()){
            return items[top--];
        }
        else {
            throw new StackException("StackException on pop:stack empty");
        }
    }
   
    //出栈
    public String peek()throws StackException{
        if(!isEmpty()){
            return items[top];
        }
        else {
            throw new StackException("Stack exception on peek - stack empty");
        }
    }
}
到天涯海角等我!
也游互助游论坛!

TOP

呵呵,
i  need a domain now

TOP

看不懂 看了标题没看清  还以为是英语的

TOP

不好意思,是计算机的
到天涯海角等我!
也游互助游论坛!

TOP

呵呵
看不懂这个程序。。。
感谢LZ分享
脚踏实地努力

相遇,相识,相知,不知道接下来是开始还是结束......

TOP

返回顶部