Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include "lex.h"
  9.  
  10. using namespace std;
  11.  
  12. int main(int argc, char *argv[]) {
  13. bool print_all_token = false;
  14. bool print_all_constants = false;
  15. bool print_all_ids = false;
  16. bool rigged_filename_pos = false;
  17. string filename = "";
  18. filename.clear();
  19.  
  20.  
  21. for (int i = 1; i < argc; i++) {
  22. if (rigged_filename_pos && argv[i][0] == '-') {
  23. cout << "UNRECOGNIZED FLAG " << filename << endl;
  24. return 1;
  25. }
  26.  
  27.  
  28. if (i == argc - 1) {
  29. if (argv[i][0] != '-') {
  30. if (!rigged_filename_pos) {
  31. rigged_filename_pos = true;
  32. filename = string(argv[i]);
  33. continue;
  34. } else {
  35. cout << "ONLY ONE FILE NAME ALLOWED" << endl;
  36. return 0;
  37. }
  38. }
  39. } else {
  40. if (argv[i][0] != '-') {
  41. if (!rigged_filename_pos) {
  42. rigged_filename_pos = true;
  43. filename = string(argv[i]);
  44. continue;
  45. } else {
  46. cout << "ONLY ONE FILE NAME ALLOWED" << endl;
  47. return 0;
  48. }
  49. }
  50. }
  51.  
  52. if (string(argv[i]).compare("-v") == 0)
  53. print_all_token = true;
  54. else if (string(argv[i]).compare("-consts") == 0)
  55. print_all_constants = true;
  56. else if (string(argv[i]).compare("-ids") == 0)
  57. print_all_ids = true;
  58.  
  59. else {
  60.  
  61. cout << "UNRECOGNIZED FLAG " << (argv[i]) << endl;
  62. return 1;
  63. }
  64.  
  65. }
  66.  
  67. istream *istreamp = &(cin);
  68. fstream fs;
  69.  
  70.  
  71. if (rigged_filename_pos) {
  72. fs.open(filename, fstream::in);
  73. if (!fs) {
  74. cout << "CANNOT OPEN " << filename << endl;
  75. return 1;
  76. }
  77.  
  78. istreamp = &fs;
  79. }
  80.  
  81.  
  82. vector<string> ids;
  83. vector<string> strings;
  84. vector<int> ints;
  85. vector<Lex> totals;
  86.  
  87. Lex nextLex;
  88. int linenum = 0;
  89. do {
  90. nextLex = getNextToken(*istreamp, linenum);
  91. totals.push_back(nextLex);
  92. if (nextLex.GetToken() == STR) {
  93. strings.push_back(nextLex.GetLexeme());
  94. } else if (nextLex.GetToken() == INT) {
  95. ints.push_back(atoi(nextLex.GetLexeme().c_str()));
  96. } else if (nextLex.GetToken() == ID) {
  97. ids.push_back(nextLex.GetLexeme());
  98. }
  99.  
  100. if (print_all_token && nextLex.GetToken() != ERR && nextLex.GetToken() != DONE)
  101. cout << nextLex << endl;
  102. else if (nextLex.GetToken() == ERR) {
  103. cout << nextLex << endl;
  104. return 1;
  105. }
  106.  
  107.  
  108. } while (nextLex.GetToken() != ERR && nextLex.GetToken() != DONE);
  109.  
  110. sort(strings.begin(), strings.end());
  111. strings.erase(unique(strings.begin(), strings.end()), strings.end());
  112.  
  113. sort(ints.begin(), ints.end());
  114. ints.erase(unique(ints.begin(), ints.end()), ints.end());
  115.  
  116. sort(ids.begin(), ids.end());
  117. ids.erase(unique(ids.begin(), ids.end()), ids.end());
  118.  
  119. if (!filename.empty())
  120. fs.close();
  121.  
  122. if (print_all_constants) {
  123. if (strings.size() != 0) {
  124. cout << "STRINGS:" << endl;
  125. for (int i = 0; i < strings.size(); i++) {
  126. cout << strings[i] << endl;
  127. }
  128. }
  129.  
  130. if (ints.size() != 0) {
  131. cout << "INTERGERS:"<< endl;
  132. for (int i = 0; i < ints.size(); i++) {
  133. cout << ints[i] << endl;
  134. }
  135. }
  136. }
  137.  
  138. if (print_all_ids) {
  139. if (ids.size() != 0) {
  140. cout << "IDENTIFIERS: ";
  141. for (int i = 0; i < ids.size() - 1; i++) {
  142. cout << ids[i] << ", ";
  143. }
  144. cout << ids[ids.size() - 1] << endl;
  145. }
  146.  
  147. }
  148.  
  149. cout << "Lines: " << to_string(linenum) << endl;
  150. if (linenum != 0) {
  151. cout << "Tokens: " << to_string(totals.size() - 1) << endl;
  152. }
  153.  
  154.  
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement