+ Node *node = (Node *) malloc (sizeof (Node));
+ node->nterm.isNterm = 1;
+ node->nterm.parents = 0;
+ node->nterm.form = form;
+ node->nterm.children[0] = child0;
+ if (child0)
+ child0->nodeInfo.parents += 1;
+ node->nterm.children[1] = child1;
+ if (child1)
+ child1->nodeInfo.parents += 1;
+ node->nterm.children[2] = child2;
+ if (child2)
+ child2->nodeInfo.parents += 1;
+ return node;
+}
+
+static Node *
+new_term (char *text)
+{
+ Node *node = (Node *) malloc (sizeof (Node));
+ node->term.isNterm = 0;
+ node->term.parents = 0;
+ node->term.text = text;
+ return node;
+}
+
+static void
+free_node (Node *node)
+{
+ if (!node)
+ return;
+ node->nodeInfo.parents -= 1;
+ /* Free only if 0 (last parent) or -1 (no parents). */
+ if (node->nodeInfo.parents > 0)
+ return;
+ if (node->nodeInfo.isNterm == 1)
+ {
+ free_node (node->nterm.children[0]);
+ free_node (node->nterm.children[1]);
+ free_node (node->nterm.children[2]);
+ }
+ else
+ free (node->term.text);
+ free (node);
+}
+
+static char *
+node_to_string (Node *node)
+{
+ char *child0;
+ char *child1;
+ char *child2;
+ char *buffer;
+ if (!node)
+ {
+ buffer = (char *) malloc (1);
+ buffer[0] = 0;
+ }
+ else if (node->nodeInfo.isNterm == 1)
+ {
+ child0 = node_to_string (node->nterm.children[0]);
+ child1 = node_to_string (node->nterm.children[1]);
+ child2 = node_to_string (node->nterm.children[2]);
+ buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
+ + strlen (child1) + strlen (child2) + 1);
+ sprintf (buffer, node->nterm.form, child0, child1, child2);
+ free (child0);
+ free (child1);
+ free (child2);
+ }
+ else
+ buffer = strdup (node->term.text);
+ return buffer;