diff --git a/cparse.y b/cparse.y index 276c678..f23450e 100644 --- a/cparse.y +++ b/cparse.y @@ -26,7 +26,7 @@ %type array {item_list *} source(A) ::= deflist(B). { A = B; } -deflist(A) ::= deflist(B) def(C). { B->next = C; A = B; } +deflist(A) ::= deflist(B) def(C). { C->next = B; A = C; } deflist(A) ::= def(B). { A = B; } def(A) ::= struct(B). { A = B; } def(A) ::= union(B). { A = B; } diff --git a/tree.c b/tree.c index c64d6b7..fa5bf62 100644 --- a/tree.c +++ b/tree.c @@ -93,14 +93,32 @@ item_list* new_array_node(item_list *ctx, char* name, short type, short sign, sh return tmp; } +/* Do i need reverse "defs" in normal order? */ + +item_list* list_reverse(item_list *defs) +{ + item_list *t; + item_list *q = defs; + item_list *p = NULL; + while (q != NULL) { + t = q->next; + q->next = p; + p = q; + q = t; + } + return p; +} + item_list* new_struct_node(item_list *ctx, char* name, item_list *defs) { + item_list *itms; + itms = list_reverse(defs); printf("Members are:\n"); - print_tree(defs); + print_tree(itms); struct item_struct *istr = (struct item_struct *)malloc(sizeof(struct item_struct)); item_list *tmp = (item_list *)malloc(sizeof(item_list)); istr->name = name; - istr->items = defs; + istr->items = itms; tmp->next = NULL; tmp->item_type = ITEM_STRUCT; tmp->item.str = istr; @@ -110,12 +128,14 @@ item_list* new_struct_node(item_list *ctx, char* name, item_list *defs) item_list* new_union_node(item_list *ctx, char* name, item_list *defs) { + item_list *itms; + itms = list_reverse(defs); printf("Members are:\n"); - print_tree(defs); + print_tree(itms); struct item_union *iun = (struct item_union *)malloc(sizeof(struct item_union)); item_list *tmp = (item_list *)malloc(sizeof(item_list)); iun->name = name; - iun->items = defs; + iun->items = itms; tmp->next = NULL; tmp->item_type = ITEM_UNION; tmp->item.un = iun;