Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. ArrayList<Token> lex ( String input ) {
  2. ArrayList<Token> tl = new ArrayList<>();
  3.  
  4. Pattern pattern = null;
  5. pattern = Pattern.compile("if[^a-zA-Z]|[a-zA-Z]+|[0-9]+|[+]|[-]");
  6.  
  7. for (String s: input.split("\\s")) {
  8.  
  9. System.out.println(s);
  10. Matcher matcher = pattern.matcher(s);
  11. while (matcher.find()) {
  12. if (matcher.group().matches("if") && s.length() == 2) {
  13. tl.add(new T_If());
  14. }
  15. else if (matcher.group().matches("then") && s.length() == 4) {
  16. tl.add(new T_Then());
  17. }
  18. else if (matcher.group().matches("[+]")) {
  19. tl.add(new T_Plus());
  20. }
  21. else if (matcher.group().matches("[-]")) {
  22. tl.add(new T_Minus());
  23. }
  24. else if (matcher.group().matches("[a-zA-Z]+(\\w)*")) {
  25. tl.add(new T_Identifier(matcher.group()));
  26. }
  27. else if (matcher.group().matches("[0-9]+")) {
  28. tl.add(new T_Integer(Integer.parseInt(matcher.group())));
  29. //System.out.println(matcher.group().toString());
  30. }
  31. }
  32. }
  33.  
  34. for (Token t: tl) {
  35. System.out.println(t.toString());
  36. }
  37.  
  38. return tl;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement