aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-07-14 13:24:52 -0400
committerTucker Evans <tuckerevans24@gmail.com>2019-07-14 13:24:52 -0400
commit9a71e795dc14c401e501125865b2a15fe59f617b (patch)
tree5d5c51d36089cf18c70084d77600dbe5366b1b67
parenta4f0f79d1dd02121ab1600a9f98941de0d07efcf (diff)
Add parse tree
-rw-r--r--tree.c118
-rw-r--r--tree.h27
2 files changed, 145 insertions, 0 deletions
diff --git a/tree.c b/tree.c
new file mode 100644
index 0000000..25c7f7e
--- /dev/null
+++ b/tree.c
@@ -0,0 +1,118 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "tree.h"
+#include "y.tab.h"
+
+extern void yyerror(char*);
+
+/* parse tree funcs */
+ptree* mktree(type, l, r)
+int type;
+ptree *l, *r;
+{
+ ptree *t = (ptree*)malloc(sizeof(ptree));
+ assert(t);
+
+ t->type = type;
+ t->l = l;
+ t->r = r;
+
+ return t;
+}
+
+ptree* mkid(str)
+char *str;
+{
+ ptree *p = mktree(ID, NULL, NULL);
+ p->attr.sval = strdup(str); /* memory leak? double strdup*/
+ return p;
+}
+
+ptree* mkinum(n)
+int n;
+{
+ ptree *p = mktree(INUM, NULL, NULL);
+ p->attr.ival = n;
+ return p;
+}
+
+ptree* mkrnum(n)
+float n;
+{
+ ptree *p = mktree(RNUM, NULL, NULL);
+ p->attr.rval = n;
+ return p;
+}
+
+ptree * mkop(type, sub, l, r)
+int type, sub;
+ptree *l, *r;
+{
+ ptree *p = mktree(type, l, r);
+ p->attr.opval = sub;
+ return p;
+}
+
+void print_tree(t)
+ptree *t;
+{
+ fprintf(stderr, "\n\n");
+ aux_tree_print(t, 0);
+ return;
+}
+
+void aux_tree_print(t, spaces)
+ptree* t;
+int spaces;
+{
+ int i;
+ if ( t ) {
+ for (i = 0; i < spaces; i++)
+ fprintf(stderr," ");
+ switch (t->type) {
+
+ case ADDOP:
+ fprintf(stderr, "[ADDOP]");
+ break;
+ case MULOP:
+ fprintf(stderr, "[MULOP]");
+ break;
+ case RELOP:
+ fprintf(stderr, "[RELOP]");
+ break;
+ case NOT:
+ fprintf(stderr, "[NOT]");
+ break;
+ case ARRAY_ACCESS:
+ fprintf(stderr, "[ARRAY ACCESS]");
+ break;
+ case LIST:
+ fprintf(stderr, "[LIST]");
+ break;
+ case ID:
+ fprintf(stderr, "[ID: %s]", t->attr.sval);
+ break;
+ case INUM:
+ fprintf(stderr, "[INUM: %d]", t->attr.ival);
+ break;
+ case RNUM:
+ fprintf(stderr, "[RNUM: %f]", t->attr.rval);
+ break;
+ case ASSIGNOP:
+ fprintf(stderr, "[ASSIGN]");
+ break;
+ default:
+ fprintf(stderr, "\t%d", t->type);
+ yyerror("Error in tree_print");
+ }
+ fprintf(stderr,"\n");
+ aux_tree_print(t->l, spaces + 2);
+ fprintf(stderr,"\n");
+ aux_tree_print(t->r, spaces + 2);
+ fprintf(stderr,"\n");
+ }
+
+}
diff --git a/tree.h b/tree.h
new file mode 100644
index 0000000..b7349af
--- /dev/null
+++ b/tree.h
@@ -0,0 +1,27 @@
+#ifndef TREE_H
+#define TREE_H
+
+typedef struct parse_tree {
+ int type;
+ union {
+ int ival; /* NUM */
+ float rval; /* RNUM */
+ char *sval; /* ID */
+ int opval; /* RELOP: LT LE GT GE EQ NE
+ ADDOP: PLUS MINUS OR
+ MULOP: MUL DIV
+ */
+ } attr;
+ struct parse_tree *l, *r;
+} ptree;
+
+void aux_tree_print(ptree*, int);
+void print_tree(ptree*);
+
+ptree* mktree(int, ptree*, ptree*);
+ptree* mkid(char*);
+ptree* mkinum(int);
+ptree* mkrnum(float);
+ptree* mkop(int, int, ptree*, ptree*);
+
+#endif