-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.c
133 lines (105 loc) · 2.97 KB
/
key.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// GROUP 39
// AKANKSHYA MISHRA 2016A7PS0026P
// NARAPAREDDY BHAVANA 2016A7PS0034P
// KARABEE BATTA 2016A7PS0052P
// AASTHA KATARIA 2016A7PS0062P
#include "key.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int hash(char *str){
long unsigned int sum = 0;
long unsigned int a = 8;
for (int i=0; i<strlen(str); i++){
sum=(sum*a+str[i])%41;
}
return sum;
}
hash_elem *create_hash_elem(int tkname, char *str){
hash_elem *h = (hash_elem*)malloc(sizeof(hash_elem));
h->str = str;
h->tkname = tkname;
h->next=NULL;
return h;
}
void insertIntoHT(hash_elem *elem){
int index = hash(elem->str);
if(KeyWordTable[index]->next==NULL){
KeyWordTable[index]->next=elem;
}
else{
hash_elem *ptr = KeyWordTable[index]->next;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=elem;
}
}
void populateKeyWordTable(){
KeyWordTable = (hTable)malloc(sizeof(hash_elem*)*43);
for(int i=0; i<43; i++){
KeyWordTable[i]=create_hash_elem(0, "none");
}
int index;
//populate this
//with
insertIntoHT(create_hash_elem(TK_WITH, "with"));
//parameters
insertIntoHT(create_hash_elem(TK_PARAMETERS, "parameters"));
//end
insertIntoHT(create_hash_elem(TK_END, "end"));
//while
insertIntoHT(create_hash_elem(TK_WHILE, "while"));
//type
insertIntoHT(create_hash_elem(TK_TYPE, "type"));
//main
insertIntoHT(create_hash_elem(TK_MAIN, "_main"));
//global
insertIntoHT(create_hash_elem(TK_GLOBAL, "global"));
//parameter
insertIntoHT(create_hash_elem(TK_PARAMETER, "parameter"));
//list
insertIntoHT(create_hash_elem(TK_LIST, "list"));
//input
insertIntoHT(create_hash_elem(TK_INPUT, "input"));
//output
insertIntoHT(create_hash_elem(TK_OUTPUT, "output"));
//int
insertIntoHT(create_hash_elem(TK_INT, "int"));
//real
insertIntoHT(create_hash_elem(TK_REAL, "real"));
//endwhile
insertIntoHT(create_hash_elem(TK_ENDWHILE, "endwhile"));
//if
insertIntoHT(create_hash_elem(TK_IF, "if"));
//then
insertIntoHT(create_hash_elem(TK_THEN, "then"));
//endif
insertIntoHT(create_hash_elem(TK_ENDIF, "endif"));
//read
insertIntoHT(create_hash_elem(TK_READ, "read"));
//write
insertIntoHT(create_hash_elem(TK_WRITE, "write"));
//return
insertIntoHT(create_hash_elem(TK_RETURN, "return"));
//call
insertIntoHT(create_hash_elem(TK_CALL, "call"));
//record
insertIntoHT(create_hash_elem(TK_RECORD, "record"));
//endrecord
insertIntoHT(create_hash_elem(TK_ENDRECORD, "endrecord"));
//else
insertIntoHT(create_hash_elem(TK_ELSE, "else"));
}
hash_elem* lookup(char *str){
int index = hash(str);
hash_elem *temp =KeyWordTable[index]->next;
while(temp!=NULL){
if(strcmp(str, temp->str)==0){
// printf("%s\n", str);
return temp;
}
temp=temp->next;
}
return KeyWordTable[index];
}