java版词法分析程序

package com;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Wordanalsys {
   static int index=0;
   static int j=0;
   static char  allstr[]=new char[100000];
   static String name;
    public boolean IsLetter(char ch)
    {
      return java.lang.Character.isLetter(ch);
    }
    public boolean IsDigit(char ch)
    {
      return java.lang.Character.isDigit(ch); 
    }
   
    public int Reserve()
    {
     KeyTable table[]=new KeyTable[5];
     table[0]=new KeyTable("while",1);
     table[1]=new KeyTable("for",2);
     table[2]=new KeyTable("if",3);
     table[3]=new KeyTable("else",4);
     table[4]=new KeyTable("int",5);
        name=new String(allstr).substring(j,index);//取子串
     for(int i=0;i<5;i++)
      if(name.equals(table[i].key))
         return table[i].code;
     return 0;
     
    }
    public static void main (String[] args) throws  Exception, IOException
    {
        Wordanalsys a=new Wordanalsys();
        BufferedReader bf = new BufferedReader(new FileReader("E:\\a.txt"));
     String str = null;
        while(null!=(str=bf.readLine()))
        {
         
          allstr = str.toCharArray();
          int k=allstr.length;
          while(j<k-1)
          {
         int m=j;
         if(a.IsLetter(allstr[j]))
            {
            while(a.IsLetter(allstr[index])||a.IsDigit(allstr[index]))
            {
               index++;
            } 
            if(a.Reserve()!=0)
             System.out.println("保留字   "+name+"  "+a.Reserve());
               else
                System.out.println("标识符  "+name+"  6");
               j=index;
         
            }   
         if(a.IsDigit(allstr[j]))
            {
             while(a.IsDigit(allstr[index]))
           index++;
                 name=new String(allstr).substring(j,index);                  
                    System.out.println("数字   "+name+"  7");
                 j=index; 
         }
         if(allstr[j]=='(')
          {
              System.out.println("左括号 "+"(  8");
              index++;
              j=index;
             }
            if(allstr[j]=='=')
            {
                System.out.println(" 等号  "+"=   9");
                index++;
                j=index;
            }
            if(allstr[j]==')')
            {
             System.out.println("右括号 "+")   10");
             index++;
             j=index;
            }
            if(allstr[j]==' ')
            {
             while(allstr[j]==' ')
             {
              index++;
              j=index;
             }
            }
            if(allstr[j]==';')
            {
             System.out.println("分号"+" ; 11");
             index++;
             j=index;
            }
            if(allstr[j]=='[')
            {
             System.out.println("中括号");
             index++;
             j=index;
            }
            if(allstr[j]==']')
            {
             System.out.println("中括号");
             index++;
             j=index;
            }
            if(allstr[j]=='{')
            {
             System.out.println("大括号");
             index++;
             j=index;
            }
            if(allstr[j]=='.')
            {
             System.out.println("点");
             index++;
             j=index;
            }
            if(allstr[j]=='}')
            {
             System.out.println("大括号");
             index++;
             j=index;
            }
            if(allstr[j]=='<')
            {
             index++;
             j=index;
                if(allstr[j]=='=')
                {
                 System.out.println("小于等于"+" <=   12");
                 index++;
                 j=index;
                }
                else
             System.out.println("小于 "+"  <  13");
            } 
            if(allstr[j]=='>')
            {
             index++;
                j=index;
                if(allstr[j]=='=')
                {
                    System.out.println("大于等于"+"  >=   15");
                    index++;
                    j=index ;
                }                
                else System.out.println("大于号"+"  >  14" );
            }  
            if(allstr[j]=='+')
            {
             index++;
                j=index;
                if(allstr[j]=='+')
                {
                     System.out.println("自增运算符"+"  ++  16");
                     index++;
                     j=index;
                }
                else
                     System.out.println("加号"+"  +   17");
            }
            if(allstr[j]=='-')
            {
             index++;
                j=index;
                if(allstr[j]=='-')
                {
                     System.out.println("自减运算符"+"  --  16");
                     index++;
                     j=index;
                }
                else System.out.println("减号"+"  -   17");
            }
               
           if(allstr[j]=='\n')
           {
         System.out.println("enter   the   next  line");
         index++;
         j=index;
           }
           if(m==j)
           {
         System.out.println("非法字符");
         index++;
         j=index;
           }
          
        }
      }
    bf.close();
    }
}
class KeyTable
{
 String key;
 int code;
 public KeyTable(String key,int code)
 {
  this.key=key;
  this.code=code;
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值