1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef PARSETREE_HPP
#define PARSETREE_HPP
 
#include <fstream>
#include <string>
#include <vector>
using namespace std;
 
#define YYSTYPE ParseTree*
 
class ParseTree {
 private:
  string label;
  vector<ParseTree*> children;
 
  int addToDot(ofstream& fout, int& nodes) {
    int root = ++nodes;
    fout << "\tn" << root << " [label=\"" << label << "\"];" << endl;
    for (vector<ParseTree*>::iterator iter = children.begin();
      iter != children.end(); ++iter)
    {
      int child = (*iter)->addToDot(fout,nodes);
      fout << "\tn" << root << " -> n" << child << ";" << endl;
    }
    return root;
  }
 
 public:
  ParseTree (const char* l) :label(l), children(0) { }
  ParseTree (const char* l, ParseTree* c1) :label(l), children(1) {
    children[0] = c1;
  }
  ParseTree (const char* l, ParseTree* c1, ParseTree* c2) 
    :label(l), children(2) 
  {
    children[0] = c1;
    children[1] = c2;
  }
  ParseTree (const char* l, ParseTree* c1, ParseTree* c2, ParseTree* c3) 
    :label(l), children(3) 
  {
    children[0] = c1;
    children[1] = c2;
    children[2] = c3;
  }
  ParseTree (const char* l, 
    ParseTree* c1, ParseTree* c2, ParseTree* c3, ParseTree* c4) 
    :label(l), children(4) 
  {
    children[0] = c1;
    children[1] = c2;
    children[2] = c3;
    children[3] = c4;
  }
 
  ~ParseTree() {
    for (vector<ParseTree*>::iterator iter = children.begin();
      iter != children.end(); ++iter)
    {
      delete *iter;
    }
    children.clear();
  }
 
  void addChild(ParseTree* c) {
    children.push_back(c);
  }
 
  bool isLeaf() {
    return children.empty();
  }
 
  void writeDot(const char* fname) {
    ofstream fout(fname);
    int nodes = 0;
    fout << "digraph g {" << endl;
    addToDot(fout,nodes);
    fout << "}" << endl;
    fout.close();
  }
};
 
extern ParseTree pt;
 
#endif //PARSETREE_HPP