我点击按钮进行LR(1)语法分析
第一次:从输入框(JTextArea 定义的input)中输入i+i#
  分析:正确输出在输出框(JTextArea 定义output)
第二次:将输入和输出框setText("");
      在input中输入:i#
  问题:在output框中居然还有第一次的结果,再加上第二次的结果
  
  请问:是不是内存缓冲区的内容没有被清除
        如何解决?????
 如果是第二次是在第一次分析后,退出程序后再启动程序时执行的话 就没有以上的问题!
   各位 如何解决呢???????

解决方案 »

  1.   

    //ByTest.java
    //主控程序
    ......
    if(e.getActionCommand()=="LR(1)分析")
    {
    if(LRscan.check()==0){
     LRscan.LR_scan();
    }
     
    else{
    this.showT.setText("");
    this.outputT.setText("");
    }

    }
    ......//LR_Scan.java
    //具体方法
    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    public class LR_Scan {
    private ByTest bytest;
    final static String one="E->E+T";
    final static String two="E->T";
    final static String three="T->T*F";
    final static String four="T->F";
    final static String five="F->(E)";
    final static String six="F->i";
    final static String in="移进";
    String action=null;
    int add=1;//记录分析步骤
    ArrayList inputString=new ArrayList();
    //构造函数指定控制
    public LR_Scan(ByTest test)
    {
      bytest=test;
    }

    //LR分析表13行9列
    //0--11表示状态结点,21--26表示规约标号,-1表示error(出错),12表示acc(接受)
    int LRtable[][] = {{ 5,-1,-1, 4,-1,-1, 1, 2, 3},
    {-1, 6,-1,-1,-1,12,-1,-1,-1},
    {-1,22, 7,-1,22,22,-1,-1,-1},
    {-1,24,24,-1,24,24,-1,-1,-1},
    { 5,-1,-1, 4,-1,-1, 8, 2, 3},
    {-1,26,26,-1,26,26,-1,-1,-1},
    { 5,-1,-1, 4,-1,-1,-1, 9, 3},
    { 5,-1,-1, 4,-1,-1,-1,-1,10},
    {-1, 6,-1,-1,11,-1,-1,-1,-1},
    {-1,21, 7,-1,21,21,-1,-1,-1},
    {-1,23,23,-1,23,23,-1,-1,-1},
    {-1,25,25,-1,25,25,-1,-1,-1}};
    public void showLRtabel()
    {
    bytest.outputT.setText("");
        bytest.outputT.append('\n'+"    "+"SLR(1)分析表 :\n" +
         "                         "+"----0--11表示状态结点,\n" +
         "                         "+"     21--26表示规约标号,\n" +
         "                         "+"     -1表示error(出错),\n" +
         "                         "+"     12表示acc(接受)。"+'\n'+'\n');
        for(int i=0;i<index_char.length;i++)
         bytest.outputT.append("  "+index_char[i]+'\t');
        
        bytest.outputT.append("\n=====================================================" +
      "====================================================="+'\n');
    for(int i=0;i<13;i++)
    { for(int j=0;j<9;j++)
    {
    bytest.outputT.append("  "+LRtable[i][j]+'\t');
    }
        bytest.outputT.append('\n'+"-------------------------------------------------"+
         "--------------------------------------------------------------------"+
         "--------------------------------------------------------------------"+'\n');
    }
    }
    //规约规则的类定义
        class Rule{
         public char x;
         public int y;
            Rule(char x,int y)
            {
             this.x=x;
             this.y=y;
            };
        }
    //规则类数组的定义
        //"1"是产生试右边只有1字符,"3"是产生试右边只有3字符
        private Rule  ru[]=new Rule[]{new Rule('E',3),new Rule('E',1),
         new Rule('T',3),new Rule('T',1),new Rule('F',3),new Rule('F',1)}; 
     
       //输入字符
        String index_char[]={"i","+","*","(",")","#","E","T","F"};    //获取index_char[]中元素的位置
        int get_index_char(String i)
        {
         for(int j=0;j<9;j++)
         {
         if(index_char[j].equals(i))
         return j;
         }
         return -1;
        }

        //取得输入串
        private String get_input(int n)//将输入串分离,得到各个字符
    {
         System.gc();
    char c=0;
    String s=null;
    char memb[]=bytest.showT.getText().toString().toCharArray();
    for(int i=0;i<bytest.showT.getText().toCharArray().length;i++)
    { c=memb[i];  
    inputString.add(String.valueOf(c));
    }
    s=inputString.get(n).toString();
    return s;
    }
        
        //状态栈类的定义
        class status_stack extends Stack{}
        status_stack status=new status_stack();
        //遍历状态栈
        private void out_status_stack()
        {
         if(status.isEmpty())
         {
         bytest.outputT.append("状态栈空!");
         }
         else{
         Enumeration items=status.elements();
         while(items.hasMoreElements())
         bytest.outputT.append(items.nextElement()+"");
         }
        }
        //符号栈类的定义
        class char_stack extends Stack{}
        char_stack charstack=new char_stack();
      //将输入串栈压入符号栈
        private void char_push()
        {
         for(int i=0;i<bytest.showT.getText().toCharArray().length;i++)
         {  
         int j=1;
         charstack.push(get_input(j));
         }
        }
      //遍历符号栈
        private void out_char_stack()
        {
         if(charstack.isEmpty())
         {
         bytest.outputT.append("符号栈空!");
         }
         else{
         Enumeration items=charstack.elements();
         while(items.hasMoreElements())
         bytest.outputT.append(items.nextElement()+"");
         }
        }
        //输入栈的定义
        class input_stack extends Stack{}
        input_stack inputstack=new input_stack();
        //自顶向下遍历输入栈
        private void out_input_stack()
        {
         if(inputstack.isEmpty())
    bytest.outputT.append("输入栈栈空!");
    else
    {
    for(int i=inputstack.size()-1;i>=0;i--)
       bytest.outputT.append(inputstack.elementAt(i)+"");
    }
        }
        
            
    }
      

  2.   

    接上......
     if(inputstack.isEmpty()) 
    bytest.outputT.append("输入栈栈空!"); 
    else 

    for(int i=inputstack.size()-1;i>=0;i--) 
      bytest.outputT.append(inputstack.elementAt(i)+""); 

        } 
    //在这里的代码:从“//输出函数”到结尾
    }
     //输出函数
        private void print(int i,String act)
        {
         String s="                     ";
         bytest.outputT.append(i+s);
         out_status_stack();
         bytest.outputT.append(s);
         out_char_stack();
         bytest.outputT.append(s);
         out_input_stack();
         bytest.outputT.append(s);
         bytest.outputT.append(act);
        }
        
      //状态转换函数
        private int GOTO()
        {
         String x;
         int  y,z;
         x=inputstack.peek().toString();
         y=Integer.parseInt(status.peek().toString());
         z=get_index_char(x);
         return LRtable[y][z];
        }
        
      //移进--规约函数
      private void Action()
        {
         int i,j,x;
         String s;
         i= GOTO();
        
         //规约出错
         if(i==-1)
         bytest.outputT.append("\n\n************************** 规约出错! ****************************\n");
         //规约成功
         if(i==12)
         bytest.outputT.append("\n************************** 规约成功! ****************************\n");
         //移进动作
         if(i>=0&&i<=11)
         {
         add++;
         status.push(i);
         s=inputstack.pop().toString();
         charstack.push(s);
         bytest.outputT.append('\n'+"");
         print(add,action);
         action=in;
         Action();
         }
         //规约动作
         if(i>=21&&i<=26)
         {
         x = ru[i-21].y;
         for(j=0;j<x;j++)
         {
         status.pop();
         charstack.pop();
         }
         inputstack.push(ru[i-21].x);
         if(ru[i-21].x=='E'&&ru[i-21].y==3)
             action=one;
             else if(ru[i-21].x=='E'&&ru[i-21].y==1)
             action=two;
             if(ru[i-21].x=='T'&&ru[i-21].y==3)
             action=three;
             else if(ru[i-21].x=='T'&&ru[i-21].y==1)
             action=four;
             if(ru[i-21].x=='F'&&ru[i-21].y==3)
             action=five;
             else if(ru[i-21].x=='F'&&ru[i-21].y==1)
             action=six;
         Action();     }
        }
        
        //LR_scan函数进行对文法的LR(1)分析
        public int LR_scan()
        {
         int k=1;
         String getchar;
         //压进栈初始元素
         status.push("0");
         charstack.push("#");
         //先将输入串压进符号栈
         for(int j=1;j<=bytest.showT.getText().toCharArray().length;j++){
    getchar=get_input(j);
        if(getchar!="#"){
         //if(getchar.equals("#"))
         //break;
         charstack.push(getchar);
        }     
        else{
         break;
        }
    }
        
         //然后由符号栈弹出,压进输入串栈
         while(charstack.size()!=1)//符号栈留下"#"
         {
             getchar=charstack.pop().toString();
         inputstack.push(getchar);
         }
         //打印初始分析表
         print(k,action);  
         //移进,规约,并打印每一步分析过程
         Action();
         return 0;
        
        }
        //检查输入串是否正确
        public int check()
        {
         int i;
         String getchar;
         i=bytest.showT.getText().toCharArray().length-1;
         getchar=get_input(i);
        if(getchar.equals("#"))
        {
         i=0;
         bytest.outputT.setText("");
    bytest.outputT.append("..............................  分析  .." +
    ".........................................................."+'\n'
    +"..............\n");
    bytest.outputT.append("              "+" 输入串:"+bytest.showT.getText()+'\n');
    bytest.outputT.append("               "+"合法的输入串!"+'\n'+'\n');
    bytest.outputT.append("步骤             状态栈             符号栈             输入串             动作");
    bytest.outputT.append("\n*****************************************************************\n");
        
        }   
        else if(getchar.equals("")){
         JOptionPane.showMessageDialog(null, "当前输入区空,请输入!",
    "提示--输入区空",JOptionPane.PLAIN_MESSAGE);  
         i=1;
        }
        else{
         JOptionPane.showMessageDialog(null, "你输入的串不符合分析规则,请输入以 # 结束的串!",
    "提示--输入串不符合规范",JOptionPane.PLAIN_MESSAGE);  
         i=1;
        }
        return i;
        }