码迷,mamicode.com
首页 > 其他好文 > 详细

0916 词法分析(3)

时间:2015-09-28 15:54:56      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

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>();
  int i;
  for(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==‘=‘)
    {
     sphrase+=cword;
    }
    if(i<scode.length()&&cword==‘>‘)
    {
     sphrase+=cword;
    }
    arrayphrase.add(sphrase);
    sphrase="";
   }
  if(i<scode.length()&&cword==‘:‘)
  {
   sphrase+=cword;
   char[] cnextTemp=scode.substring(i+1,i+2).toCharArray();
   cword=cnextTemp[0];
   if(i<scode.length()&&i<scode.length()&&cword==‘=‘)
   {
    i++;
    sphrase+=cword;
   }
   arrayphrase.add(sphrase);
   sphrase="";
  }
  if(i<scode.length()&&cword==‘+‘)
  {
   sphrase+=cword;
   arrayphrase.add(sphrase);
   sphrase="";
  }
  if(i<scode.length()&&cword==‘-‘)
  {
   sphrase+=cword;
   arrayphrase.add(sphrase);
   sphrase="";
  }
  if(i<scode.length()&&cword==‘*‘)
  {
   sphrase+=cword;
   arrayphrase.add(sphrase);
   sphrase="";
  }
  if(i<scode.length()&&cword==‘/‘)
  {
   sphrase+=cword;
   arrayphrase.add(sphrase);
   sphrase="";
  }
  if(i<scode.length()&&cword==‘;‘)
  {
   sphrase+=cword;
   arrayphrase.add(sphrase);
   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+">");
    }
  }  
 }
   }
}

0916 词法分析(3)

标签:

原文地址:http://www.cnblogs.com/linjituan/p/4843968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!