diff --git a/exercises/02/TDT4205_PS2_oyvindps.pdf b/exercises/02/TDT4205_PS2_oyvindps.pdf new file mode 100644 index 0000000..78bd121 Binary files /dev/null and b/exercises/02/TDT4205_PS2_oyvindps.pdf differ diff --git a/exercises/02/vslc/src/parser.y b/exercises/02/vslc/src/parser.y index 3df5fde..ab5b7bd 100644 --- a/exercises/02/vslc/src/parser.y +++ b/exercises/02/vslc/src/parser.y @@ -13,6 +13,7 @@ %left '*' '/' %nonassoc UMINUS %right '~' +%expect 1 %nonassoc IF THEN %nonassoc ELSE diff --git a/exercises/02/vslc/src/tree.c b/exercises/02/vslc/src/tree.c index 25aab7a..4372474 100644 --- a/exercises/02/vslc/src/tree.c +++ b/exercises/02/vslc/src/tree.c @@ -5,6 +5,50 @@ static void node_finalize ( node_t *discard ); static void destroy_subtree ( node_t *discard ); +typedef struct stem_t *stem; +struct stem_t { const char *str; stem next; }; +static void +tree_print(node_t* root, stem head) +{ + static const char *sdown = " │", *slast = " └", *snone = " "; + struct stem_t col = {0, 0}, *tail; + + for (tail = head; tail; tail = tail->next) { + if (!tail->next) { + if (!strcmp(sdown, tail->str)) + printf(" ├"); + else + printf("%s", tail->str); + break; + } + printf("%s", tail->str); + } + + printf("──%s", node_string[root->type] ); + if ( root->type == IDENTIFIER_DATA || + root->type == STRING_DATA || + root->type == EXPRESSION ) + printf("(%s)", (char *) root->data); + else if (root->type == NUMBER_DATA) + printf ( "(%ld)", *((int64_t *)root->data) ); + putchar ( '\n' ); + + if (!root->n_children) return; + + if (tail && tail->str == slast) + tail->str = snone; + + if (!tail) tail = head = &col; + else tail->next = &col; + + for ( int64_t i=0; i < root->n_children; i++ ) { + col.str = root->n_children - i - 1 ? sdown : slast; + tree_print(root->children[i], head); + } + tail->next = 0; +} + + /* External interface */ void destroy_syntax_tree ( void ) @@ -16,7 +60,8 @@ destroy_syntax_tree ( void ) void print_syntax_tree ( void ) { - node_print ( root, 0 ); + //node_print ( root, 0 ); + tree_print(root, 0); }