Advertisement
golim22

Untitled

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