public class GramAnalyse {
private String str = null;
private String currentToken;
private String[] Token;
private int tokenIndex = 0;
private enum tokenList{
//文件开始结束标识
EOF,
//标识符
IDENTIFIER,
//操作符
COLEQ,PLUS,
//数字
NUMBER,
//分隔符
SEMI,LBRACE,COMMA,RBRACE,
//关键字
OUT
}

GramAnalyse(String str){
this.str = str;
}

public void analyse(){
if (str!=null){
Token = str.split(" ");
}
currentToken = Token[tokenIndex];
if (currentToken=="EOF") this.match(tokenList.EOF);
stmtSequence();
//System.out.println("Successful File!");
}

private void stmtSequence(){
this.stmt();
while (currentToken=="SEMI"){
this.stmt();
}
}

private void stmt(){
if (currentToken=="IDENTIFIER"){
match(tokenList.IDENTIFIER);
match(tokenList.COLEQ);
exp();
}
else if (currentToken=="OUT"){
match(tokenList.OUT);
match(tokenList.LBRACE);
pl();
match(tokenList.RBRACE);
} else {
System.out.println(currentToken);
System.exit(-1);
}
}

private void exp(){
if(currentToken=="IDENTIFIER"){
match(tokenList.IDENTIFIER);
if (currentToken=="PLUS"){
match(tokenList.PLUS);
exp();
}
}
else if(currentToken=="NUMBER"){
match(tokenList.NUMBER);
if (currentToken=="PLUS"){
match(tokenList.PLUS);
exp();
}
}
else if(currentToken=="LBRACE"){
match(tokenList.LBRACE);
stmt();
match(tokenList.COMMA);
exp();
match(tokenList.RBRACE);
}
}

private void pl(){
exp();
while(currentToken=="COMMA"){
match(tokenList.COMMA);
exp();
}
}

private void match(tokenList currenttoken){
if (tokenIndex<Token.length){
switch(currenttoken){
case EOF:
if (currentToken=="EOF"){
tokenIndex++;
currentToken = Token[tokenIndex];
System.out.println(currentToken);
} else {
System.out.println("there is an error for EOF!");
}
break;
case IDENTIFIER:
if (currentToken=="IDENTIFIER"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for IDENTIFIER!");
}
break;
case OUT:
if (currentToken=="OUT"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for OUT!");
}
break;
case NUMBER:
if (currentToken=="NUMBER"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for NUMBER!");
}
break;
case COLEQ:
if (currentToken=="COLEQ"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for COLEQ!");
}
break;
case PLUS:
if (currentToken=="PLUS"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for PLUS!");
}
break;
case LBRACE:
if (currentToken=="LBRACE"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for LBRACE!");
}
break;
case RBRACE:
if (currentToken=="RBRACE"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for RBRACE!");
}
break;
case COMMA:
if (currentToken=="COMMA"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for SEMI!");
}
break;
case SEMI:
if (currentToken=="SEMI"){
tokenIndex++;
currentToken = Token[tokenIndex];
} else {
System.out.println("there is an error for SEMI!");
}
break;
default:
{
System.out.println("There is an error!");
System.exit(-1);
}
}
}
}
}我明明已经写了方法,为什么不能掉用呢?Javaenum

解决方案 »

  1.   

    while(currentToken=="COMMA"){这一句没有进入吧。判断错了。
      

  2.   

    对不起哈,忘了给你文本文档了,你看着这个给俺解一下呗,谢谢哈
    EOF
    IDENTIFIERwef COLEQ NUMBER SEMI 
    IDENTIFIER COLEQ NUMBER SEMI 
    IDENTIFIER COLEQqw NUMBER SEMI 
    IDENTIFIER COLEQ NUMBER SEMI 
    OUT LBRACE IDENTIFIER COMMA IDENTIFIER COMMA IDENTIFIER COMMA IDENTIFIER RBRACE 
    EOF
    第一个EOF都没有进入,不知道为什么?
      

  3.   


    import java.io.*;public class Main {
    public static void main(String[] args) {
    try {
    FileReader fr = new FileReader(new File("1.txt"));
    BufferedReader br = new BufferedReader(fr);
    String str;
    while((str=br.readLine())!=null){
    GramAnalyse ga = new GramAnalyse(str);
    ga.analyse();
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e){
    e.printStackTrace();
    }
    }}这个是main方法,您要是有时间帮俺调试下呗,嘿嘿,马上要去上课了,谢谢哈
      

  4.   

    因为你没有进入while循环,当然不会进入EOF了
      

  5.   

    谢了,是因为判断String内容是否相等,用equals方法,一个低级错误。