Advertisement
golim22

Untitled

Dec 11th, 2017
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 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, int line)
  51. : value(value)
  52. , type(type)
  53. , line(line)
  54. {}
  55. string value;
  56. type type;
  57. int line;
  58. };
  59.  
  60. int main()
  61. {
  62. vector<Lexem> lexems;
  63. fstream f("C:\\Users\\Rihards\\Desktop\\compiler\\comp.txt", ios::in);
  64. fstream p("C:\\Users\\Rihards\\Desktop\\compiler\\runtime_output.txt", ios::out);
  65. string temp;
  66. int linecount = 1;
  67. p << "this is start" << endl;
  68. while (!f.eof())
  69. {
  70. while (isspace(f.peek()))
  71. {
  72. if (f.peek() == '\n')
  73. {
  74. linecount++; p << "end of line" << endl;
  75. }
  76. f.get();
  77.  
  78.  
  79. }
  80.  
  81. if (f.eof())
  82. break;
  83.  
  84. if (isalpha(f.peek()))
  85. {
  86. do {
  87. temp += tolower(f.get());
  88. } while (isalpha(f.peek()));
  89. type t = ident;
  90.  
  91. if (keywords.find(temp) != keywords.end()) {
  92. t = keyword;
  93. }
  94.  
  95. lexems.push_back(Lexem(temp, t, linecount));
  96. p << temp << " line:" << linecount << endl;
  97. }
  98. else if (isdigit(f.peek()))
  99. {
  100. do {
  101. temp += f.get();
  102. } while (isdigit(f.peek()));
  103. lexems.push_back(Lexem(temp, number, linecount));
  104. }
  105.  
  106. else //массив делимторов
  107. {
  108. if (f.peek() == ':')
  109. {
  110. type t = delim;
  111. temp += f.get();
  112. if (f.peek() == '=') {
  113. temp += f.get();
  114. t = delim2;
  115. }
  116. lexems.push_back(Lexem(temp, t, linecount));
  117. }
  118. else if (f.peek() == '<')
  119. {
  120. type t = delim;
  121. temp += f.get();
  122. if (f.peek() == '>') {
  123. temp += f.get();
  124. t = delim2;
  125. }
  126. lexems.push_back(Lexem(temp, t, linecount));
  127. }
  128. else if (f.peek() == '\'')
  129. {
  130. lexems.push_back(Lexem("'", delim, linecount));
  131. f.get();
  132. while (f.peek() != '\'')
  133. {
  134. temp += f.get();
  135. }
  136. lexems.push_back(Lexem(temp, str, linecount));
  137.  
  138. lexems.push_back(Lexem("'", delim, linecount));
  139. f.get();
  140. }
  141. else
  142. {
  143. type t = error;
  144.  
  145. temp += f.get();
  146. if (delims.find(temp) != delims.end()) {
  147. t = delim;
  148.  
  149. }
  150. else {
  151. p << "We found error! Line:" << linecount << " UNKNOWN SIMBOL!!!!" << temp << endl;
  152. }
  153. lexems.push_back(Lexem(temp, t, linecount));
  154.  
  155.  
  156. }
  157.  
  158. }
  159.  
  160. temp.clear();
  161. }
  162.  
  163.  
  164.  
  165. for (int i = 0; i < lexems.size(); i++) {
  166. cout << lexems[i].type << '\t' << lexems[i].value << endl;
  167. }
  168. cout << "0=keywords, 1=delimiters, 2=double delimiters, 3=identificator, 4=constants, 5=strings, 6=errors" << endl;
  169. f.close(); //закрывать файловую переменную. файл нам еще пригодится!
  170.  
  171. for (int i = 0; i < lexems.size(); i++) {
  172. if (lexems[i].type == 3)
  173. cout << lexems[i].type << '\t' << lexems[i].value << endl;
  174. }
  175.  
  176.  
  177. cout << "___________________" << endl;
  178. for (int i = 0; i < lexems.size(); i++) {
  179. if (lexems[i].type == 4)
  180. cout << lexems[i].type << '\t' << lexems[i].value << endl;
  181. }
  182.  
  183. cout << "___________________" << endl;
  184. for (int i = 0; i < lexems.size(); i++) {
  185. if (lexems[i].type == 5)
  186. cout << lexems[i].type << '\t' << lexems[i].value << endl;
  187. }
  188.  
  189. cout << "_________UNKNOWN SYMBOLS__________" << endl;
  190. for (int i = 0; i < lexems.size(); i++) {
  191. if (lexems[i].type == 6)
  192. cout << lexems[i].type << '\t' << lexems[i].line << '\t' << lexems[i].value << endl;
  193. }
  194.  
  195.  
  196. system("PAUSE");
  197. return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement