summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTing-Wei Lan <lantw44@gmail.com>2015-10-21 16:51:15 +0800
committerTing-Wei Lan <lantw44@gmail.com>2015-10-21 16:51:57 +0800
commitb6d79d3af973cecf68a356996358f674b1798c0c (patch)
treea5c5e7bf941c51c4bc3176ac098e72ea4c2abd4b
parent9dda481bb913f613e7b41e4fbbc49d579bec0880 (diff)
downloadcompiler2015-b6d79d3af973cecf68a356996358f674b1798c0c.tar
compiler2015-b6d79d3af973cecf68a356996358f674b1798c0c.tar.gz
compiler2015-b6d79d3af973cecf68a356996358f674b1798c0c.tar.bz2
compiler2015-b6d79d3af973cecf68a356996358f674b1798c0c.tar.lz
compiler2015-b6d79d3af973cecf68a356996358f674b1798c0c.tar.xz
compiler2015-b6d79d3af973cecf68a356996358f674b1798c0c.tar.zst
compiler2015-b6d79d3af973cecf68a356996358f674b1798c0c.zip
Don't export non-namespaced symbols
-rw-r--r--lexer.l6
-rw-r--r--main.c10
-rw-r--r--symbol-table.c34
-rw-r--r--symbol-table.h18
4 files changed, 34 insertions, 34 deletions
diff --git a/lexer.l b/lexer.l
index b7c3585..829c158 100644
--- a/lexer.l
+++ b/lexer.l
@@ -96,10 +96,10 @@ ERROR .
for (i = 0; i < SIZE_OF_ARR(reserved); i++)
if (strcmp(yytext, reserved[i]) == 0) break;
if (i == SIZE_OF_ARR(reserved)) {
- symtab * ptr;
- ptr = lookup(yytext);
+ CcmmcSymbol * ptr;
+ ptr = ccmmc_symbol_table_lookup(yytext);
if (ptr == NULL)
- insertID(yytext, line_number);
+ ccmmc_symbol_table_insert_id(yytext, line_number);
else
ptr->counter++;
}
diff --git a/main.c b/main.c
index 2fd1c2c..49f3b8d 100644
--- a/main.c
+++ b/main.c
@@ -4,8 +4,8 @@
#include "symbol-table.h"
static int id_compare(const void *a, const void *b) {
- const symtab *aa = *(const symtab**)a;
- const symtab *bb = *(const symtab**)b;
+ const CcmmcSymbol *aa = *(const CcmmcSymbol**)a;
+ const CcmmcSymbol *bb = *(const CcmmcSymbol**)b;
return strcmp(aa->lexeme, bb->lexeme);
}
@@ -17,12 +17,12 @@ int main(int argc, char **argv) {
yylex();
int len;
- symtab **id_list = fillTab(&len);
- qsort(id_list, len, sizeof(symtab*), id_compare);
+ CcmmcSymbol **id_list = ccmmc_symbol_table_tmp(&len);
+ qsort(id_list, len, sizeof(CcmmcSymbol*), id_compare);
puts("Frequency of identifiers:");
for (int i = 0; i < len; i++) {
- printf("%-16s%d\n", id_list[i]->lexeme, id_list[i]->counter);
+ printf("%-15s %d\n", id_list[i]->lexeme, id_list[i]->counter);
}
return 0;
diff --git a/symbol-table.c b/symbol-table.c
index 68597f7..08a1859 100644
--- a/symbol-table.c
+++ b/symbol-table.c
@@ -7,9 +7,9 @@
#define TABLE_SIZE 256
-symtab *hash_table[TABLE_SIZE];
+static CcmmcSymbol *hash_table[TABLE_SIZE];
-int HASH(char *str) {
+static int hash(char *str) {
int idx = 0;
while (*str) {
idx = idx << 1;
@@ -20,12 +20,12 @@ int HASH(char *str) {
}
/* returns the symbol table entry if found else NULL */
-symtab *lookup(char *name) {
+CcmmcSymbol *ccmmc_symbol_table_lookup(char *name) {
int hash_key;
- symtab *symptr;
+ CcmmcSymbol *symptr;
if (!name)
return NULL;
- hash_key = HASH(name);
+ hash_key = hash(name);
symptr = hash_table[hash_key];
while (symptr) {
@@ -37,12 +37,12 @@ symtab *lookup(char *name) {
}
-void insertID(char *name, int line_number) {
+void ccmmc_symbol_table_insert_id(char *name, int line_number) {
int hash_key;
- symtab *ptr;
- symtab *symptr = malloc(sizeof(symtab));
+ CcmmcSymbol *ptr;
+ CcmmcSymbol *symptr = malloc(sizeof(CcmmcSymbol));
- hash_key = HASH(name);
+ hash_key = hash(name);
ptr = hash_table[hash_key];
if (ptr == NULL) {
@@ -62,31 +62,31 @@ void insertID(char *name, int line_number) {
symptr->counter = 1;
}
-void printSym(symtab *ptr) {
+static void print_symbol(CcmmcSymbol *ptr) {
printf(" Name = %s \n", ptr->lexeme);
printf(" References = %d \n", ptr->counter);
}
-void printSymTab(void) {
+void ccmmc_symbol_table_print(void) {
puts("----- Symbol Table ---------");
for (int i = 0; i < TABLE_SIZE; i++)
{
- symtab *symptr;
+ CcmmcSymbol *symptr;
symptr = hash_table[i];
while (symptr != NULL)
{
printf("====> index = %d\n", i);
- printSym(symptr);
+ print_symbol(symptr);
symptr = symptr->front;
}
}
}
-symtab **fillTab(int *len) {
+CcmmcSymbol **ccmmc_symbol_table_tmp(int *len) {
int cnt = 0;
for (int i = 0; i < TABLE_SIZE; i++)
{
- symtab *symptr = hash_table[i];
+ CcmmcSymbol *symptr = hash_table[i];
while (symptr != NULL)
{
cnt++;
@@ -94,11 +94,11 @@ symtab **fillTab(int *len) {
}
}
- symtab **tp = malloc(sizeof(symtab*)*cnt);
+ CcmmcSymbol **tp = malloc(sizeof(CcmmcSymbol*)*cnt);
cnt = 0;
for (int i = 0; i < TABLE_SIZE; i++)
{
- symtab *symptr = hash_table[i];
+ CcmmcSymbol *symptr = hash_table[i];
while (symptr != NULL)
{
tp[cnt++] = symptr;
diff --git a/symbol-table.h b/symbol-table.h
index 9ca58dc..b1e5a3f 100644
--- a/symbol-table.h
+++ b/symbol-table.h
@@ -1,19 +1,19 @@
#ifndef CCMMC_HEADER_SYMBOL_TABLE_H
#define CCMMC_HEADER_SYMBOL_TABLE_H
-struct symtab {
+typedef struct CcmmcSymbol_struct {
char lexeme[256];
- struct symtab *front;
- struct symtab *back;
+ struct CcmmcSymbol_struct *front;
+ struct CcmmcSymbol_struct *back;
int line;
int counter;
-};
+} CcmmcSymbol;
-typedef struct symtab symtab;
-symtab* lookup(char *name);
-void insertID(char *name, int line_number);
-void printSymTab(void);
-symtab **fillTab(int *len);
+CcmmcSymbol *ccmmc_symbol_table_lookup (char *name);
+void ccmmc_symbol_table_insert_id (char *name,
+ int line_number);
+void ccmmc_symbol_table_print (void);
+CcmmcSymbol **ccmmc_symbol_table_tmp (int *len);
#endif
// vim: set sw=4 ts=4 sts=4 et: