-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.c
65 lines (50 loc) · 1.31 KB
/
symbol.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "p2.h"
struct table symtable[1024];
int lastentry = 0;
int symEntry = 0;
int lookupID(char s[]) {
int p;
int rc = -1;
for (p = 0; p < lastentry; p++) {
if (strcmp(symtable[p].lexptr, s) == 0) {
rc = p;
break;
}
}
return(rc);
}
int lookupVar_index(char s[]) {
int p;
int rc = -2;
for (p = 0; p < lastentry; p++) {
if (strcmp(symtable[p].lexptr, s) == 0) {
rc = symtable[p].var_index;
break;
}
}
return(rc);
}
int installID(char s[], int tk, int var_index){
int rc;
rc = lookupID(s);
if (rc == -1){
// execute if id is not in table
if(lastentry+1 == 1024){
printf("Symbol table full!!");
}
symtable[lastentry].token = tk;
symtable[lastentry].lexptr = (char *)malloc(strlen(s)+1); // ' '
strcpy(symtable[lastentry].lexptr, s); // yytext copies to lexptr at index lastentry
if (var_index == 0) {
symtable[lastentry].var_index = symEntry++;
} else {
symtable[lastentry].var_index = var_index;
}
rc = lastentry;
lastentry++;
}
return(rc);
}