Advertisement
golim22

Untitled

Dec 10th, 2017
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <locale>
  6. #include <unordered_set>
  7.  
  8. using namespace std;
  9.  
  10. unordered_set<string> keywords = {
  11. "procedure",
  12. "real",
  13. "var",
  14. "string",
  15. "begin",
  16. "if",
  17. "then",
  18. "else",
  19. "delete",
  20. "end",
  21. };
  22.  
  23. unordered_set<string> delims = {
  24. ",",
  25. "+",
  26. ":",
  27. ")",
  28. "(",
  29. ";",
  30. "[",
  31. "]",
  32. "<",
  33. ">",
  34. "'",
  35.  
  36. };
  37.  
  38. enum type
  39. {
  40. keyword,
  41. delim,
  42. delim2,
  43. ident,
  44. number,
  45. str,
  46. error,
  47. };
  48.  
  49. struct Lexem {
  50. Lexem(const string &value, type type)
  51. : value(value)
  52. , type(type)
  53. {}
  54. string value;
  55. type type;
  56. };
  57.  
  58. int main()
  59. {
  60. vector<Lexem> lexems;
  61. fstream f("C:\\Users\\Rihards\\Desktop\\compiler\\comp.txt", ios::in);
  62. fstream p("C:\\Users\\Rihards\\Desktop\\compiler\\runtime_output.txt", ios::out);
  63. string temp;
  64. int linecount = 1;
  65. p << "this is start" << endl;
  66. while (!f.eof())
  67. {
  68. while (isspace(f.peek()))
  69. {
  70. if (f.peek() == '\n')
  71. {
  72. linecount++; p << "end of line" << endl;
  73. }
  74. f.get();
  75.  
  76.  
  77. }
  78.  
  79. if (isalpha(f.peek()))
  80. {
  81. do {
  82. temp += tolower(f.get());
  83. } while (isalpha(f.peek()));
  84. type t = ident;
  85.  
  86. if (keywords.find(temp) != keywords.end()) {
  87. t = keyword;
  88. }
  89.  
  90. lexems.push_back(Lexem(temp, t));
  91. p << temp << " line:" << linecount << endl;
  92. }
  93. else if (isdigit(f.peek()))
  94. {
  95. do {
  96. temp += f.get();
  97. } while (isdigit(f.peek()));
  98. lexems.push_back(Lexem(temp, number));
  99. }
  100.  
  101. else //массив делимторов
  102. {
  103. if (f.peek() == ':')
  104. {
  105. type t = delim;
  106. temp += f.get();
  107. if (f.peek() == '=') {
  108. temp += f.get();
  109. t = delim2;
  110. }
  111. lexems.push_back(Lexem(temp, t));
  112. }
  113. else if (f.peek() == '<')
  114. {
  115. type t = delim;
  116. temp += f.get();
  117. if (f.peek() == '>') {
  118. temp += f.get();
  119. t = delim2;
  120. }
  121. lexems.push_back(Lexem(temp, t));
  122. }
  123. else if (f.peek() == '\'')
  124. {
  125. lexems.push_back(Lexem("'", delim));
  126. f.get();
  127. while (f.peek() != '\'')
  128. {
  129. temp += f.get();
  130. }
  131. lexems.push_back(Lexem(temp, str));
  132.  
  133. lexems.push_back(Lexem("'", delim));
  134. f.get();
  135. }
  136. else
  137. {
  138. type t = error;
  139.  
  140. temp += f.get();
  141. if (delims.find(temp) != delims.end()) {
  142. t = delim;
  143. //cout << "We found error! Line:" << linecount << " UNKNOWN SIMBOL!!!!" << endl;
  144. }
  145. lexems.push_back(Lexem(temp, t));
  146.  
  147. }
  148.  
  149. }
  150.  
  151. temp.clear();
  152. }
  153.  
  154.  
  155.  
  156. for (int i = 0; i < lexems.size(); i++) {
  157. cout << lexems[i].type << '\t' << lexems[i].value << endl;
  158. }
  159. cout << "0=keywords, 1=delimiters, 2=double delimiters, 3=identificator, 4=constants, 5=strings, 6=errors" << endl;
  160. f.close(); //закрывать файловую переменную. файл нам еще пригодится!
  161.  
  162. for (int i = 0; i < lexems.size(); i++) {
  163. if (lexems[i].type == 3)
  164. cout << lexems[i].type << '\t' << lexems[i].value << endl;
  165. }
  166.  
  167.  
  168. cout << "___________________" << endl;
  169. for (int i = 0; i < lexems.size(); i++) {
  170. if (lexems[i].type == 4)
  171. cout << lexems[i].type << '\t' << lexems[i].value << endl;
  172. }
  173.  
  174. cout << "___________________" << endl;
  175. for (int i = 0; i < lexems.size(); i++) {
  176. if (lexems[i].type == 5)
  177. cout << lexems[i].type << '\t' << lexems[i].value << endl;
  178. }
  179.  
  180.  
  181.  
  182.  
  183. system("PAUSE");
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement