TokenType ([{}{}()])[{}]{}
package com.kinglead; import java.util.ArrayList; import java.util.List; /** * @author kinglead * @date 2021-01-29 下午 20:55 * @describe {请写具体描述} */ public class Test3 { private static int LEFT_SQUARE_BRACKET_COUNT = 0; private static int LEFT_CURLY_BRACKET_COUNT = 0; private static int LEFT_BRACKET_COUNT = 0; //([{}{}()])[{}]{} public static void main(String[] args) { List<Token> list = parseCode("([{}{}()])[{}]{}"); System.out.println(list); } private static List<Token> parseCode(String codeText){ //空判断 if(codeText == null || "".equals(codeText)) return new ArrayList<>(); List<Token> list = new ArrayList<>(); while (!"".equals(codeText)) { String firstStr = codeText.substring(0,1); if ("[".equals(firstStr)) { LEFT_SQUARE_BRACKET_COUNT++; Token token = new Token(); token.setType(TokenType.LEFT_SQUARE_BRACKET); list.add(token); codeText = func(token, codeText.substring(1)); } if ("{".equals(firstStr)) { LEFT_CURLY_BRACKET_COUNT++; Token token = new Token(); token.setType(TokenType.LEFT_CURLY_BRACKET); list.add(token); codeText = func(token, codeText.substring(1)); } if ("(".equals(firstStr)) { LEFT_BRACKET_COUNT++; Token token = new Token(); token.setType(TokenType.LEFT_BRACKET); list.add(token); codeText = func(token, codeText.substring(1)); } } // func(null, codeText, list); return list; } private static String func(Token token, String codeText){ if(codeText == null || "".equals(codeText)) return ""; List<Token> childrenList = new ArrayList<>(); String firstStr = codeText.substring(0,1); if("[".equals(firstStr)){ LEFT_SQUARE_BRACKET_COUNT++; Token tokenChildren = new Token(); tokenChildren.setType(TokenType.LEFT_SQUARE_BRACKET); childrenList.add(tokenChildren); codeText = func(tokenChildren, codeText.substring(1)); token.setChildren(childrenList); } if("]".equals(firstStr)){ LEFT_SQUARE_BRACKET_COUNT--; Token tokenChildren = new Token(); tokenChildren.setType(TokenType.RIGHT_SQUARE_BRACKET); childrenList.add(tokenChildren); if(LEFT_SQUARE_BRACKET_COUNT != 0 || LEFT_CURLY_BRACKET_COUNT != 0 || LEFT_BRACKET_COUNT != 0) { codeText = func(tokenChildren, codeText.substring(1)); }else{ codeText = codeText.substring(1); } token.setChildren(childrenList); } if("{".equals(firstStr)){ LEFT_CURLY_BRACKET_COUNT++; Token tokenChildren = new Token(); tokenChildren.setType(TokenType.LEFT_CURLY_BRACKET); childrenList.add(tokenChildren); codeText = func(tokenChildren, codeText.substring(1)); token.setChildren(childrenList); } if("}".equals(firstStr)){ LEFT_CURLY_BRACKET_COUNT--; Token tokenChildren = new Token(); tokenChildren.setType(TokenType.RIGHT_CURLY_BRACKET); childrenList.add(tokenChildren); if(LEFT_SQUARE_BRACKET_COUNT != 0 || LEFT_CURLY_BRACKET_COUNT != 0 || LEFT_BRACKET_COUNT != 0) { codeText = func(tokenChildren, codeText.substring(1)); }else{ codeText = codeText.substring(1); } token.setChildren(childrenList); } if("(".equals(firstStr)){ LEFT_BRACKET_COUNT++; Token tokenChildren = new Token(); tokenChildren.setType(TokenType.LEFT_BRACKET); childrenList.add(tokenChildren); codeText = func(tokenChildren, codeText.substring(1)); token.setChildren(childrenList); } if(")".equals(firstStr)){ LEFT_BRACKET_COUNT--; Token tokenChildren = new Token(); tokenChildren.setType(TokenType.RIGHT_BRACKET); childrenList.add(tokenChildren); if(LEFT_SQUARE_BRACKET_COUNT != 0 || LEFT_CURLY_BRACKET_COUNT != 0 || LEFT_BRACKET_COUNT != 0) { codeText = func(tokenChildren, codeText.substring(1)); }else{ codeText = codeText.substring(1); } token.setChildren(childrenList); } return codeText; } // 词法树节点 static class Token { TokenType type; List<Token> children; public TokenType getType() { return type; } public void setType(TokenType type) { this.type = type; } public List<Token> getChildren() { return children; } public void setChildren(List<Token> children) { this.children = children; } @Override public String toString() { return "Token{" + "type=" + type + ", children=" + children + '}'; } } // 词类型 enum TokenType { LEFT_SQUARE_BRACKET, // [ RIGHT_SQUARE_BRACKET, // ] LEFT_CURLY_BRACKET, // { RIGHT_CURLY_BRACKET, // } LEFT_BRACKET, // ( RIGHT_BRACKET, // ) } }