2014-12-06 61 views
0

這個代碼分析數字和運營商只我需要它來分析字母只,並把其他事情無效我應該做些什麼細節我從教程網站得到這段代碼,並修改它,但沒有結果。詞法分析在Java中的字母

 package lexical; 

    public class Tokenizer { 


int pos; 
char[] expression; 

Tokenizer(String expression) { 
    this.expression = expression.toCharArray(); 
    this.pos = 0; 
} 

enum Type { OPERATOR, LITTER, UNKNOWN } 

class Lexeme { 
    String type, token; 
    Lexeme(String type, String token) { 
     this.type = type; 
     this.token = token; 
    } 
} 

Lexeme getNextToken() { 
    StringBuilder token = new StringBuilder(); 
    boolean endOfToken = false; 
    Type type = Type.UNKNOWN; 
    while (!endOfToken && hasMoreTokens()) { 
     while(expression[pos] == ' ' && hasMoreTokens()) 
      pos++; 
     switch (expression[pos]) { 
      case '+': 
      case '-': 
      case '*': 
      case '/': 
       if(type != Type.LITTER) { 
        type = Type.OPERATOR; 
        token.append(expression[pos]); 
        pos++; 
       } 
       endOfToken = true; 
       break; 
      case ' ': 
       endOfToken = true; 
       pos++; 
       break; 
      default: 
       if(Character.isDigit(expression[pos]) || expression[pos] == '.') { 
        token.append(expression[pos]); 
        type = Type.LITTER; 
       } else { 
        System.out.println("Systax error at position: " + pos); 
       } 
       pos++; 
       break; 
     } 
    } 
    return new Lexeme(type.name().toLowerCase(), token.toString()); 
} 

boolean hasMoreTokens() { 
    return pos < expression.length; 
} 

public static void main(String[] args) { 
    String expression = "54+18+5"; 
    Tokenizer tokenizer = new Tokenizer(expression); 
    while (tokenizer.hasMoreTokens()) { 
     Lexeme nextToken = tokenizer.getNextToken(); 
     System.out.print("Type: " + nextToken.type + "\tLexeme: " + nextToken.token + "\n"); 
    } 
} 


} 

回答

0

這裏這部分是它正在尋找一個數字, 給出了一個非數字或非週期性質的錯誤。

if(Character.isDigit(expression[pos]) || expression[pos] == '.') { 
    token.append(expression[pos]); 
    type = Type.LITTER; 
} else { 
    System.out.println("Systax error at position: " + pos); 
} 

字符串可以normallly不放任何東西,所以你只想消除if語句,做

default: 
    token.append(expression[pos]); 

但是,如果你想限制你的字符串中的字符,然後修改,如果條件檢查你想要的角色。而不是:

if(Character.isDigit(expression[pos]) || expression[pos] == '.') { 

將其更改爲只接受您認爲在字符串中有效的字符。例如

if (Character.isAlphabetic(expression[pos]) || expression[pos] == "-") { 

您可能還需要一個新的標識,而不是

type = Type.LITTER; 
+0

我努力,因爲你發佈你的答案,但沒有 你能不能請張貼檢查所有字母和什麼是所有代碼無效 在此先感謝 – user3513220 2014-12-06 14:02:15

+0

什麼字符是字母 - 只是AZ,az? – Scooter 2014-12-06 14:18:25

+0

字符是字母a-z – user3513220 2014-12-06 14:55:19

0

我不知道你想怎麼處理「A12」或任何部分是字母和數字的一部分。此代碼將返回「A」爲「A12」

package lexical; 

public class Tokenizer { 

    int pos; 
    char[] expression; 

    Tokenizer(String expression) { 
     this.expression = expression.toCharArray(); 
     this.pos = 0; 
    } 

    enum Type { 

     OPERATOR, ALPHA, UNKNOWN, LITTER, ERROR 
    } 

    class Lexeme { 

     String type, token; 

     Lexeme(String type, String token) { 
      this.type = type; 
      this.token = token; 
     } 
    } 

    Lexeme getNextToken() { 
     StringBuilder token = new StringBuilder(); 
     boolean endOfToken = false; 
     Type type = Type.UNKNOWN; 
     // skip whitespace 
     while (expression[pos] == ' ' && hasMoreTokens()) { 
      pos++; 
     } 

     while (!endOfToken && hasMoreTokens()) { 

      switch (expression[pos]) { 
       case ' ': 
       { 
        endOfToken = true; 
        pos++; 
        break; 
       } 
       default: 
        if (Character.isAlphabetic(expression[pos])) {      
         token.append(expression[pos]); 
         type = Type.ALPHA;      
        } else { 
         System.out.println("Systax error at position: " + pos); 
        } 
        pos++; 
        break; 
      } 
     } 
     return new Lexeme(type.name().toLowerCase(), token.toString()); 
    } 

    boolean hasMoreTokens() { 
     return pos < expression.length; 
    } 

    public static void main(String[] args) { 
     expression = "Hello World"; 
     Tokenizer tokenizer = new Tokenizer(expression); 
     while (tokenizer.hasMoreTokens()) { 
      Lexeme nextToken = tokenizer.getNextToken(); 
      System.out.print("Type: " + nextToken.type + "\tLexeme: " + nextToken.token + "\n"); 
     }   
     expression = "123 ABC A12"; 
     tokenizer = new Tokenizer(expression); 
     while (tokenizer.hasMoreTokens()) { 
      Lexeme nextToken = tokenizer.getNextToken(); 
      System.out.print("Type: " + nextToken.type + "\tLexeme: " + nextToken.token + "\n"); 
     }   
    } 
} 

輸出:

Type: alpha Lexeme: Hello 
Type: alpha Lexeme: World 
Systax error at position: 0 
Systax error at position: 1 
Systax error at position: 2 
Type: unknown Lexeme: 
Type: alpha Lexeme: ABC 
Systax error at position: 9 
Systax error at position: 10 
Type: alpha Lexeme: A