import parser.Parser;import java.util.ArrayList; import java.util.Arrays; import java.util.List;public class Main {public static void main(String[] args) {// 关键词List<String> keyList = new ArrayList<>(Arrays.asList("int","String"));// 关键词数List<Integer> keyNum = new ArrayList<>(Arrays.asList(1,2));// 运算符和界符List<String> symbolList = new ArrayList<>(Arrays.asList("+","-"));// 运算符和界符的数List<Integer> symbolNum = new ArrayList<>(Arrays.asList(10,11));// 从文件取出的字符String letter;// 将字符转为单词String words;String test ="int main() { int i,j; String a,b;} 123 ccd";Parser parser = new Parser();parser.analysis(test,keyList);} }
package parser;import java.util.List;public class Parser {public void analysis(String test,List<String> keyList){System.out.println("test:" + test);int length = test.length();System.out.println("length:" + length);// 字符类型for(int num=0;num<length;){char ch = test.charAt(num);System.out.println("ch:" + ch);int typeword = typeword(ch); // System.out.println("typeword:" + typeword);String addword=Character.toString(ch);String[] rs;switch (typeword){case 1:rs= number(test,addword,num,keyList);addword = rs[0];num = Integer.parseInt(rs[1]);System.out.println("<"+addword+",1,int"+">");break;case 2:rs= identifier(test,addword,num,keyList);addword = rs[0];num = Integer.parseInt(rs[1]);System.out.println("<"+addword+",2,String"+">");break;case 3:rs= symbol(test,addword,num);addword = rs[0];num = Integer.parseInt(rs[1]);System.out.println("<"+addword+",3,symbol"+">");break;default:num++;break;}}}public int typeword(char str) {if (Character.isDigit(str)) {return 1;}if (Character.isLetter(str)) {return 2;}if (Character.toString(str).equals("+") || Character.toString(str).equals("-")) {return 3;}return 0;}public String[] identifier(String letter, String s, int n, List<String> keyList) {int j = n + 1;boolean flag = true;while (flag) {if (j >= letter.length()) {break;}if (isNumeric(letter.charAt(j)) || Character.isLetter(letter.charAt(j))) {s += letter.charAt(j);if (isKeyword(keyList, s)) {n = ++j;return new String[]{s, String.valueOf(n)};}j++;} else {flag = false;}}n = j;return new String[]{s, String.valueOf(n)};}public String[] symbol(String letter, String s, int n) {int j = n + 1;boolean flag = true;while (flag) {if (Character.toString(letter.charAt(j)).equals("+") || Character.toString(letter.charAt(j)).equals("-")) {s += letter.charAt(j);j++;} else {flag = false;}}n = j;return new String[]{s, String.valueOf(n)};}public String[] number(String letter, String s, int n, List<String> keyList) {int j = n + 1;boolean flag = true;while (flag) {if (isNumeric(letter.charAt(j))) {s += letter.charAt(j);j++;} else {flag = false;}}n = j;return new String[]{s, String.valueOf(n)};}public static boolean isKeyword(List<String> keyList, String str) {try {return keyList.contains(str);} catch (Exception e) {return false;}}public static boolean isNumeric(char str) {try {Double.parseDouble(Character.toString(str));return true;} catch (Exception e) {return false;}} }
测试结果: