資源簡介
java編寫的詞法分析器模擬自動機的運行,但是此程序只是初級版本未實現報錯功能,提供一種思想。附測試用例

代碼片段和文件信息
package?V2;
import?java.io.BufferedReader;
import?java.io.FileOutputStream;
import?java.io.FileReader;
import?java.io.IOException;
import?java.io.PrintWriter;
public?class?WordAnalyzer?{
public?int?currentLine?=?0;
public?char?cha?=?‘?‘;
BufferedReader?in?=?null;
PrintWriter?out;
boolean?isEnd?=?false;
public?static?void?main(String?args[]){
new?WordAnalyzer().analyze();
}
boolean?isValidAfterANumber(char?peek)?{
if?(peek?==?9?||?peek?==?10?||?peek?==?13?||?peek?==?32
?||peek?==?37?||peek?==?58?||?(peek?>=?40?&&?peek?<=?47)?||?(peek?>=?59?&&?peek?<=?62))
return?true;
else
return?false;
}
???private??boolean?isValidAfterRemaindedWords(char?peek){//保留字后面只能是空格?或\r\n
if?(peek?==?9?||?peek?==?10?||?peek?==?13?||?peek?==?32||peek==‘ ‘){
return?true;
}
return?false;
????}
public?void?deleteSpaces()throws?IOException{?
//結尾的換行是/r/n
if?(cha?==?‘\n‘||cha?==?‘\r‘?||?cha?==?‘?‘||cha==‘ ‘)?{
if(cha==‘\n‘){
currentLine++;
//System.out.println(“CURRENT=“+currentLine);
}
//System.out.println(“執行刪除空格11“);
in.mark(2);
getNextChar();
while(cha==‘?‘||cha==‘\r‘||cha==‘\n‘||cha==‘ ‘){
if(cha==‘\n‘){
currentLine++;
System.out.println(“CURRENT2=“+currentLine);
}
// System.out.println(“執行刪除空格“);
in.mark(2);
getNextChar();
}
in.reset();
// System.out.println(“退出刪除空格“+cha);
}
// System.out.println(“chdddd退出刪除空格“+cha);
}
public?void?getNextChar()?{
try?{
int?nextChar?=?0;
if?((nextChar?=?in.read())?!=?-1)?{
cha?=?(char)?nextChar;
}?else?{
//System.out.println(“讀到結尾了!“);
isEnd?=?true;
cha=‘#‘;
return;
//System.out.println(“結尾“+isEnd);
}
//System.out.println(“讀到了字符??“+cha+“-->“+(int)cha);
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
public?boolean?getACharisEquals(char?c)?{
getNextChar();
if(cha==c)
return?true;
else
return?false;
}
private??boolean?isBEGIN()?throws?IOException{
String?temp=““;
char?a;
int?s;
in.mark(7);
for(int?i=0;i<4;i++){
if((s=in.read())==-1){
//System.out.println(“合法的1“);
return?false;
}
else{
temp=temp+(char)s;
}
}
a=(char)(in.read());
if(isValidAfterRemaindedWords(a)){
//System.out.println(“合法的1“);
if(temp.equals(“egin“)){
// System.out.println(“合法的12“);
return?true;
}
else{
// System.out.println(“合法的123“);
return?false;
}
}
else{
// System.out.println(“合法的1234“);
return?false;
}
}?
private?boolean?isREAD()throws?IOException{
String?temp=““;
char?a;
int?s;
in.mark(7);
for(int?i=0;i<3;i++){
if((s=in.read())==-1){
return?false;
}
else{
temp=temp+(char)s;
}
}
a=(char)(in.read());
if(isValidAfterRemaindedWords(a)){
if(temp.equals(“ead“)){
return?true;
}
else{
return?false;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????657??2010-11-11?15:54??test.txt
?????文件??????16396??2010-11-11?16:17??WordAnalyzer.java
-----------?---------??----------?-----??----
????????????????17053????????????????????2
評論
共有 條評論