import java.util.*; import java.util.regex.*; import java.lang.*; import java.io.*; class Ideone { { String line; while ((line = in.readLine()) != null) { } } static enum TokenType { INTEGER, FLOAT, STRING, NAME, OPERATOR, COMMENT, COMMA, COLON, OPENBR, CLOSEBR, OPENAR, CLOSEAR, SEMICOLON } static class Token { public final TokenType type; { this.type = type; this.string = string; } { return this.string; } } { Pattern ptInt = Pattern.compile("^[0-9]+"); Pattern ptDbl = Pattern.compile("^(([0-9]+)?[.][0-9]+|[0-9]+#)"); Pattern ptStr = Pattern.compile("^\"([^\"]|\"\")+\""); Pattern ptName = Pattern.compile("^[a-zA-Z][a-zA-Z0-9]*(%|#|[$])?"); Pattern ptOpe = Pattern.compile("^(>=|<=|<>|[-/<>^=\\\\]|[+]|[*])"); Pattern ptCmt = Pattern.compile("^([Rr][Ee][Mm] |')"); Matcher matcher; List<Token> list = new ArrayList<Token>(); int index = 0; int len = line.length(); while (index < len) { if ((matcher = ptCmt.matcher(target)).lookingAt()) { index = line.length(); list.add(new Token(target, TokenType.COMMENT)); } else if ((matcher = ptDbl.matcher(target)).lookingAt()) { index += matcher.end(); list.add(new Token(matcher.group(), TokenType.FLOAT)); } else if ((matcher = ptInt.matcher(target)).lookingAt()) { index += matcher.end(); list.add(new Token(matcher.group(), TokenType.INTEGER)); } else if ((matcher = ptStr.matcher(target)).lookingAt()) { index += matcher.end(); temp = temp.substring(1, temp.length() - 1); temp = temp.replaceAll("\"\"", "\""); list.add(new Token(temp, TokenType.STRING)); } else if ((matcher = ptName.matcher(target)).lookingAt()) { index += matcher.end(); list.add(new Token(matcher.group().toUpperCase(), TokenType.NAME)); } else if ((matcher = ptOpe.matcher(target)).lookingAt()) { index += matcher.end(); list.add(new Token(matcher.group(), TokenType.OPERATOR)); } else if (target.startsWith("(")) { index++; list.add(new Token("(", TokenType.OPENBR)); } else if (target.startsWith(")")) { index++; list.add(new Token(")", TokenType.CLOSEBR)); } else if (target.startsWith("[")) { index++; list.add(new Token("[", TokenType.OPENAR)); } else if (target.startsWith("]")) { index++; list.add(new Token("]", TokenType.CLOSEAR)); } else if (target.startsWith(",")) { index++; list.add(new Token(",", TokenType.COMMA)); } else if (target.startsWith(":")) { index++; list.add(new Token(":", TokenType.COLON)); } else if (target.startsWith(";")) { index++; list.add(new Token(";", TokenType.SEMICOLON)); } else if (target.startsWith(" ")) { index++; } else { index++; } } return list; } }
1000 REM コメント dayo 1010 LET A = 123 1020 LET B% = (234*33)+45\3+MAX(A,20) ' コメント dayo 1030 LET C# = 345.678 / SIN(180/PI#) + 1# 1040 LET D$ = "ABC ""DE""FG HIJ" 1050 DIM E[100], F[10][20] 1060 LET E[23]=A*A+B% 1070 print a; b%; c#; "hoge" 1080 if a=b% then goto 1070 else goto 1090 1090 for i=0 to 10 1100 print i;e[i] 1110 next i 1120 end
[1000, REM コメント dayo] [1010, LET, A, =, 123] [1020, LET, B%, =, (, 234, *, 33, ), +, 45, \, 3, +, MAX, (, A, ,, 20, ), ' コメント dayo] [1030, LET, C#, =, 345.678, /, SIN, (, 180, /, PI#, ), +, 1#] [1040, LET, D$, =, ABC "DE"FG HIJ] [1050, DIM, E, [, 100, ], ,, F, [, 10, ], [, 20, ]] [1060, LET, E, [, 23, ], =, A, *, A, +, B%] [1070, PRINT, A, ;, B%, ;, C#, ;, hoge] [1080, IF, A, =, B%, THEN, GOTO, 1070, ELSE, GOTO, 1090] [1090, FOR, I, =, 0, TO, 10] [1100, PRINT, I, ;, E, [, I, ]] [1110, NEXT, I] [1120, END]