Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. package pr3;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import java.io.BufferedReader;
  9. import java.io.FileReader;
  10. import java.io.IOException;
  11.  
  12. public class Main {
  13. static BufferedReader c1;
  14. public static enum TokenType {
  15. STRING("\'[^']*\'"),
  16. COMMENT("(\\*(.|\\n)*?\\*) | //.* [%]+ | \\{(.|\\n)*?\\}"), //ВОПРОС
  17. KEYWORD("(?<!\\w)(?:[Pp]rogram|[Uu]ses|[Vv]ar|[Bb]egin|[Ee]nd|[Ww]hile|[Ii]f|[Tt]hen|[Ee]lse|[Dd]o|[Rr]epeat|[Uu]ntil|[Oo]f|[Ff]unction|[Pp]rocedure|[Tt]o|[Dd]ownto|[Ff]or|[Aa]bsolute|[Aa]nd|[Aa]rray|[Cc]ase|[Cc]onst|[Dd]iv|[Gg]oto|[Ll]abel|[Ii]nterface|[Ll]ibrary|[Mm]od|[Nn]ot|[Oo]r|[Oo]bject|[Tt]ype|[Ww]ith|[Xx]or)(?!\\w)"),
  18. OPERATOR(":=|<>|[*|/|+|-|<|>|=]|div|mod|and|or|xor|<=|>="),
  19. TYPE("[Ii]nteger|[Rr]eal|[Cc]har|[Bb]yte|[Ww]ord|[Ss]hortint|[Ll]ongint|[Ii]nt64|[Uu]int64|[Dd]ouble|[Ss]tring|[Bb]oolean|[Aa]rray|[Rr]ecord|[Pp]ointer"),
  20. NUMBER("-?[0-9]?[.]?[0-9]+"),
  21. IDENTIFIER("\\w+"),
  22. BRACKET("[\\[|\\]|(|)]"),
  23. PUNCTUATION("[.|,|:|;|.]"),
  24. WHITESPACE("\\s+"),
  25. UNEXPECTED("\\S+");
  26.  
  27. public final String pattern;
  28.  
  29. private TokenType(String pattern) {
  30. this.pattern = pattern;
  31. }
  32. }
  33.  
  34. public static class Token {
  35. public TokenType type;
  36. public String data;
  37.  
  38. public Token(TokenType type, String data) {
  39. this.type = type;
  40. this.data = data;
  41. }
  42.  
  43. @Override
  44. public String toString() {
  45. return String.format("(%s %s)", type.name(), data);
  46. }
  47. }
  48.  
  49. public static ArrayList<Token> lex(String input) {
  50. ArrayList<Token> tokens = new ArrayList<Token>();
  51. StringBuffer tokenPatternsBuffer = new StringBuffer();
  52. for (TokenType tokenType : TokenType.values())
  53. tokenPatternsBuffer.append(String.format("|(?<%s>%s)", tokenType.name(), tokenType.pattern));
  54. Pattern tokenPatterns = Pattern.compile(new String(tokenPatternsBuffer.substring(1)));
  55.  
  56. Matcher matcher = tokenPatterns.matcher(input);
  57. while (matcher.find()) {
  58. if (matcher.group(TokenType.STRING.name()) != null) {
  59. tokens.add(new Token(TokenType.STRING, matcher.group(TokenType.STRING.name())));
  60. continue;
  61. } else if (matcher.group(TokenType.COMMENT.name()) != null) {
  62. tokens.add(new Token(TokenType.COMMENT, matcher.group(TokenType.COMMENT.name())));
  63. continue;
  64. } else if (matcher.group(TokenType.KEYWORD.name()) != null){
  65. tokens.add(new Token(TokenType.KEYWORD, matcher.group(TokenType.KEYWORD.name())));
  66. continue;
  67. } else if (matcher.group(TokenType.OPERATOR.name()) != null) {
  68. tokens.add(new Token(TokenType.OPERATOR, matcher.group(TokenType.OPERATOR.name())));
  69. continue;
  70. } else if (matcher.group(TokenType.TYPE.name()) != null){
  71. tokens.add(new Token(TokenType.TYPE, matcher.group(TokenType.TYPE.name())));
  72. continue;
  73. } else if (matcher.group(TokenType.NUMBER.name()) != null) {
  74. tokens.add(new Token(TokenType.NUMBER, matcher.group(TokenType.NUMBER.name())));
  75. continue;
  76. } else if (matcher.group(TokenType.IDENTIFIER.name()) != null){
  77. tokens.add(new Token(TokenType.IDENTIFIER, matcher.group(TokenType.IDENTIFIER.name())));
  78. continue;
  79. } else if (matcher.group(TokenType.BRACKET.name()) != null){
  80. tokens.add(new Token(TokenType.BRACKET, matcher.group(TokenType.BRACKET.name())));
  81. continue;
  82. } else if (matcher.group(TokenType.PUNCTUATION.name()) != null) {
  83. tokens.add(new Token(TokenType.PUNCTUATION, matcher.group(TokenType.PUNCTUATION.name())));
  84. continue;
  85. } else if (matcher.group(TokenType.UNEXPECTED.name()) != null) {
  86. tokens.add(new Token(TokenType.UNEXPECTED, matcher.group(TokenType.UNEXPECTED.name())));
  87. continue;
  88. } else if (matcher.group(TokenType.WHITESPACE.name()) != null)
  89. continue;
  90. }
  91.  
  92. return tokens;
  93. }
  94.  
  95. public static void main(String[] args) {
  96. // System.out.println("Вставьте код в текстовый документ input.txt");
  97. String c = new String();
  98. StringBuilder in = new StringBuilder();
  99. try{
  100. c1=new BufferedReader(new FileReader("C:\\Users\\Максим\\Desktop\\input.txt"));
  101. while ((c = c1.readLine())!=null){
  102. in.append(c);
  103. }
  104. }
  105. catch(IOException ex){
  106. System.out.println(ex.getMessage());
  107. }
  108. ArrayList<Token> tokens = lex(in.toString());
  109. System.out.println();
  110. for (Token token: tokens) {
  111. System.out.println(token);
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement