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;
}
}