| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1603 人关注过本帖
标题:用java写的编译原理课中的词法分析程序有错误不知道怎么改,求救~~
取消只看楼主 加入收藏
koalaylj
Rank: 1
来 自:北京
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-9-29
收藏
 问题点数:0 回复次数:1 
用java写的编译原理课中的词法分析程序有错误不知道怎么改,求救~~
/*词法分析程序,待分析的程序(下面复出)是pascal语言,分析到"."即分析结束*/
import *;
public class A
{
  public static void main(String args[])
  {
        StringBuffer str=new StringBuffer();
        
        Getsym getsymbol=new Getsym();
        
        Inout savefile=new Inout();
        
        Init inits=new Init();
        
        System.out.println("~~~~词法分析开始~~~~~");
    
        inits.init();   
        
        int j=0;
        
        do
        {   
            j=getsymbol.getsym();
        }while(j!=-1);
        
        savefile.save(str);
        
        System.out.println("~~分析结束,谢谢使用!~~");            
    }
}

enum symbol       //所有符号
{      
    nul,        ident,        number,         plus,         minus,
    times,      slash,        oddsym,         eql,          neq,
    lss,        leq,          gtr,            geq,          lparen,
    rparen,     comma,        semicolon,      period,       becomes,
    beginsym,   endsym,       ifsym,          thensym,      whilesym,
    writesym,   readsym,      dosym,          callsym,      constsym,
    varsym,     procsym,      lbracket,       rbracket        
}

class Init        //初始化符号表
{
    private symbol sym;
    
    private symbol ssym[]=new symbol[256];
    
    private symbol wsym[]=new symbol[13];
    
    private String word[]=new String[13];

    public symbol getssym(char ch)
    {
        return ssym[ch];
    }    
    public symbol getwsym(int i)
    {
        return wsym[i];
    }
    public String getword(int i)
    {
        return word[i];        
    }    
    
    public void init()
    {
        for (int i=0; i<=255; i++)
        {
            ssym[i] = null;
        }

        ssym['+'] = symbol.plus;
        ssym['-'] = symbol.minus;
        ssym['*'] = symbol.times;
        ssym['/'] = symbol.slash;
        ssym['('] = symbol.lparen;
        ssym[')'] = symbol.rparen;            
        ssym['='] = symbol.eql;
        ssym[','] =             
        ssym['.'] = symbol.period;
        ssym['#'] = symbol.neq;
        ssym[';'] = symbol.semicolon;                
        ssym['{'] = symbol.lbracket;
        ssym['}'] = symbol.rbracket;

        word[1]="call";
        word[2]="const";
        word[3]= "do";
        word[4]="end";
        word[5]="if";
        word[6]="odd";
        word[7]="procedure";
        word[8]="read";
        word[9]="then";
        word[10]="var";
        word[11]="while";
        word[12]="write";
    
        wsym [0] = symbol.beginsym;
        wsym [1] = symbol.callsym;
        wsym [2] = symbol.constsym;
        wsym [3] = symbol.dosym;
        wsym [4] = symbol.endsym;
        wsym [5] = symbol.ifsym;
        wsym [6] = symbol.oddsym;
        wsym [7] = symbol.procsym;
        wsym [8] = symbol.readsym;
        wsym [9] = symbol.thensym;
        wsym [10] = symbol.varsym;
        wsym [11] = symbol.whilesym;
        wsym [12] = symbol.writesym;
    }        
}

class Getsym   //取单词或符号
{    
    private int eof=-1;
    
    final int al=10;    // the largest length of symbol
    
    final int norw=13;   //keyword numbers
    
    final int nmax=14;   //the largest length of number
    
    private char ch=' ';
    
    private int j,i,k;
    
    private int xx=0;
    
    private String id;
    
    symbol sym;
        
    Inout read=new Inout();
    
    char a[]=new char[al+1];
    
    public int getsym()  //取单词
    {
            
        while(ch==' '||ch==10||ch==9)              
        {
            ch=read.getch();
        }
            if(ch>='a'&&ch<='z')                       
            {
                k=0;
                
                do
                {
                    if(k<al)   
                    {
                        a[k]=ch;
                        
                        k++;
                    }
                    
                    ch=read.getch();
                
                }while(ch>='a' && ch<='z' || ch>='0' && ch<='9');
                
                id=new String(a);
    
                for(i=0;i<=10;i++)
                {
                    a[i]=' ';
                }
                
                for(i=0;i<13;i++)
                {  
                    if(id.trim().compareTo(inits.getword(i))==0)
                    {  
                        j=i;
                        
                        break;
                    }                
                    else
                        j=13;
                }
                
                
                System.out.print(" ( ");
                
                //str.append(" < ");                        
                
                if(j>=0&&j<=12)
                {
                    sym=inits.getwsym(j);
                    
                    System.out.print(sym);
                   
                    System.out.print(" , ");
                    
                    System.out.print(id);
                    
                }
                else                                        
                {
                    if(String.valueOf(eof)==id)
                        xx=-1;
                    else
                    {
                        sym=symbol.ident;
                        
                        System.out.print(sym);
                        
                        System.out.print(" , ");
                        
                        System.out.print(id);
                        
                    }
                }
            
                str.append(sym);
                //str.append( , );
                str.append(id);
                //str.append( >\n);
                
    
            }
            else
            {
            
                System.out.print("( ");
                
                //str.append( < );

                if(ch>='0'&&ch<='9')                             
                {
                    k=0;
                    
                    int num=0;
                    
                    sym=symbol.number;
                    
                    do
                    {
                        num=10*num+ch-'0';
                        
                        k++;
                        
                        ch=read.getch();
                        
                    }while(ch>='0' && ch<='9');
                    
                    System.out.print(sym);
                    
                    System.out.print(" , ");
                    
                    System.out.print(num);
                    
                    //str.append(sym+" , "+String.valueOf(num));
                    
                    str.append(sym);
                    //str.append( , );
                    str.append(num);
                }
                else
                {
                    if(ch==':')  
                    {
                        ch=read.getch();
                        
                        if(ch=='=')
                        {
                            sym=symbol.becomes;
                            
                            System.out.print(sym);
                            
                            ch=read.getch();
                        }
                        else
                        {
                            sym=symbol.nul;
                            
                            System.out.print(sym);
                            
                        }
                    }
                    else
                    {
                        if(ch=='<')  
                        {
                            ch=read.getch();
                            
                            if(ch=='=')
                            {
                                sym=symbol.leq;
                                
                                System.out.print(sym);                        
                                
                                ch=read.getch();
                            }
                            else
                            {
                                sym=symbol.lss;
                                
                                System.out.print(sym);
                                
                            }
                        }
                        else
                        {
                            if(ch=='>')
                            {
                                ch=read.getch();
                                
                                if(ch=='=')
                                {
                                    sym=symbol.geq;
                                    
                                    System.out.print(sym);
                                    
                                    
                                    ch=read.getch();
                                }
                                else   
                                {
                                    sym=symbol.gtr;
                                    
                                    System.out.print(sym);
                                }
                            }
                            else
                            {
                                sym=inits.getssym(ch);

                                if(sym!=symbol.period)
                                {
                                   ch=read.getch();
                                }
                                else
                                    xx=-1;
                                    
                                System.out.print(sym);//end richard
                                
                                str.append(sym);
                            }
                        }
                    }
                    
                    System.out.print(" , null");
                    
                    str.append( null);
                    
                    //str.append( , null);
                }
            }
            
            System.out.println(" )");
            
            //str.append( >\n);
                
            return xx;
        }
    
}
class Inout        //读写文件
{
    private int length=0;
    
    private long pos=0;
    
    private char line[]=new char[81];
    
    private int cc=0;
    
    private char ch;
    
    RandomAccessFile result=null;
    
    RandomAccessFile readfile=null;
        
    public void readline()   //读行
    {
        File file=new File("E:\\java","yuan.txt");
        try  
        {   
            readfile=new RandomAccessFile(file,"r");
                
            String s=null;
                
            readfile.seek(pos);
          
            if((s=readfile.readLine().trim())!=null)
            {
                s.getChars(0,s.length(),line,0);
                
                length=s.length();
                
                pos=readfile.getFilePointer();   
            }
            
            readfile.close();            
        }
        catch (IOException e)
        {
          System.out.println(e);
        }
    }

    public char getch()  //读一个字符
    {     
        if(cc==length)
        {
            readline();
            
            cc=0;
        }
        
        ch=line[cc];
        
        cc++;
        
        return ch;        
    }
        
    public void save(String string)    //保存文件
    {
        
        try
        {
            result=new RandomAccessFile("result.txt","rw");
        
            result.writeChars(string);

            result.close();
                
        }catch(IOException e)
        {
            System.out.println(e);        
        }        
    }
}




/*待分析的程序*/
int gcd (int u, int v)
{
if (v := 0) return u ;
if(v<=0) return u;
else return gcd(v,u-u/v*v)
}
.
搜索更多相关主题的帖子: 词法分析程序 java 原理 new 编译 
2008-06-12 22:39
koalaylj
Rank: 1
来 自:北京
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-9-29
收藏
得分:0 
回复 2# freish 的帖子
我是大连海事大学的,多多指教~~~
2008-06-13 08:57
快速回复:用java写的编译原理课中的词法分析程序有错误不知道怎么改,求救~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024816 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved