fork download
  1. import java.util.*;
  2. import java.util.regex.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String line;
  11. while ((line = in.readLine()) != null)
  12. {
  13. System.out.println(toTokens(line));
  14. }
  15. }
  16.  
  17. static enum TokenType
  18. {
  19. INTEGER, FLOAT, STRING, NAME, OPERATOR, COMMENT,
  20. COMMA, COLON, OPENBR, CLOSEBR, OPENAR, CLOSEAR,
  21. SEMICOLON
  22. }
  23.  
  24. static class Token
  25. {
  26. public final TokenType type;
  27. public final String string;
  28. Token(String string, TokenType type)
  29. {
  30. this.type = type;
  31. this.string = string;
  32. }
  33. @Override public String toString()
  34. {
  35. return this.string;
  36. }
  37. }
  38.  
  39. static List<Token> toTokens(String line)
  40. {
  41. Pattern ptInt = Pattern.compile("^[0-9]+");
  42. Pattern ptDbl = Pattern.compile("^(([0-9]+)?[.][0-9]+|[0-9]+#)");
  43. Pattern ptStr = Pattern.compile("^\"([^\"]|\"\")+\"");
  44. Pattern ptName = Pattern.compile("^[a-zA-Z][a-zA-Z0-9]*(%|#|[$])?");
  45. Pattern ptOpe = Pattern.compile("^(>=|<=|<>|[-/<>^=\\\\]|[+]|[*])");
  46. Pattern ptCmt = Pattern.compile("^([Rr][Ee][Mm] |')");
  47. Matcher matcher;
  48. List<Token> list = new ArrayList<Token>();
  49. int index = 0;
  50. int len = line.length();
  51. while (index < len)
  52. {
  53. String target = line.substring(index);
  54. if ((matcher = ptCmt.matcher(target)).lookingAt())
  55. {
  56. index = line.length();
  57. list.add(new Token(target, TokenType.COMMENT));
  58. }
  59. else if ((matcher = ptDbl.matcher(target)).lookingAt())
  60. {
  61. index += matcher.end();
  62. list.add(new Token(matcher.group(), TokenType.FLOAT));
  63. }
  64. else if ((matcher = ptInt.matcher(target)).lookingAt())
  65. {
  66. index += matcher.end();
  67. list.add(new Token(matcher.group(), TokenType.INTEGER));
  68. }
  69. else if ((matcher = ptStr.matcher(target)).lookingAt())
  70. {
  71. index += matcher.end();
  72. String temp = matcher.group();
  73. temp = temp.substring(1, temp.length() - 1);
  74. temp = temp.replaceAll("\"\"", "\"");
  75. list.add(new Token(temp, TokenType.STRING));
  76. }
  77. else if ((matcher = ptName.matcher(target)).lookingAt())
  78. {
  79. index += matcher.end();
  80. list.add(new Token(matcher.group().toUpperCase(), TokenType.NAME));
  81. }
  82. else if ((matcher = ptOpe.matcher(target)).lookingAt())
  83. {
  84. index += matcher.end();
  85. list.add(new Token(matcher.group(), TokenType.OPERATOR));
  86. }
  87. else if (target.startsWith("("))
  88. {
  89. index++;
  90. list.add(new Token("(", TokenType.OPENBR));
  91. }
  92. else if (target.startsWith(")"))
  93. {
  94. index++;
  95. list.add(new Token(")", TokenType.CLOSEBR));
  96. }
  97. else if (target.startsWith("["))
  98. {
  99. index++;
  100. list.add(new Token("[", TokenType.OPENAR));
  101. }
  102. else if (target.startsWith("]"))
  103. {
  104. index++;
  105. list.add(new Token("]", TokenType.CLOSEAR));
  106. }
  107. else if (target.startsWith(","))
  108. {
  109. index++;
  110. list.add(new Token(",", TokenType.COMMA));
  111. }
  112. else if (target.startsWith(":"))
  113. {
  114. index++;
  115. list.add(new Token(":", TokenType.COLON));
  116. }
  117. else if (target.startsWith(";"))
  118. {
  119. index++;
  120. list.add(new Token(";", TokenType.SEMICOLON));
  121. }
  122. else if (target.startsWith(" "))
  123. {
  124. index++;
  125. }
  126. else
  127. {
  128. System.out.println("Unknown: " + target.substring(0, 1));
  129. index++;
  130. }
  131. }
  132. return list;
  133. }
  134. }
Success #stdin #stdout 0.09s 380352KB
stdin
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
stdout
[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]