package 第二章; import java.util.Scanner; public class 词法分析1 { public static char[] ch1= new char[100]; public static char[] ch=new char[20]; public static int[] c=new int[20]; public static String[] b; public static String str; public static String s; public static int count=0; String[]str1={"abstract","boolean","break","base","case","catch", "char","class","continue","default","do","double", "else","extends","false","final","finally","float", "for","if","implements","import","instanceof","int", "interface","long","native","new","null","package", "private","protected","public","return","short", "static", "super","switch","synchronized","this","throw","throws", "transient","try","true","void", "volatile","while"}; char[]ch2={'+','.','-','*','/','%','$','<','>'}; char[]ch3={'=',';',',',':','(',')','[',']','{','}'}; char[]ch4={'1','2','3','4','5','6','7','8','9','0'}; char[]ch5={'A','B','C','D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q','R','S','T','U','V', 'W','X','Y','Z','a','b','c','d','e','f','g', 'h','i','j','k','l','m','n','o','p','q','r', 's','t','u','v','w','x','y','z','_'}; public static void main(String[] args) { System.out.println("请输入字符串:"); Scanner sc = new Scanner(System.in); 词法分析1 strin = new 词法分析1(); str = sc.nextLine(); b=str.split(" "); for(int p=0;p<b.length;p++){ s=b[p]; ch1=s.toCharArray(); strin.Cstr1(); strin.Cch2(); strin.Cch3(); strin.Cch4(); } } public void Cch2() { for (int i = 0; i < ch1.length; i++) { for (int j = 0; j < ch2.length;j++) { if(ch1[i]==ch2[j]) { System.out.println(ch1[i] + " "+"ch2 "+" "+j+" "+"该字符为运算符"+" "); } } } } public void Cch3() { for (int m = 0; m < ch1.length; m++) { for (int n = 0; n < ch3.length; n++) { if(ch1[m]==ch3[n]) { System.out.println(ch1[m] + " "+"ch3 "+" "+n+" "+"该字符为界符"+" "); } } } } public void Cch4() { for (int k = 0; k < ch1.length; k++) { for (int l = 0; l < 10; l++) { if(ch1[k]==ch4[l]) { System.out.println(ch1[k] + " "+"ch4"+" "+l+" "+"该字符为数字常量"+" "); } } } } public boolean Nch2() { int r=0; for(int m=0; m< ch1.length; m++) { // System.out.println(str.length()); for(int m2=0; m2<9; m2++) { if(ch1[m]==ch2[m2]) { return false; } if(ch1[m]!=ch2[m2]) { r++; if(r==( ch1.length)*(ch2.length)) { return true; } } } } return false; } public boolean Nch3() { int s=0; for(int n=0; n< ch1.length; n++) { for(int n2=0; n2<ch3.length; n2++) { if(ch1[n]==ch3[n2]) { return false; } if(ch1[n]!=ch3[n2]) { s++; if(s==( ch1.length)*(ch3.length)) { return true; } } } } return false; } public int Cstr1() { for (int k1= 0; k1 < b.length; k1++) { for(int k2=0;k2<str1.length;k2++) { if(b[k1].equals(str1[k2])) { System.out.println(str1[k2] + " "+"str1"+" "+k1+" "+"该单词为关键字"+" "); // break; return 0; } } } for(int kk=0;kk<ch5.length;kk++) { // System.out.println(ch5[kk]); if(ch1[0]==ch5[kk]&&Nch2()&&Nch3()) { System.out.println(str+" "+"该单词为标识符"+" "); break; } } return 0; } }
各位大神好,上面是我的代码,出了点问题:当我输入表达式"if a+n=9 then a=0",程序应该输出
{if str1 19 该单词为关键字
a 该单词为标识符
+ ch2 0 该字符为运算符
n 该单词为标识符
= ch3 0 该字符为界符
9 ch4 8 该字符为数字常量
then 该单词为标识符
a 该单词为标识符
= ch3 0 该字符为界符
0 ch4 9 该字符为数字常量},但是,运行以后却输出这样的:
{if str1 0 该单词为关键字
if str1 0 该单词为关键字
+ ch2 0 该字符为运算符
= ch3 0 该字符为界符
9 ch4 8 该字符为数字常量
if str1 0 该单词为关键字
if str1 0 该单词为关键字
= ch3 0 该字符为界符
0 ch4 9 该字符为数字常量},麻烦大神们帮我看看,谢谢了!
相关分类