Lexer正则表达式

时间:2017-07-10 10:23:43

标签: java regex lexer

我是Java的新手,必须从算术表达式中编译词法分析器以进行进一步计算。我找到了有用的代码来检查,但是在分析字符串时有一个问题。当我有类似的东西:5-9.3它确定为:

NUMBER 5

NUMBER -9.3

而不是

NUMBER 5

ADDSUBSTR -

NUMBER 9.3

不知道如何修复它。非常感谢你的帮助。想要解决它,因为在计算时更难以应对它。抱歉我的排版很糟糕。

public class Lexer {

    public static enum TokenType {

        NUMBER("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?"), ADDSUBSTR("[+|-]"), DIVMULT ("[/|*]"), WHITESPACE("[ \t\f\r\n]+");
        public final String pattern;

        private TokenType(String pattern) {
        this.pattern = pattern;
    }
    }


    public static class Token {
        public TokenType token;
        public String data;

        public Token (TokenType token, String data) {
            this.token = token;
            this.data = data;
        }

        @Override
        public String toString() {
        return String.format("%s %s", token.name(), data);
        }
    }

    public static ArrayList<Token> lexer(String s) {

        ArrayList<Token> tokens = new ArrayList<>();

        StringBuffer tokenBuffer = new StringBuffer();
        for (TokenType tokenType : TokenType.values())
          tokenBuffer.append(String.format("|(?<%s>%s)", tokenType.name(), tokenType.pattern));
        Pattern tokenPatterns = Pattern.compile(new String(tokenBuffer.substring(1)));

        Matcher matcher = tokenPatterns.matcher(s);
        while (matcher.find()) {
          if (matcher.group(TokenType.NUMBER.name()) != null) {
            tokens.add(new Token(TokenType.NUMBER, matcher.group(TokenType.NUMBER.name())));
          }
          else if (matcher.group(TokenType.ADDSUBSTR.name()) != null) {
            tokens.add(new Token(TokenType.ADDSUBSTR, matcher.group(TokenType.ADDSUBSTR.name())));
          } 
          else if (matcher.group(TokenType.DIVMULT.name()) != null) {
            tokens.add(new Token(TokenType.DIVMULT, matcher.group(TokenType.DIVMULT.name())));
          }
          else if (matcher.group(TokenType.WHITESPACE.name()) != null);
            }

    return tokens;

    }

    public static void main(String[] args) {

        try (Scanner in = new Scanner(System.in)) {
            String input = in.nextLine();
            ArrayList<Token> tokens = lexer(input);
            for (Token token : tokens)
                System.out.println(token);
        }
    }

}

0 个答案:

没有答案