Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. package ee.ut.cs.akt.aktk;
  2.  
  3. import static ee.ut.cs.akt.aktk.TokenType.*;
  4.  
  5. import java.io.Reader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Arrays;
  9.  
  10. public class Lexer {
  11.  
  12. private String[] parts;
  13.  
  14. public Lexer(String input) {
  15. //input = input.toLowerCase();
  16. input = input.replaceAll( "//.*|(\"(?:\\\\[^\"]|\\\\\"|.)*?\")|(?s)/\\*.*?\\*/", "$1 " ); //eemaldab commentid, pretty smart huh?
  17. for (String symbol : new String[]{"+", "-", "*", "/", "(", ")", "{", "}"}) {
  18. input = input.replace(symbol, " " + symbol + " ");
  19. }
  20. this.parts = input.trim().split("\\s+");
  21. }
  22.  
  23. public List<Token> readAllTokens() {
  24. System.out.println(Arrays.toString(parts));
  25. List<Token> result = new ArrayList<Token>();
  26. for (String part : parts) {
  27. if (part.equals("(")) {
  28. result.add(new Token(TokenType.LPAREN, null));
  29. }
  30. else if (part.equals(")")) {
  31. result.add(new Token(TokenType.RPAREN, null));
  32. }
  33. else if (part.equals("if")) {
  34. result.add(new Token(TokenType.IF, null));
  35. }
  36. else if (part.equals("while")) {
  37. result.add(new Token(TokenType.WHILE, null));
  38. }
  39. else if (part.equals("var")) {
  40. result.add(new Token(TokenType.VAR, null));
  41. }
  42. else if (part.equals("+")) {
  43. result.add(new Token(TokenType.PLUS, null));
  44. }
  45. else if (part.equals("-")) {
  46. result.add(new Token(TokenType.MINUS, null));
  47. }
  48. else if (part.equals("{")) {
  49. result.add(new Token(TokenType.LBRACE, null));
  50. }
  51. else if (part.equals("}")) {
  52. result.add(new Token(TokenType.RBRACE, null));
  53. }
  54. else if (part.equals("*")) {
  55. result.add(new Token(TokenType.TIMES, null));
  56. }
  57. else if (part.equals("/")) {
  58. result.add(new Token(TokenType.DIV, null));
  59. }
  60. else {
  61. try {
  62. Integer.parseInt(part);
  63. result.add(new Token(TokenType.INTEGER, Integer.parseInt(part)));
  64. } catch (NumberFormatException e) {
  65. try {
  66. Double.parseDouble(part);
  67. result.add(new Token(TokenType.DOUBLE, Double.parseDouble(part)));
  68. } catch (NumberFormatException e2) {
  69. //kontrollin kas string
  70. if ((part.startsWith("\"")) && (part.endsWith("\""))) {
  71. result.add(new Token(TokenType.STRING, part.replace("\"", "")));
  72. }
  73. else {
  74. // Nüüd peaks asi olema muutuja nimi.
  75. // Kontrolli, kas vastab muutuja nime reeglitele
  76. if ((!Character.isLetter(part.charAt(0))) && (!(part.startsWith("_")))) {
  77. throw new IllegalArgumentException("Muutujanimi peab algama tähe või alakriipsuga");
  78. }
  79. for (int i = 0; i < part.length(); i++) {
  80. if ((!Character.isLetterOrDigit(part.charAt(i))) && (part.charAt(i)!="_".charAt(0))) {
  81. throw new IllegalArgumentException("Muutujanimi ei tohi sisaldada midagi muud peale tähtede, numbrite ja alakriipsu");
  82. }
  83. }
  84. // kontroll läbitud, salvestame tokeni
  85. result.add(new Token(TokenType.VARIABLE, part));
  86. }
  87. }
  88. }
  89. }
  90. }
  91. result.add(new Token(TokenType.EOF, null));
  92. return result;
  93. }
  94.  
  95.  
  96. /**
  97. * Boonus
  98. *
  99. * Tagasta list, kus lekseemid on pakendatud koos infoga nende paiknemise kohta lähtekoodis
  100. */
  101. public List<PositionedToken> readAllPositionedTokens() {
  102. throw new UnsupportedOperationException();
  103. }
  104.  
  105. /**
  106. * Boonus2
  107. *
  108. * Kui Lexer toetab Reader-iga konstruktorit, siis
  109. * readNextToken-i kasutamisel ei tohiks sisendit lugeda korraga rohkem,
  110. * kui järgmise lekseemi tuvastamiseks hädapärast tarvis.
  111. *
  112. * NB! Arvesta, et Reader-i markSupported() võib tagastada false.
  113. */
  114. public Lexer(Reader input) {
  115. throw new UnsupportedOperationException();
  116. }
  117.  
  118. /**
  119. * See meetod peab olema implementeeritud vaid boonuse saamiseks
  120. */
  121. public Token readNextToken() {
  122. throw new UnsupportedOperationException();
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement