Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. const set<string> terminals{"BOF", "BECOMES", "COMMA", "ELSE", "EOF", "EQ",
  10. "GE", "GT", "ID", "IF", "INT", "LBRACE", "LE", "LPAREN", "LT", "MINUS",
  11. "NE", "NUM", "PCT", "PLUS", "PRINTLN", "RBRACE", "RETURN", "RPAREN", "SEMI",
  12. "SLASH", "STAR", "WAIN", "WHILE", "AMP", "LBRACK", "RBRACK", "NEW", "DELETE",
  13. "NULL"};
  14.  
  15. map<string, string> symbolTable;
  16.  
  17. struct Node {
  18. vector<string> derivationRule;
  19. int numChildren;
  20. vector<Node*> children;
  21. Node(vector<string>& rules, int numChildren) : derivationRule(rules), numChildren(numChildren) {}
  22. };
  23.  
  24. Node* constructTree() {
  25. int numChildren = 0;
  26. vector<string> rule;
  27. string line;
  28. string token;
  29.  
  30. getline(cin, line);
  31.  
  32. istringstream str(line);
  33.  
  34. while (str >> token) {
  35. rule.emplace_back(token);
  36. numChildren++;
  37. }
  38.  
  39. numChildren--;
  40.  
  41. Node* n = new Node(rule, numChildren);
  42.  
  43. if (terminals.count(rule[0]) == 0 && numChildren) { // check if non terminal and has children
  44. for (int i = 0; i < numChildren; i++) {
  45. n->children.emplace_back(constructTree());
  46. }
  47. }
  48. return n;
  49. }
  50.  
  51.  
  52. void checkTree(Node* n) {
  53. // IDs are derived from [dcl, factor, lvalue]
  54. if ((n->derivationRule)[0] == "dcl") {
  55.  
  56. string type;
  57.  
  58. if ((n->children[0]->derivationRule).size() == 2) {
  59. type = (n->children[0]->children[0])->derivationRule[1];
  60. }
  61.  
  62. else if ((n->children[0]->derivationRule).size() > 2 &&
  63. (n->children[0]->children[0])->derivationRule[2] == "STAR") {
  64. type = "int*";
  65. }
  66.  
  67. string ID = (n->children[1]->derivationRule)[1];
  68.  
  69. if (symbolTable.count(ID) > 0) {
  70. cerr << "ERROR: variable name already in use" << endl;
  71. return;
  72. }
  73. else {
  74. symbolTable[ID] = type;
  75. }
  76. }
  77. else if ((n->derivationRule)[0] == "lvalue" && (n->derivationRule)[1] == "ID") {
  78.  
  79.  
  80. if (symbolTable.count(ID) == 0) {
  81. cerr << "ERROR: variable has not been inialized" << endl;
  82. return;
  83. }
  84. }
  85.  
  86. else if ((n->derivationRule)[0] == "factor" && (n->derivationRule)[1] == "ID") {
  87.  
  88. string ID = (n->children[0]->derivationRule)[1];
  89.  
  90. if (symbolTable.count(ID) == 0) {
  91. cerr << "ERROR: variable has not been inialized" << endl;
  92. return;
  93. }
  94. }
  95.  
  96. for (auto a : n->children) {
  97. checkTree(a);
  98. }
  99. }
  100.  
  101. void treeDestroy(Node* n) {
  102. if (n->children.size() == 0) {
  103. delete n;
  104. return;
  105. }
  106. for (int i = 0; i < n->children.size(); i++) {
  107. treeDestroy(n->children[i]);
  108. }
  109. delete n;
  110. }
  111.  
  112. int main() {
  113. Node* tree = constructTree();
  114.  
  115. checkTree(tree);
  116.  
  117. if (tree->children.size() == 0) {
  118. delete tree;
  119. return;
  120. }
  121. for (int i = 0; i < tree->children.size(); i++) {
  122. treeDestroy(tree->children[i]);
  123. }
  124. delete tree;
  125.  
  126. cerr << "wain" << endl;
  127.  
  128. for (auto it : symbolTable) {
  129. cerr << it.first << " " << it.second << endl;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement