Berkeley YACC
1993-03-03
Berkeley's version of Yet Another Compiler Compiler
Main Page
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Macros
Groups
symtab.c
Go to the documentation of this file.
1
#include "
defs.h
"
2
3
4
/* TABLE_SIZE is the number of entries in the symbol table. */
5
/* TABLE_SIZE must be a power of two. */
6
7
#define TABLE_SIZE 1024
8
9
10
bucket
**
symbol_table
;
11
bucket
*
first_symbol
;
12
bucket
*
last_symbol
;
13
14
15
int
16
hash
(name)
17
char *name;
18
{
19
register
char
*s;
20
register
int
c, k;
21
22
assert(name && *name);
23
s = name;
24
k = *s;
25
while
(c = *++s)
26
k = (31*k + c) & (
TABLE_SIZE
- 1);
27
28
return
(k);
29
}
30
31
32
bucket
*
33
make_bucket
(name)
34
char *name;
35
{
36
register
bucket
*bp;
37
38
assert(name);
39
bp = (
bucket
*)
MALLOC
(
sizeof
(
bucket
));
40
if
(bp == 0)
no_space
();
41
bp->
link
= 0;
42
bp->
next
= 0;
43
bp->
name
=
MALLOC
(strlen(name) + 1);
44
if
(bp->
name
== 0)
no_space
();
45
bp->
tag
= 0;
46
bp->
value
=
UNDEFINED
;
47
bp->
index
= 0;
48
bp->
prec
= 0;
49
bp->
class
=
UNKNOWN
;
50
bp->
assoc
=
TOKEN
;
51
52
if
(bp->
name
== 0)
no_space
();
53
strcpy
(bp->
name
, name);
54
55
return
(bp);
56
}
57
58
59
bucket
*
60
lookup
(name)
61
char *name;
62
{
63
register
bucket
*bp, **bpp;
64
65
bpp = symbol_table +
hash
(name);
66
bp = *bpp;
67
68
while
(bp)
69
{
70
if
(strcmp(name, bp->
name
) == 0)
return
(bp);
71
bpp = &bp->
link
;
72
bp = *bpp;
73
}
74
75
*bpp = bp =
make_bucket
(name);
76
last_symbol->
next
= bp;
77
last_symbol = bp;
78
79
return
(bp);
80
}
81
82
83
create_symbol_table
()
84
{
85
register
int
i;
86
register
bucket
*bp;
87
88
symbol_table = (
bucket
**)
MALLOC
(
TABLE_SIZE
*
sizeof
(
bucket
*));
89
if
(symbol_table == 0)
no_space
();
90
for
(i = 0; i <
TABLE_SIZE
; i++)
91
symbol_table[i] = 0;
92
93
bp =
make_bucket
(
"error"
);
94
bp->
index
= 1;
95
bp->
class
=
TERM
;
96
97
first_symbol = bp;
98
last_symbol = bp;
99
symbol_table[
hash
(
"error"
)] = bp;
100
}
101
102
103
free_symbol_table
()
104
{
105
FREE
(symbol_table);
106
symbol_table = 0;
107
}
108
109
110
free_symbols
()
111
{
112
register
bucket
*p, *q;
113
114
for
(p = first_symbol; p; p = q)
115
{
116
q = p->
next
;
117
FREE
(p);
118
}
119
}
bucket::tag
char * tag
Definition:
defs.h:117
TOKEN
#define TOKEN
Definition:
defs.h:56
MALLOC
#define MALLOC(n)
Definition:
defs.h:103
symbol_table
bucket ** symbol_table
Definition:
symtab.c:10
UNDEFINED
#define UNDEFINED
Definition:
defs.h:77
bucket::name
char * name
Definition:
defs.h:116
defs.h
UNKNOWN
#define UNKNOWN
Definition:
defs.h:70
TERM
#define TERM
Definition:
defs.h:71
hash
int hash(char *name)
Definition:
symtab.c:16
bucket::prec
short prec
Definition:
defs.h:120
free_symbol_table
free_symbol_table()
Definition:
symtab.c:103
bucket::link
struct bucket * link
Definition:
defs.h:114
create_symbol_table
create_symbol_table()
Definition:
symtab.c:83
make_bucket
bucket * make_bucket(char *name)
Definition:
symtab.c:33
no_space
no_space()
Definition:
error.c:27
bucket::class
char class
Definition:
defs.h:121
first_symbol
bucket * first_symbol
Definition:
symtab.c:11
last_symbol
bucket * last_symbol
Definition:
symtab.c:12
bucket::index
short index
Definition:
defs.h:119
FREE
#define FREE(x)
Definition:
defs.h:102
bucket::next
struct bucket * next
Definition:
defs.h:115
TABLE_SIZE
#define TABLE_SIZE
Definition:
symtab.c:7
bucket::value
short value
Definition:
defs.h:118
bucket::assoc
char assoc
Definition:
defs.h:122
strcpy
char * strcpy()
free_symbols
free_symbols()
Definition:
symtab.c:110
lookup
bucket * lookup(char *name)
Definition:
symtab.c:60
bucket
Definition:
defs.h:112
byacc
symtab.c
Generated on Thu Mar 16 2017 09:51:27 for Berkeley YACC by
1.8.6