标签:
示例:
比如如下的代码段:
while(i>=j) i--
关键字表
package CompilePrograme; import java.awt.List; import java.util.Scanner; public class Compile { public static void main(String[] args) { // TODO Auto-generated method stub double startTime = System.currentTimeMillis(); final int MAX_LEN = 100; Scanner input = new Scanner(System.in); System.out.print("Please input a string <end with ‘#‘>:"); String uString = input.nextLine(); input.close(); String[] keyWords = new String[] { "begin", "if", "then", "while", "do", "end" }; char[] analyseData = new char[MAX_LEN]; int index = 0, key = 0; List list = new List(); do { String compareStr = null; char temp = uString.charAt(index); list = extactCharacters(temp, analyseData, keyWords, uString, index, compareStr); if (list.getItemCount() == 0) { index++; continue; } // 规定List的第一个元素为index,第二个元素为key index = Integer.parseInt(list.getItem(0)); key = Integer.parseInt(list.getItem(1)); String words = list.getItem(2); System.out.println("< " + key + " ," + words + " >"); } while (key != 0); double endTime = System.currentTimeMillis(); System.out.println("\nProgram running time is :" + (endTime - startTime)); } public static List extactCharacters(char temp, char[] analyseDate, String[] keywords, String uString, int index, String compareStr) { int keyID = -1, m = 0; // index--; List list = new List(); // 1.判断下一个读入的字符是否为空格,用while作为大循环,若读取到空格则跳出方法,提取下一个字符进行判断 while (temp != ‘ ‘) { // 2.判断当前字符是字母或者数字和字母的组合 if (temp >= ‘a‘ && temp <= ‘z‘) { m = 0; // 当读取到不是大小写字母或者数字时候判断为一个单词读取完成 while (temp >= ‘a‘ && temp <= ‘z‘ || temp >= ‘A‘ && temp <= ‘Z‘ || temp >= ‘0‘ && temp <= ‘9‘) { analyseDate[m++] = temp; compareStr += temp + ""; temp = uString.charAt(++index); } compareStr = compareStr.substring(4); // 与读取出来的字符判断是否为关键字 for (int i = 0; i < 6; i++) { if (compareStr.equals(keywords[i])) { keyID = i + 1; list.add(index + ""); list.add(keyID + ""); list.add(compareStr); return list; } } // 不是关键字就当作为标识符 keyID = 10; list.add(index + ""); list.add(keyID + ""); list.add(compareStr); return list; } // 3,判断当前字符是数字? else if (temp >= ‘0‘ && temp <= ‘9‘) { m = 0; String tempTokens = null; // 对后面的字符进行判断是否为数字 while (temp >= ‘0‘ && temp <= ‘9‘) { analyseDate[m++] = temp; tempTokens += temp; temp = uString.charAt(++index); } // 不是数字则返回种别码,结束当前方法 keyID = 11; tempTokens = tempTokens.substring(4); list.add(index + ""); list.add(keyID + ""); list.add(tempTokens + ""); return list; } m = 0; // 4.判断当前字符是其他关系运算符 String token = null; switch (temp) { case ‘<‘: // String token = null; analyseDate[m++] = temp; token += temp; if (uString.charAt(++index) == ‘=‘) { analyseDate[m++] = temp; keyID = 22; token += uString.charAt(index++); } else if (uString.charAt(++index) == ‘>‘) { analyseDate[m++] = temp; keyID = 21; token += uString.charAt(index++); } else { keyID = 23; } list.add(index + ""); list.add(keyID + ""); token = token.substring(4); list.add(token); return list; case ‘>‘: // String tokens = null; analyseDate[m++] = temp; token += temp; if (uString.charAt(++index) == ‘=‘) { keyID = 24; analyseDate[m++] = temp; token += uString.charAt(index++); } else { keyID = 20; } list.add(index + ""); list.add(keyID + ""); token = token.substring(4); list.add(token); return list; case ‘:‘: analyseDate[m++] = temp; token += temp; if (uString.charAt(++index) == ‘=‘) { keyID = 18; // analyseDate[m++] = temp; analyseDate[m++] = uString.charAt(index); token += uString.charAt(index++); } else { keyID = 17; } list.add(index + ""); list.add(keyID + ""); token = token.substring(4); list.add(token); return list; case ‘*‘: keyID = 13; break; case ‘/‘: keyID = 14; break; case ‘+‘: keyID = 15; break; case ‘-‘: keyID = 16; break; case ‘=‘: keyID = 25; break; case ‘;‘: keyID = 26; break; case ‘(‘: keyID = 27; break; case ‘)‘: keyID = 28; break; case ‘#‘: keyID = 0; break; default: keyID = -1; break; } analyseDate[m++] = temp; list.add(++index + ""); list.add(keyID + ""); list.add(temp + ""); return list; } return list; } }
运行结果
标签:
原文地址:http://www.cnblogs.com/jianzhihao/p/5924714.html