Advertisement
Guest User

prog 3 parsetree.h 2

a guest
Nov 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. /*
  2. * parsetree.h
  3. */
  4.  
  5. #ifndef PARSETREE_H_
  6. #define PARSETREE_H_
  7.  
  8. #include <vector>
  9. #include <map>
  10. using std::vector;
  11. using std::map;
  12.  
  13. // NodeType represents all possible types
  14. enum NodeType { ERRTYPE, INTTYPE, STRTYPE, BOOLTYPE, IDENTTYPE };
  15.  
  16. // a "forward declaration" for a class to hold values
  17. class Value;
  18.  
  19. class ParseTree {
  20. int linenum;
  21. ParseTree *left;
  22. ParseTree *right;
  23.  
  24. public:
  25. ParseTree(int linenum, ParseTree *l = 0, ParseTree *r = 0)
  26. : linenum(linenum), left(l), right(r) {}
  27.  
  28. virtual ~ParseTree() {
  29. delete left;
  30. delete right;
  31. string GetID;
  32. }
  33.  
  34. int GetLinenum() const { return linenum; }
  35.  
  36. virtual NodeType GetType() const { return ERRTYPE; }
  37.  
  38. int LeafCount() const {
  39. int lc = 0;
  40. if( left ) lc += left->LeafCount();
  41. if( right ) lc += right->LeafCount();
  42. if( left == 0 && right == 0 )
  43. lc++;
  44. return lc;
  45. }
  46.  
  47. // other methods
  48. int IdentCount(map<string, int> *ident) const{
  49. int ic = 0;
  50. //map<string, int> *ident;
  51. if( left ) ic += left->IdentCount(ident);
  52. if( right ) ic += right->IdentCount(ident);
  53. if( (*this).GetType() == IDENTTYPE ){
  54. ic++;
  55. string id = this->GetID();
  56. (*ident)[id]++;
  57. }
  58. return ic;
  59. }
  60.  
  61. int StringCount() const{
  62. int sc = 0;
  63. if( left ) sc += left->StringCount();
  64. if( right ) sc += right->StringCount();
  65. if( (*this).GetType() == STRTYPE )
  66. sc++;
  67. return sc;
  68. }
  69.  
  70. virtual string GetID() const { return "";}
  71.  
  72. //map<NodeType, int> identMap;
  73. };
  74.  
  75. class StmtList : public ParseTree {
  76.  
  77. public:
  78. StmtList(ParseTree *l, ParseTree *r) : ParseTree(0, l, r) {}
  79.  
  80. };
  81.  
  82. class IfStatement : public ParseTree {
  83. public:
  84. IfStatement(int line, ParseTree *ex, ParseTree *stmt) : ParseTree(line, ex, stmt) {}
  85. };
  86.  
  87. class Assignment : public ParseTree {
  88. public:
  89. Assignment(int line, ParseTree *lhs, ParseTree *rhs) : ParseTree(line, lhs, rhs) {}
  90. };
  91.  
  92. class PrintStatement : public ParseTree {
  93. public:
  94. PrintStatement(int line, ParseTree *e) : ParseTree(line, e) {}
  95. };
  96.  
  97. class PlusExpr : public ParseTree {
  98. public:
  99. PlusExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  100. };
  101.  
  102. class MinusExpr : public ParseTree {
  103. public:
  104. MinusExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  105. };
  106.  
  107. class TimesExpr : public ParseTree {
  108. public:
  109. TimesExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  110. };
  111.  
  112. class DivideExpr : public ParseTree {
  113. public:
  114. DivideExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  115. };
  116.  
  117. class LogicAndExpr : public ParseTree {
  118. public:
  119. LogicAndExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  120. };
  121.  
  122. class LogicOrExpr : public ParseTree {
  123. public:
  124. LogicOrExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  125. };
  126.  
  127. class EqExpr : public ParseTree {
  128. public:
  129. EqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  130. };
  131.  
  132. class NEqExpr : public ParseTree {
  133. public:
  134. NEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  135. };
  136.  
  137. class LtExpr : public ParseTree {
  138. public:
  139. LtExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  140. };
  141.  
  142. class LEqExpr : public ParseTree {
  143. public:
  144. LEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  145. };
  146.  
  147. class GtExpr : public ParseTree {
  148. public:
  149. GtExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  150. };
  151.  
  152. class GEqExpr : public ParseTree {
  153. public:
  154. GEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  155. };
  156.  
  157. class IConst : public ParseTree {
  158. int val;
  159.  
  160. public:
  161. IConst(int l, int i) : ParseTree(l), val(i) {}
  162. IConst(Token& t) : ParseTree(t.GetLinenum()) {
  163. val = stoi(t.GetLexeme());
  164. }
  165.  
  166. NodeType GetType() const { return INTTYPE; }
  167. };
  168.  
  169. class BoolConst : public ParseTree {
  170. bool val;
  171.  
  172. public:
  173. BoolConst(Token& t, bool val) : ParseTree(t.GetLinenum()), val(val) {}
  174.  
  175. NodeType GetType() const { return BOOLTYPE; }
  176. };
  177.  
  178. class SConst : public ParseTree {
  179. string val;
  180.  
  181. public:
  182. SConst(Token& t) : ParseTree(t.GetLinenum()) {
  183. val = t.GetLexeme();
  184. }
  185.  
  186. NodeType GetType() const { return STRTYPE; }
  187. };
  188.  
  189. class Ident : public ParseTree {
  190. string id;
  191.  
  192. public:
  193. Ident(Token& t) : ParseTree(t.GetLinenum()), id(t.GetLexeme()) {}
  194.  
  195. NodeType GetType() const { return IDENTTYPE; }
  196.  
  197. string GetID() const { return id; }
  198. };
  199.  
  200. #endif /* PARSETREE_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement