0916 词法分析(2)

 

 

 

 

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')
   {
    flag=1;
    i++;
    sphrase+=cword;
    if(i<scode.length()){
    char[] cnextTemp=scode.substring(i,i+1).toCharArray();
    cword=cnextTemp[0];
    }else{
     break;
    }
   }
   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(String ssign :arrayphrase){
   switch (ssign){
   case "begin":
    System.out.print("<1 "+ssign+">");
    break;
   case "end":
    System.out.print("<2 "+ssign+">");
    break;
   case "if":
    System.out.print("<3 "+ssign+">");
    break;
   case "then":
    System.out.print("<4 "+ssign+">");
    break;
   case "else":
    System.out.print("<5 "+ssign+">");
    break;
   case "while":
    System.out.print("<6 "+ssign+">");
    break;
   case "do":
    System.out.print("<7 "+ssign+">");
    break;
   case "+":
    System.out.print("<13 "+ssign+">");
    break;
   case "-":
    System.out.print("<14 "+ssign+">");
    break;
   case "*":
    System.out.print("<15 "+ssign+">");
    break;
   case "/":
    System.out.print("<16 "+ssign+">");
    break;
   case "<=":
    System.out.print("<17 "+ssign+">");
    break;
   case "<>":
    System.out.print("<18 "+ssign+">");
    break;
   case "<":
    System.out.print("<19 "+ssign+">");
    break;
   case ":":
    System.out.print("<21 "+ssign+">");
    break;
   case ":=":
    System.out.print("<22 "+ssign+">");
    break;
   case ";":
    System.out.print("<23 "+ssign+">");
    break;
   default:
   int flagisn=1;
   char[] cssigntemp=ssign.toCharArray();
    for(int k=0;k<ssign.length();k++){
     if(Character.isDigit(cssigntemp[k])!=true){
      flagisn=0;
     }
    }
    if(flagisn==1){
     System.out.print("<11 "+ssign+">");
    }else{
     System.out.printf("<10"+ssign+">");
    }
  }  
 }
   }
}

posted @ 2015-09-24 09:04  44.林集团  阅读(144)  评论(0编辑  收藏  举报