📄 analyzer.java
字号:
package try_sw;import java.util.*;import java.util.regex.*;import java.io.*;class FormatException extends Exception{ private String msg=""; public FormatException(String msg){ this.msg= msg; } public String toString(){ return "FormatException : "+msg; }}class lexicalError extends Exception{ private String msg=""; public lexicalError(String msg){ this.msg= msg; } public String toString(){ return "lexicalError : "+msg; }}public class analyzer { /** * @param args */ public Mystack analyze(Newhash hash,String contest){ Mystack sta = new Mystack(); // Mystack stb = new Mystack();//what is that for? Pattern pn = Pattern.compile("([a-zA-Z0-9]|[\\.])+");//number,variable,illegic variable Pattern pn1= Pattern.compile("[a-zA-Z]([a-zA-Z0-9]*)");//variable Pattern pn2= Pattern.compile("([0]|([1-9]([0-9]*)))[\\.]([0-9]+)|[0]|([1-9]([0-9]*))");//number Pattern pn3= Pattern.compile("[^\\.a-zA-Z0-9][^\\.a-zA-Z0-9]");//两个非词 Pattern pn4= Pattern.compile("[^\\.a-zA-Z0-9]");//a non-word Pattern pn5= Pattern.compile("/\\*.+\\*/");/////////////innotation Matcher m6 = pn5.matcher(contest); while(m6.find())contest=m6.replaceAll(""); ////////////innotation String[] test = contest.split("\\s+"); //非字母字符:先查两再查一 for(String str : test){ String work = str; while(work.length()>0){ try{ Matcher m = pn.matcher(work); if(m.lookingAt()){//是数字或variable Matcher m1= pn1.matcher(m.group()); Matcher m2 = pn2.matcher(m.group()); if(m1.matches()){//是合法变量组合 if(hash.containsKey(m.group())) sta.push(m.group(), 2);//在表里找得到的,是保留字 else sta.push(m.group(), 1);//找不到的,是变量 }else if(m2.matches()){//是数字组合 sta.push(m.group(), 3);//是数字 }else{//是字母数字,或数字,但不是合法组合 work=m.replaceFirst("");//将匹配部分去掉 throw new FormatException(m.group()); } work=m.replaceFirst("");//将匹配部分去掉 }else if(work.length()>1){//1.不是数字字母2。还有两个以上剩余 Matcher m3 = pn3.matcher(work); Matcher m4 = pn4.matcher(work); if(m3.lookingAt()){//若两个非字母数字相连 if (hash.containsKey(m3.group())){//若他们合在一起是个东西 sta.push(m3.group(), hash.get(m3.group())); work=m3.replaceFirst("");//将匹配部分去掉 }//end if else if(m4.lookingAt()){//在只有一个是non字母数字时 if(hash.containsKey(m4.group())){//若他是可以找到的 sta.push(m4.group(), hash.get(m4.group()));//就压栈 }else{work=m4.replaceFirst("");//将匹配部分去掉 throw new FormatException(m4.group());}//若找不到,就报错 work=m4.replaceFirst("");//将匹配部分去掉 }//end else if }else if(m4.lookingAt()){//////添加 if(hash.containsKey(m4.group())){ sta.push(m4.group(), hash.get(m4.group())); work=m4.replaceFirst("");//将匹配部分去掉 }else{work=m4.replaceFirst(""); throw new FormatException(m4.group());}//若找不到,就报错 } }else if (work.length()==1){//1.不是数字字母2。只有一个剩余 Matcher m5 = pn4.matcher(work); if(m5.lookingAt()){ if(hash.containsKey(m5.group())){ sta.push(m5.group(), hash.get(m5.group())); work=m5.replaceFirst("");//将匹配部分去掉 }else{//1.only one left which is not a num 2.it can't be found work=m5.replaceFirst("");//将匹配部分去掉 throw new FormatException(m5.group()); } } }else{//这里其实不可达 throw new lexicalError(work.substring(0,10)+"..."); }//end else }catch(FormatException fe){System.out.println(fe);} catch(lexicalError le){System.out.println(le);} //end try }//end while }//end for sta.push("#", 0);//#-->0 return sta; } ///////////////////////////// static Newhash nh = new Newhash();//用例测试 static void hashinit(){ /* * 1--variable * 2--preserved * 3--number * 4--logicsymbol * 5--mathsymbol * 6--othersymbol */ String[] preservedarr = {"if","else","while","read","write","int","real"}; String[] logicop= {"<","==","<>"}; String[] mathop= {"+","-","*","/","(",")"}; String[] otherop = {"=",";","{","}","/*","*/","[","]"}; nh.put(preservedarr, 2); nh.put(logicop,4); nh.put(mathop, 5); nh.put(otherop, 6); } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub /* * * BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); String test2 = ""; test2 = rd.readLine(); String[] test = test2.split("\\s+"); Pattern pn = Pattern.compile("([a-zA-Z0-9]|[.])+");//number,variable,illegic variable Pattern pn1= Pattern.compile("[a-zA-Z]([a-zA-Z0-9]*)");//variable Pattern pn2= Pattern.compile("([0]|([1-9]([0-9]*)))[.]([0-9]+)|[0]|([1-9]([0-9]*))");//number Pattern pn3= Pattern.compile("[^\\.a-zA-Z0-9][^\\.a-zA-Z0-9]");//两个非词 Pattern pn4= Pattern.compile("[^\\.a-zA-Z0-9]");//a non-word //非字母字符:先查两再查一 for(String str : test){ String work = str; while(work.length()>0){ try{ Matcher m = pn.matcher(work); if(m.lookingAt()){//是数字或variable Matcher m1= pn1.matcher(m.group()); Matcher m2 = pn2.matcher(m.group()); if(m1.matches()){//是合法变量组合 System.out.println(m.group());//在表里找得到的,是保留字 }else if(m2.matches()){//是数字组合 System.out.println(m2.group()); }else{//是字母数字,或数字,但不是合法组合 work=m.replaceFirst("");//将匹配部分去掉 throw new FormatException(m.group()); } work=m.replaceFirst("");//将匹配部分去掉 }else if(work.length()>1){//1.不是数字字母2。还有两个以上剩余 Matcher m3 = pn3.matcher(work); Matcher m4 = pn4.matcher(work);//一个non if(m3.lookingAt()){//若两个非字母数字相连 if (m3.group().equals("{{")){//若他们合在一起是个东西 System.out.println(m3.group()); work=m3.replaceFirst("");//将匹配部分去掉 }//end if else if(m4.lookingAt()){//在只有一个是non字母数字时 if(m4.group().equals("*")){//若他是可以找到的 System.out.println(m4.group()); work=m4.replaceFirst("");//将匹配部分去掉 }else{work=m4.replaceFirst("");//将匹配部分去掉 throw new FormatException(m4.group());}//若找不到,就报错 }//end else if }else if(m4.lookingAt()){//////添加 if(m4.group().equals("*")){ System.out.println(m4.group()); work=m4.replaceFirst("");//将匹配部分去掉 }else{work=m4.replaceFirst(""); throw new FormatException(m4.group());}//若找不到,就报错 } }else if (work.length()==1){//1.不是数字字母2。只有一个剩余 Matcher m5 = pn4.matcher(work); if(m5.lookingAt()){ if(m5.group().equals("*")){ System.out.println(m5.group()); work=m5.replaceFirst("");//将匹配部分去掉 }else{//1.only one left which is not a num 2.it can't be found work=m5.replaceFirst("");//将匹配部分去掉 throw new FormatException(m5.group()); } } }else{//这里其实不可达 throw new lexicalError(work.substring(0,10)+"..."); }//end else }catch(FormatException fe){System.out.println(fe);} catch(lexicalError le){System.out.println(le);} //end try }//end while }//end for *///test it Pattern pn7= Pattern.compile("/\\*.+\\*/"); BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); Matcher m7 = pn7.matcher(rd.readLine()); if(m7.find()) System.out.println(m7.group()); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -