summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTing-Wei Lan <lantw44@gmail.com>2015-10-21 13:56:34 +0800
committerTing-Wei Lan <lantw44@gmail.com>2015-10-21 14:10:50 +0800
commit6b1492542e63297d769a3b5aeda8633c80319ad0 (patch)
tree9301848bb4d1e8f82a68235d553034b47c7e05ce
parentcfc90007070c42c1b205b1d77b3ab3b61b0fba4c (diff)
downloadcompiler2015-6b1492542e63297d769a3b5aeda8633c80319ad0.tar.gz
compiler2015-6b1492542e63297d769a3b5aeda8633c80319ad0.tar.zst
compiler2015-6b1492542e63297d769a3b5aeda8633c80319ad0.zip
Fix coding style and remove all tabs
-rw-r--r--symbol-table.c139
-rw-r--r--symbol-table.h4
2 files changed, 69 insertions, 74 deletions
diff --git a/symbol-table.c b/symbol-table.c
index 6d98bb7..2620346 100644
--- a/symbol-table.c
+++ b/symbol-table.c
@@ -1,89 +1,84 @@
-#include<stdio.h>
-#include<stdlib.h>
-#include<string.h>
-#include<ctype.h>
-#include<math.h>
-#include"symbol-table.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <math.h>
+#include "symbol-table.h"
-#define TABLE_SIZE 256
+#define TABLE_SIZE 256
-symtab * hash_table[TABLE_SIZE];
+symtab *hash_table[TABLE_SIZE];
extern int linenumber;
-int HASH(char * str){
- int idx=0;
- while(*str){
- idx = idx << 1;
- idx+=*str;
- str++;
- }
- return (idx & (TABLE_SIZE-1));
+int HASH(char *str) {
+ int idx = 0;
+ while (*str) {
+ idx = idx << 1;
+ idx += *str;
+ str++;
+ }
+ return (idx & (TABLE_SIZE-1));
}
-/*returns the symbol table entry if found else NULL*/
-
-symtab * lookup(char *name){
- int hash_key;
- symtab* symptr;
- if(!name)
- return NULL;
- hash_key=HASH(name);
- symptr=hash_table[hash_key];
+/* returns the symbol table entry if found else NULL */
+symtab *lookup(char *name) {
+ int hash_key;
+ symtab *symptr;
+ if (!name)
+ return NULL;
+ hash_key = HASH(name);
+ symptr = hash_table[hash_key];
- while(symptr){
- if(!(strcmp(name,symptr->lexeme)))
- return symptr;
- symptr=symptr->front;
- }
- return NULL;
+ while (symptr) {
+ if (!(strcmp(name, symptr->lexeme)))
+ return symptr;
+ symptr = symptr->front;
+ }
+ return NULL;
}
-void insertID(char *name){
- int hash_key;
- symtab* ptr;
- symtab* symptr=(symtab*)malloc(sizeof(symtab));
-
- hash_key=HASH(name);
- ptr=hash_table[hash_key];
-
- if(ptr==NULL){
- /*first entry for this hash_key*/
- hash_table[hash_key]=symptr;
- symptr->front=NULL;
- symptr->back=symptr;
- }
- else{
- symptr->front=ptr;
- ptr->back=symptr;
- symptr->back=symptr;
- hash_table[hash_key]=symptr;
- }
-
- strcpy(symptr->lexeme,name);
- symptr->line=linenumber;
- symptr->counter=1;
+void insertID(char *name) {
+ int hash_key;
+ symtab *ptr;
+ symtab *symptr = malloc(sizeof(symtab));
+
+ hash_key = HASH(name);
+ ptr = hash_table[hash_key];
+
+ if (ptr == NULL) {
+ /* first entry for this hash_key */
+ hash_table[hash_key] = symptr;
+ symptr->front = NULL;
+ symptr->back = symptr;
+ } else {
+ symptr->front = ptr;
+ ptr->back = symptr;
+ symptr->back = symptr;
+ hash_table[hash_key] = symptr;
+ }
+
+ strcpy(symptr->lexeme, name);
+ symptr->line = linenumber;
+ symptr->counter = 1;
}
-void printSym(symtab* ptr)
-{
- printf(" Name = %s \n", ptr->lexeme);
- printf(" References = %d \n", ptr->counter);
+void printSym(symtab *ptr) {
+ printf(" Name = %s \n", ptr->lexeme);
+ printf(" References = %d \n", ptr->counter);
}
-void printSymTab(void)
-{
- int i;
- printf("----- Symbol Table ---------\n");
- for (i=0; i<TABLE_SIZE; i++)
+void printSymTab(void) {
+ puts("----- Symbol Table ---------");
+ for (int i = 0; i < TABLE_SIZE; i++)
{
- symtab* symptr;
- symptr = hash_table[i];
- while (symptr != NULL)
- {
- printf("====> index = %d \n", i);
- printSym(symptr);
- symptr=symptr->front;
- }
+ symtab *symptr;
+ symptr = hash_table[i];
+ while (symptr != NULL)
+ {
+ printf("====> index = %d\n", i);
+ printSym(symptr);
+ symptr = symptr->front;
+ }
}
}
diff --git a/symbol-table.h b/symbol-table.h
index 9a54318..fa94c8e 100644
--- a/symbol-table.h
+++ b/symbol-table.h
@@ -1,4 +1,4 @@
-struct symtab{
+struct symtab {
char lexeme[256];
struct symtab *front;
struct symtab *back;
@@ -7,6 +7,6 @@ struct symtab{
};
typedef struct symtab symtab;
-symtab * lookup(char *name);
+symtab* lookup(char *name);
void insertID(char *name);
void printSymTab(void);