From 36858486cf0a6feba95d3b8963ee45a751fe761a Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Tue, 30 Mar 2010 13:52:56 +0200 Subject: [PATCH] ag: symbol table refactor --- ag/parser.y | 28 ++++---- ag/symbol_table.c | 178 ++++++++++++++++++++++------------------------ ag/symbol_table.h | 31 ++++---- 3 files changed, 117 insertions(+), 120 deletions(-) diff --git a/ag/parser.y b/ag/parser.y index 7634069..ba76bb6 100644 --- a/ag/parser.y +++ b/ag/parser.y @@ -12,11 +12,11 @@ @autoinh symbols @attributes { char *name; } ID -@attributes { struct symbol_t *fields; struct symbol_t *symbols; } Program -@attributes { struct symbol_t *fields; } Ids Structdef -@attributes { struct symbol_t *pars; } Pars -@attributes { struct symbol_t *symbols; } Funcdef Stats Bool Expr Call Lexpr Field Term Plusterm Malterm Bterm Orterm Exprs -@attributes { struct symbol_t *in_symbols; struct symbol_t *out_symbols; } Stat +@attributes { struct symbol *fields; struct symbol *symbols; } Program +@attributes { struct symbol *fields; } Ids Structdef +@attributes { struct symbol *pars; } Pars +@attributes { struct symbol *symbols; } Funcdef Stats Bool Expr Call Lexpr Field Term Plusterm Malterm Bterm Orterm Exprs +@attributes { struct symbol *in_symbols; struct symbol *out_symbols; } Stat @traversal @postorder t @@ -34,21 +34,21 @@ Program: Funcdef ';' Program | Structdef ';' Program @{ - @i @Program.fields@ = table_merge(@Structdef.fields@, @Program.1.fields@, 1); + @i @Program.fields@ = tab_merge(@Structdef.fields@, @Program.1.fields@, 1); @i @Program.1.symbols@ = @Program.0.symbols@; @} | - @{ @i @Program.fields@ = new_table(); @} + @{ @i @Program.fields@ = new_tab(); @} ; Funcdef: FUNC ID '(' Pars ')' Stats END - @{ @i @Stats.symbols@ = table_merge(@Funcdef.symbols@, @Pars.pars@, 0); @} + @{ @i @Stats.symbols@ = tab_merge(@Funcdef.symbols@, @Pars.pars@, 0); @} | FUNC ID '(' ')' Stats END - @{ @i @Stats.symbols@ = table_merge(@Funcdef.symbols@, new_table(), 0); @} + @{ @i @Stats.symbols@ = tab_merge(@Funcdef.symbols@, new_tab(), 0); @} ; @@ -58,18 +58,18 @@ Structdef: STRUCT Ids END ; Ids: ID Ids - @{ @i @Ids.fields@ = table_add_symbol(@Ids.1.fields@, @ID.name@, SYMBOL_TYPE_FIELD, 1); @} + @{ @i @Ids.fields@ = tab_add_symbol(@Ids.1.fields@, @ID.name@, S_FIELD, 1); @} | - @{ @i @Ids.fields@ = new_table(); @} + @{ @i @Ids.fields@ = new_tab(); @} ; Pars: Pars ',' ID - @{ @i @Pars.pars@ = table_add_symbol(@Pars.1.pars@, @ID.name@, SYMBOL_TYPE_VAR, 0); @} + @{ @i @Pars.pars@ = tab_add_symbol(@Pars.1.pars@, @ID.name@, S_VAR, 0); @} | ID - @{ @i @Pars.pars@ = table_add_symbol(new_table(), @ID.name@, SYMBOL_TYPE_VAR, 0); @} + @{ @i @Pars.pars@ = tab_add_symbol(new_tab(), @ID.name@, S_VAR, 0); @} ; @@ -84,7 +84,7 @@ Stats: Stat ';' Stats Stat: VAR ID ASSIGN Expr @{ - @i @Stat.out_symbols@ = table_add_symbol(clone_table(@Stat.in_symbols@), @ID.name@, SYMBOL_TYPE_VAR, 0); + @i @Stat.out_symbols@ = tab_add_symbol(clone_tab(@Stat.in_symbols@), @ID.name@, S_VAR, 0); @i @Expr.symbols@ = @Stat.in_symbols@; @} diff --git a/ag/symbol_table.c b/ag/symbol_table.c index b28687f..0c2df50 100755 --- a/ag/symbol_table.c +++ b/ag/symbol_table.c @@ -3,149 +3,145 @@ #include #include "symbol_table.h" -struct symbol_t *new_table(void) { - return (struct symbol_t *)NULL; +struct symbol *new_tab(void) +{ + return SYMNULL; } -struct symbol_t *clone_table(struct symbol_t *table) { - struct symbol_t *element; - struct symbol_t *new_tablex; +struct symbol *clone_tab(struct symbol *tab) +{ + struct symbol *elm; + struct symbol *new_tabx; - element=table; - new_tablex=new_table(); - while((struct symbol_t *)NULL!=element) { - /* check return value */ - new_tablex=table_add_symbol(new_tablex,element->identifier,element->type,0); - element=element->next; + elm = tab; + new_tabx = new_tab(); + while(elm != SYMNULL) { + new_tabx = tab_add_symbol(new_tabx, elm->ident, elm->type, 0); + elm = elm->next; } - return new_tablex; + return new_tabx; } -struct symbol_t *table_add_symbol(struct symbol_t *table, char *identifier, short type, short check) { - struct symbol_t *element; - struct symbol_t *new_element; +struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short check) +{ + struct symbol *elm; + struct symbol *new_elm; - if(table_lookup(table,identifier)!=(struct symbol_t *)NULL) { + if(tab_lookup(tab,ident) != SYMNULL) { if(check) { - fprintf(stderr,"Duplicate field %s.\n",identifier); + fprintf(stderr, "Feld doppelt vorhanden: \"%s\"\n", ident); exit(3); } - - table=table_remove_symbol(table,identifier); + tab = tab_remove_symbol(tab, ident); } - new_element=(struct symbol_t *)malloc(sizeof(struct symbol_t)); - new_element->next=(struct symbol_t *)NULL; - new_element->identifier=strdup(identifier); - new_element->type=type; + new_elm = (struct symbol *) malloc(sizeof(struct symbol)); + new_elm->next = SYMNULL; + new_elm->ident = strdup(ident); + new_elm->type = type; - if((struct symbol_t *)NULL==table) { - return new_element; + if(tab == SYMNULL) { + return new_elm; } - element=table; - while((struct symbol_t *)NULL!=element->next) { - element=element->next; + elm = tab; + while(elm->next != SYMNULL) { + elm = elm->next; } - - element->next=new_element; + elm->next = new_elm; - return table; + return tab; } -struct symbol_t *table_lookup(struct symbol_t *table, char *identifier) { - struct symbol_t *element; +struct symbol *tab_lookup(struct symbol *tab, char *ident) +{ + struct symbol *elm = tab; - element=table; - - if((struct symbol_t *)NULL==table) { - return (struct symbol_t *)NULL; + if(tab == SYMNULL) { + return SYMNULL; } - if(strcmp(element->identifier,identifier)==0) { - return element; + if(strcmp(elm->ident, ident) == 0) { + return elm; } - while((struct symbol_t *)NULL!=element->next) { - element=element->next; - if(strcmp(element->identifier,identifier)==0) { - return element; + while(elm->next != SYMNULL) { + elm = elm->next; + if(strcmp(elm->ident, ident) == 0) { + return elm; } } - return (struct symbol_t *)NULL; + return SYMNULL; } -struct symbol_t *table_merge(struct symbol_t *table, struct symbol_t *to_add, short check) { - struct symbol_t *element; - struct symbol_t *new_table=clone_table(table); +struct symbol *tab_merge(struct symbol *tab, struct symbol *to_add, short check) +{ + struct symbol *elm = to_add; + struct symbol *new_tab = clone_tab(tab); - element=to_add; - while(element!=(struct symbol_t *)NULL) { - new_table=table_add_symbol(new_table,element->identifier,element->type,check); - element=element->next; + while(elm != SYMNULL) { + new_tab = tab_add_symbol(new_tab, elm->ident, elm->type, check); + elm = elm->next; } - return new_table; + return new_tab; } -struct symbol_t *table_remove_symbol(struct symbol_t *table, char *identifier) { - struct symbol_t *element; - struct symbol_t *previous_element; - struct symbol_t *new_element; +struct symbol *tab_remove_symbol(struct symbol *tab, char *ident) +{ + struct symbol *elm = tab; + struct symbol *previous_elm = SYMNULL; + struct symbol *new_elm; - if((struct symbol_t *)NULL==table) { - return table; + if(tab == SYMNULL) { + return SYMNULL; } - previous_element=(struct symbol_t *)NULL; - element=table; - - while((struct symbol_t *)NULL!=element) { - if(strcmp(element->identifier,identifier)==0) { - if((struct symbol_t *)NULL==previous_element) { - new_element=element->next; + while(elm != SYMNULL) { + if(strcmp(elm->ident, ident) == 0) { + if(previous_elm == SYMNULL) { + new_elm = elm->next; + } else { + previous_elm->next = elm->next; + new_elm = tab; } - else { - previous_element->next=element->next; - new_element=table; - } - (void)free(element->identifier); - (void)free(element); - return new_element; + (void)free(elm->ident); + (void)free(elm); + return new_elm; } - previous_element=element; - element=element->next; + previous_elm = elm; + elm = elm->next; } - return table; + return tab; } -void check_variable(struct symbol_t *table, char *identifier) { - struct symbol_t *element=table_lookup(table,identifier); - if(element!=(struct symbol_t *)NULL) { - if(element->type!=SYMBOL_TYPE_VAR) { - fprintf(stderr,"Identifier %s not a variable.\n",identifier); +void check_variable(struct symbol *tab, char *ident) +{ + struct symbol *elm = tab_lookup(tab, ident); + if(elm != SYMNULL) { + if(elm->type != S_VAR) { + fprintf(stderr, "Identifier ist keine Variable: \"%s\"\n", ident); exit(3); } - } - else { - fprintf(stderr,"Unknown identifier %s.\n",identifier); + } else { + fprintf(stderr, "Unbekannter Identifier: \"%s\"\n", ident); exit(3); } } -void check_field(struct symbol_t *table, char *identifier) { - struct symbol_t *element=table_lookup(table,identifier); - if(element!=(struct symbol_t *)NULL) { - if(element->type!=SYMBOL_TYPE_FIELD) { - fprintf(stderr,"Identifier %s not a variable.\n",identifier); +void check_field(struct symbol *tab, char *ident) +{ + struct symbol *elm = tab_lookup(tab, ident); + if(elm != SYMNULL) { + if(elm->type != S_FIELD) { + fprintf(stderr, "Identifier ist kein Feld: \"%s\"\n", ident); exit(3); } - } - else { - fprintf(stderr,"Unknown identifier %s.\n",identifier); + } else { + fprintf(stderr, "Unbekannter Identifier: \"%s\"\n", ident); exit(3); } } diff --git a/ag/symbol_table.h b/ag/symbol_table.h index e160c83..63626b0 100755 --- a/ag/symbol_table.h +++ b/ag/symbol_table.h @@ -1,23 +1,24 @@ #ifndef SYMBOL_TABLE_H #define SYMBOL_TABLE_H -#define SYMBOL_TYPE_FIELD 1 -#define SYMBOL_TYPE_VAR 2 +#define S_FIELD 0 +#define S_VAR 1 -struct symbol_t { - char *identifier; - struct symbol_t *next; +#define SYMNULL (struct symbol *)NULL + +struct symbol { + char *ident; + struct symbol *next; short type; }; -struct symbol_t *clone_table(struct symbol_t *table); -struct symbol_t *new_table(void); -struct symbol_t *table_add_symbol(struct symbol_t *table, char *identifier, short type, short check); -struct symbol_t *table_lookup(struct symbol_t *table, char *identifier); -struct symbol_t *table_remove_symbol(struct symbol_t *table, char *identifier); -struct symbol_t *table_merge(struct symbol_t *table, struct symbol_t *to_add, short check); -void check_variable(struct symbol_t *table, char *identifier); -void check_field(struct symbol_t *table, char *identifier); - -#endif /* SYMBOL_TABLE_H */ +struct symbol *clone_tab(struct symbol *tab); +struct symbol *new_tab(void); +struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short check); +struct symbol *tab_lookup(struct symbol *tab, char *ident); +struct symbol *tab_remove_symbol(struct symbol *tab, char *ident); +struct symbol *tab_merge(struct symbol *tab, struct symbol *to_add, short check); +void check_variable(struct symbol *tab, char *ident); +void check_field(struct symbol *tab, char *ident); +#endif -- 2.25.1