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("請(qǐng)輸入字符串:");
??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+"?"+"該字符為運(yùn)算符"+"???");
???}
??}
????}
??}
??
??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+"?"+"該字符為數(shù)字常量"+"???");
???}
?????}
??}
}
??
??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+"?"+"該單詞為關(guān)鍵字"+"???");
// 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+"?"+"該單詞為標(biāo)識(shí)符"+"???");??
????????break;
??}
??????
?}?
??
?
??return?0;?
??}
?
}各位大神好,上面是我的代碼,出了點(diǎn)問(wèn)題:當(dāng)我輸入表達(dá)式"if a+n=9 then a=0",程序應(yīng)該輸出{if str1 19 該單詞為關(guān)鍵字 ??a ?該單詞為標(biāo)識(shí)符+ ch2 ?0 該字符為運(yùn)算符 ?n ? 該單詞為標(biāo)識(shí)符= ch3 ?0 該字符為界符 ?9 ch4 8 該字符為數(shù)字常量 ??then 該單詞為標(biāo)識(shí)符a ?該單詞為標(biāo)識(shí)符= ch3 ?0 該字符為界符 ?0 ch4 9 該字符為數(shù)字常量},但是,運(yùn)行以后卻輸出這樣的:{if str1 0 該單詞為關(guān)鍵字 ??if str1 0 該單詞為關(guān)鍵字 ??+ ch2 ?0 該字符為運(yùn)算符 ??= ch3 ?0 該字符為界符 ?9 ch4 8 該字符為數(shù)字常量 ??if str1 0 該單詞為關(guān)鍵字 ??if str1 0 該單詞為關(guān)鍵字 ??= ch3 ?0 該字符為界符 ?0 ch4 9 該字符為數(shù)字常量},麻煩大神們幫我看看,謝謝了!? ?
添加回答
舉報(bào)
0/150
提交
取消