From 40f423a4b3b1e64e8424ab239cc14ecc5077640f Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Sun, 22 Apr 2018 14:49:42 +0200 Subject: Beginning of the symbol table implementation --- src/symboltable.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/symboltable.c (limited to 'src/symboltable.c') diff --git a/src/symboltable.c b/src/symboltable.c new file mode 100644 index 0000000..852d1b8 --- /dev/null +++ b/src/symboltable.c @@ -0,0 +1,35 @@ +#include "symboltable.h" + +extern int lineno; /* from lexical analyser */ + +SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0}; + +void addVar(const char name[], int type) { + int count; + for (count = 0; count < symbol_table.size; count++) { + if (!strcmp(symbol_table.entries[count].name, name)) { + printf("semantic error, redefinition of variable %s near line %d\n", name, + lineno); + return; + } + } + if (++symbol_table.size > symbol_table.maxsize) { + printf("too many variables near line %d\n", lineno); + exit(1); + } + strcpy(symbol_table.entries[symbol_table.size - 1].name, name); + symbol_table.entries[symbol_table.size - 1].type = type; +} + +void lookup(const char name[]) { + int count; + + for (count = 0; count < symbol_table.size; count++) { + if (!strcmp(symbol_table.entries[count].name, name)) { + return; + } + printf("No definition of the variable %s near line %d\n", name, + lineno); + } +} + -- cgit v1.2.3