欢迎来到天天文库
浏览记录
ID:55037679
大小:19.00 KB
页数:3页
时间:2020-04-26
《用JAVA写词法分析器.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjava.lang.*;importjava.io.*;publicclassWordanalsys{staticcharch;staticintindex=0;staticintj=0;staticcharallstr[]=newchar[];staticStringname;publiccharGetchar(){try{ch=(char)System.in.read();}catch(Exceptione){e.printStackTrace();}returnch;}publicbooleanIsLet
2、ter(charch){returnjava.lang.Character.isLetter(ch);}publicbooleanIsDigit(charch){returnjava.lang.Character.isDigit(ch);}publicintReserve(){KeyTabletable[]=newKeyTable[5];table[0]=newKeyTable("while",1);table[1]=newKeyTable("for",2);table[2]=newKeyTable("if",3);tab
3、le[3]=newKeyTable("else",4);table[4]=newKeyTable("int",5);name=newString(allstr).substring(j,index);//取子串for(inti=0;i<5;i++)if(name.equals(table[i].key))returntable[i].code;return0;}publicstaticvoidmain(String[]args){Wordanalsysa=newWordanalsys();ch=a.Getchar();in
4、tm=0;while(ch!='#'){allstr[index]=ch;index++;ch=a.Getchar();}intk=index;index=0;while(j5、6、a.IsDigit(allstr[index])){index++;}if(a.Reserve()!=0)System.out.println("保留字"+name+""+a.Reserve());elseSyste7、m.out.println("标识符"+"6");j=index;}if(a.IsDigit(allstr[j])){while(a.IsDigit(allstr[index]))index++;name=newString(allstr).substring(j,index);intb=java.lang.Integer.pasint(name);Stringname1=Integer.toBinaryString(b);System.out.println("数字"+name+"7"+name1);j=index;}i8、f(allstr[j]=='('){System.out.println("左括号"+"(8");index++;j=index;}if(allstr[j]=='='){System.out.println("等号"+"=9");index++;j=index;}if(allstr[j]==')'){System.out.println("右括号"+")10");index++;j=index;}if(allstr[j]==''){while(allstr[j]==''){index++;j=index;}}if(alls9、tr[j]==';'){System.out.println("分号"+";11");index++;j=index;}if(allstr[j]=='<'){index++;j=index;if(allstr[j]=='='){System.out.println("小于等于"+"<=12");index++;j=index;}elseSystem.out.println("小于"+"<13");}if(allstr[j]=='>'){index++;j=index;if(allstr[j]=='='){System.ou10、t.println("大于等于"+">=15");index++;j=index;}elseSystem.out.println("大于号"+">14");}if(allstr[j]=='+'){index++;j=index;if(allstr[j]=='+'){System.out.println(
5、
6、a.IsDigit(allstr[index])){index++;}if(a.Reserve()!=0)System.out.println("保留字"+name+""+a.Reserve());elseSyste
7、m.out.println("标识符"+"6");j=index;}if(a.IsDigit(allstr[j])){while(a.IsDigit(allstr[index]))index++;name=newString(allstr).substring(j,index);intb=java.lang.Integer.pasint(name);Stringname1=Integer.toBinaryString(b);System.out.println("数字"+name+"7"+name1);j=index;}i
8、f(allstr[j]=='('){System.out.println("左括号"+"(8");index++;j=index;}if(allstr[j]=='='){System.out.println("等号"+"=9");index++;j=index;}if(allstr[j]==')'){System.out.println("右括号"+")10");index++;j=index;}if(allstr[j]==''){while(allstr[j]==''){index++;j=index;}}if(alls
9、tr[j]==';'){System.out.println("分号"+";11");index++;j=index;}if(allstr[j]=='<'){index++;j=index;if(allstr[j]=='='){System.out.println("小于等于"+"<=12");index++;j=index;}elseSystem.out.println("小于"+"<13");}if(allstr[j]=='>'){index++;j=index;if(allstr[j]=='='){System.ou
10、t.println("大于等于"+">=15");index++;j=index;}elseSystem.out.println("大于号"+">14");}if(allstr[j]=='+'){index++;j=index;if(allstr[j]=='+'){System.out.println(
此文档下载收益归作者所有