今天在网上逛的时候,看到一段识别java注释代码的自动机,所以跟大家分享!(因为链接地址不记得了,所以涉及版权问题请与我联系)
class CommentAutoMata{
final int start=0;
final int one=1;
final int two=2;
final int three=3;
final int finalState=4;
final int five=5;
final int six=6;
public List getCommentList(String s){
ArrayList ret=new ArrayList();
int begin=-1;
int curState=start;
for(int i=0;i
switch(curState){
case(start):
if(c=='/'){
begin=i;
curState=one;
}
else if(c=='”'){
//识别字符出
curState=5;
}
break;
case(one):
if(c=='*') curState=two;
else curState=start;
break;
case(two):
if(c=='*') curState=three;
break;
case(three):
if(c=='/'){
curState=finalState;
//eat
ret.add(s.substring(begin,i+1));
//reset;
curState=start;
begin=-1;
}
else curState=two;
break;
case(five):
if(c=='”') curState=start;
else if(c=='\\')curState=six;
break;
case(six):
curState=five;
break;
}
}
return ret;
}
}