diff options
author | Ting-Wei Lan <lantw44@gmail.com> | 2015-10-19 01:50:32 +0800 |
---|---|---|
committer | Ting-Wei Lan <lantw44@gmail.com> | 2015-10-19 01:50:32 +0800 |
commit | 3cb9f6677b30eba522deb12b4bb87cdec7de44d0 (patch) | |
tree | 689a8df0825ef646035270d5846501019d64a305 | |
download | compiler2015-3cb9f6677b30eba522deb12b4bb87cdec7de44d0.tar.gz compiler2015-3cb9f6677b30eba522deb12b4bb87cdec7de44d0.tar.zst compiler2015-3cb9f6677b30eba522deb12b4bb87cdec7de44d0.zip |
Import files provided by TATA
-rw-r--r-- | Makefile | 21 | ||||
-rw-r--r-- | header.h | 11 | ||||
-rw-r--r-- | lexer.l | 90 | ||||
-rw-r--r-- | symboltable.c | 89 |
4 files changed, 211 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e99acd1 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +TARGET = scanner +OBJECT = lex.yy.o symboltable.o +CC = cc -g +LEX = flex +LIBS = -lfl + +scanner: lex.yy.o symboltable.o + $(CC) -o scanner lex.yy.o symboltable.o + +symboltable.o: symboltable.c + $(CC) -c symboltable.c + +lex.yy.o: lex.yy.c + $(CC) -c lex.yy.c + +lex.yy.c: lexer.l + $(LEX) $(LIB) lexer.l + +clean: + rm -f $(TARGET) $(OBJECT) + diff --git a/header.h b/header.h new file mode 100644 index 0000000..fd9a8aa --- /dev/null +++ b/header.h @@ -0,0 +1,11 @@ +struct symtab{ + char lexeme[256]; + struct symtab *front; + struct symtab *back; + int line; + int counter; +}; + +typedef struct symtab symtab; +symtab * lookup(char *name); +void insert(char *name); @@ -0,0 +1,90 @@ +%option noyywrap +%{ +#include <stdio.h> +#include "header.h" +int linenumber; +symtab * lookup(); +symtab * ptr; +void insertID(); +void printSymTab(); +/* You need to define for all tokens in C--, here are some examples */ +#define INT 10 +#define FLOAT 11 +#define OP_ASSIGN 12 +#define OP_OR 13 +#define MK_LPAREN 14 +#define MK_RPAREN 15 +#define ERROR 100 + +%} + +letter [A-Za-z] +digit [0-9] +ID {letter}({letter}|{digit}|"_")* +WS [ \t]+ +Int_constant {digit}+ + +/* You need to define the following RE's +Float_constant +String_constant +comment +*/ + +OP_assign "=" +OP_or "||" + +/* Other operators appear here */ + + +newline "\n" + +DL_lparen "(" +DL_rparen ")" +DL_lbrace "{" +DL_rbrace "}" +DL_comma "," +DL_semicol ";" +DL_dot "." + +/* Other separators appear here */ + +error . + +%% + +{WS} {} +{RWInt} {/* return INT; */ } +{RWFloat} {/* return FLOAT; */} +{ID} { + ptr = lookup(yytext); + if (ptr == NULL) + insertID(yytext); + else + ptr->counter++; + } + +{OP_assign} /* return OP_ASSIGN; */ +{OP_or} /* return OP_OR; */ +{DL_lparen} /* return MK_LPAREN; */ +{DL_rparen} /* return MK_RPAREN; */ +{DL_lbrace} /* return MK_LBRACE; */ +{DL_rbrace} /* return MK_RBRACE; */ +{DL_comma} {} +{DL_semicol} {} +{newline} linenumber += 1; +{error} printf("ERR \n");/* return ERROR; */ + + +%% + +main(int argc, char **argv) +{ + argc--; ++argv; + if (argc > 0) + yyin = fopen(argv[0], "r"); + else + yyin = stdin; + yylex(); + printSymTab(); +} + diff --git a/symboltable.c b/symboltable.c new file mode 100644 index 0000000..0006eb2 --- /dev/null +++ b/symboltable.c @@ -0,0 +1,89 @@ +#include<stdio.h> +#include<stdlib.h> +#include<string.h> +#include<ctype.h> +#include<math.h> +#include"header.h" + +#define TABLE_SIZE 256 + +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)); +} + +/*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; +} + + +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 printSym(symtab* ptr) +{ + printf(" Name = %s \n", ptr->lexeme); + printf(" References = %d \n", ptr->counter); +} + +void printSymTab() +{ + int i; + printf("----- Symbol Table ---------\n"); + for (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; + } + } +} |