import scala.collection.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class JavaAnaylsis {
private static String[] keyword =
{"boolean", "byte", "char", "double", "false", "float", "int", "long",
"new", "null", "short", "true", "void", "instanceof", "break",
"case", "catch", "continue", "default", "do", "else", "for", "if",
"return", "switch", "try", "while", "finally", "throw", "this",
"super", "abstract", "final", "namtive", "private", "protected",
"public", "static", "synchronized", "transient", "volatile",
"class", "extends", "implements", "interface", "package", "import",
"throws"};
static Vector vc;
public JavaAnaylsis(String str) throws Exception {
}
/*
* 判断读入的字符是否为字母
*/
public static boolean isLetter(char c) {
if ((c >= 'a' && c <= 'z') || (c > +'A' && c <= 'Z')) {
return true;
} else
return false;
}
/*
* 判断读入的字符是否为数字
*/
public static boolean isDigit(char c) {
if (c >= '0' && c <= '9') {
return true;
} else
return false;
}
/*
* 判断是否为关键字
*/
public static boolean isKey(String ss) {
int flag = 0;
for (int i = 0; i < keyword.length; i++) {
if (ss.equals(keyword[i])) {
flag = 1;
break;
}
}
if (flag == 1) {
return true;
}
return false;
}
/*
* 判断是否为运算符
*/
public static boolean isSpilt(char ch) {
if (ch == '+' || ch == '-' || ch == '|' || ch == '=' || ch == '&') {
return true;
} else
return false;
}
/*
* 判断输入的字符并输出单词符号
*/
public static Vector judgement(String string) throws Exception {
String wordString;
Vector vc = new Vector();
char ch = 0;
int m = 0;
String str = null;
for (int i = 0; i < string.length(); i++) {
switch (m) {
case 0:
ch = string.charAt(i);
if(ch == '\''){
str = "";
m = 10;
}
// 判断是否为运算符使用超前搜索
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/'
|| ch == '=' || ch == '>' || ch == '<' || ch == '!'
|| ch == '%' || ch == '|') {
str = "";
str += ch;
if (ch == '+' || ch == '-' || ch == '>' || ch == '<'
|| ch == '!' || ch == '|' || ch == '&') {
ch = string.charAt(i + 1); // 对下一个字符进行判断是否为运算符
if (isSpilt(ch)) {
str += ch;
m = 4;
} else {
ch = string.charAt(i - 1); // 不是运算符则进行回退操作
m = 4;
}
}
}
// 判断是否为界符
else if (ch == ',' || ch == ';' || ch == '{' || ch == '}'
|| ch == '(' || ch == ')') {
str = "";
str+=ch;
m = 5;
}
// 判断是否数字
else if (isDigit((ch = string.charAt(i)))) {
str = "";
str += ch;
m = 3;
}
// 判断是否字母
else if (isLetter(ch = string.charAt(i))) {
str = "";
str += ch;
m = 2;
}
// 判断是否下划线或美元符号
else if (ch == '_' || ch == '$') {
str = "";
str += ch;
m = 2;
} else {
}
break;
case 4:
i--;
wordString = str+"\0044";
vc.add(wordString);
m = 0;
break;
case 5:
i--;
wordString = str+"\0045";
vc.add(wordString);
m = 0;
break;
case 7:
i--;
wordString = str+"\0047";
vc.add(wordString);
m = 0;
break;
case 2:
if (isLetter(ch = string.charAt(i))||isDigit(ch = string.charAt(i))) {
str += ch;
} else {
if (isKey(str)) {
wordString = str+"\0041";
vc.add(wordString);
} else {
wordString = str+"\0042";
vc.add(wordString);
}
i--;
m = 0;
}
break;
case 3:
if (isDigit((ch = string.charAt(i)))) {
str += ch;
} else {
wordString = str+"\0043";
vc.add(wordString);
i--;
m = 0;
}
break;
case 10:
ch = string.charAt(i);
if(ch == '\''){
wordString = str+"\00410";
vc.add(wordString);
m=0;
}else {
str += ch;
}
break;
}
}
return vc;
}
public static void main(String[] args) throws Exception {
String a = "a123('DSS','222a+',3)";
JavaAnaylsis javaAnaylsis = new JavaAnaylsis(a);
Vector judgement = JavaAnaylsis.judgement(a);
System.out.println(judgement);
}
}
JAVA自动解析方法名与参数值
最新推荐文章于 2025-03-30 19:50:26 发布