0916词法分析
package compileWork;
import java.util.ArrayList;
import java.util.Scanner;
public class compileWork_1
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("please input code");
String scode=s.nextLine();
s.close();
ArrayList<String> arrayphrase=new ArrayList<String>();
for(int i=0;i<scode.length();i++)
{
char[] stemp=scode.substring(i, i+1).toCharArray();
char cword=stemp[0];
String sphrase="";
int flag=0;
while(i<scode.length()&&cword<='Z'&&cword>='A'||cword<='z'&&cword>='a'||cword>='0'&&cword<='9')
{
i++;
sphrase+=cword;
char[] cnextTemp=scode.substring(i,i+1).toCharArray();
cword=cnextTemp[0];
}
if(flag==1){
arrayphrase.add(sphrase);
sphrase="";
}
if(i<scode.length()&&cword=='<')
{
i++;
sphrase+=cword;
char[] cnextTemp=scode.substring(i,i+1).toCharArray();
cword=cnextTemp[0];
if(i<scode.length()&&i<scode.length()&&cword=='=')
{
i++;
sphrase+=cword;
}
if(i<scode.length()&&cword=='>')
{
i++;
sphrase+=cword;
}
arrayphrase.add(sphrase);
sphrase="";
}
if(i<scode.length()&&cword==':')
{
i++;
sphrase+=cword;
char[] cnextTemp=scode.substring(i,i+1).toCharArray();
cword=cnextTemp[0];
if(i<scode.length()&&i<scode.length()&&cword=='=')
{
i++;
sphrase+=cword;
}
arrayphrase.add(sphrase);
sphrase="";
}
if(i<scode.length()&&cword=='+')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()&&cword=='-')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()&&cword=='*')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()&&cword=='/')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()&&cword==':')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()){
sphrase+=cword;
arrayphrase.add(sphrase);
}
}
for(int j=0;j<6;j++)
{
System.out.print(arrayphrase.get(j));
}
}
}
老师好 因为能力不足只完成了一半 过段时间会补交