Advertisement
ShrekOP

Assg2

Dec 16th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<vector<string>> CODE, INTCODE, MNT, MDT, FVPPL, AVPPL;
  4. void toVector(string filename) {
  5. string data, word;
  6. ifstream file(filename);
  7. while (getline(file, data)) {
  8. string line = " ";
  9. for (int i = 0; i < data.length(); i++) {
  10. if (data[i] == ';')
  11. break;
  12. else if (data[i] == ',')
  13. line += ' ';
  14. else line += data[i];
  15. }
  16. istringstream iss(line);
  17. vector<string> temp;
  18. for (string word; iss >> word;)
  19. {
  20. temp.push_back(word);
  21. }
  22. CODE.push_back(temp);
  23. }
  24. file.close();
  25. }
  26. void showTable(vector<vector<string>> table, string name, bool isIC = false)
  27. {
  28. cout << "\n-------------------------------------" << endl;
  29. cout << " " << name << endl;
  30. cout << "-------------------------------------\n"
  31. << endl;
  32. for (int i = 0; i < table.size(); i++)
  33. {
  34. for (int j = 0; j < table[i].size(); j++)
  35. {
  36. if (isIC)
  37. {
  38. if (j == 0 || j == table[i].size() - 1)
  39. cout << table[i][j] << " ";
  40. else
  41. cout << table[i][j] << ", ";
  42. }
  43. else
  44. cout << table[i][j] << "\t";
  45. }
  46. cout << endl;
  47. }
  48. cout << endl;
  49. }
  50. int getRowIndex(string str, vector<vector<string>> table)
  51. {
  52. for (int i = 0; i < table.size(); i++)
  53. if (str == table[i][0])
  54. return i;
  55. return -1;
  56. }
  57. int getColIndex(int row_id, string str, vector<vector<string>> table)
  58. {
  59. for (int i = 0; i < table[row_id].size(); i++)
  60. if (str == table[row_id][i])
  61. return i;
  62. return -1;
  63. }
  64. void genTAB()
  65. {
  66. int mnt_index = 1;
  67. for (int i = 0; i < CODE.size(); i++)
  68. {
  69. vector<string> temp;
  70. string macro;
  71. if (CODE[i][0] == "MACRO")
  72. {
  73. macro = CODE[i][1];
  74. temp.push_back(CODE[i][1]);
  75. temp.push_back(to_string(CODE[i].size() - 2));
  76. temp.push_back(to_string(mnt_index));
  77. MNT.push_back(temp);
  78. temp.clear();
  79. temp = CODE[i];
  80. temp.erase(temp.begin());
  81. FVPPL.push_back(temp);
  82. temp.clear();
  83. while (CODE[i][0] != "MEND")
  84. {
  85. i++;
  86. temp.clear();
  87. for (int j = 0; j < CODE[i].size(); j++)
  88. {
  89. int arg_id = getColIndex(getRowIndex(macro, FVPPL), CODE[i][j], FVPPL);
  90. if (arg_id >= 0)
  91. temp.push_back("#" + to_string(arg_id));
  92. else
  93. temp.push_back(CODE[i][j]);
  94. }
  95. if (getRowIndex(CODE[i][0], FVPPL) > -1)
  96. {
  97. temp.clear();
  98. temp = CODE[i];
  99. AVPPL.push_back(temp);
  100. }
  101. MDT.push_back(temp);
  102. mnt_index++;
  103. }
  104. }
  105. else
  106. {
  107. INTCODE.push_back(CODE[i]);
  108. }
  109. }
  110. }
  111. int main()
  112. {
  113. toVector("input.asm");
  114. genTAB();
  115. showTable(INTCODE, "Intermediate Code (IC)", true);
  116. showTable(MNT, "Macro Names Table (MNT)");
  117. showTable(MDT, "Macro Definition Table (MDT)");
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement