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
134
135
136
137
138
139
140
141
142
143
144
|
/*++
/* NAME
/* symbol 3
/* SUMMARY
/* rudimentary symbol table package
/* SYNOPSIS
/* #include "symbol.h"
/*
/* void sym_init()
/*
/* void sym_enter(name, type)
/* char *name;
/* int type;
/*
/* struct symbol *sym_find(name)
/* char *name;
/* DESCRIPTION
/* This is a rudimentary symbol-table package, just enough to
/* keep track of a couple of C keywords.
/*
/* sym_init() primes the table with C keywords. At present, most of
/* the keywords that have to do with types are left out.
/* We need a different strategy to detect type definitions because
/* we do not keep track of typedef names.
/*
/* sym_enter() adds an entry to the symbol table.
/*
/* sym_find() locates a symbol table entry (it returns 0 if
/* it is not found).
/* AUTHOR(S)
/* Wietse Venema
/* Eindhoven University of Technology
/* Department of Mathematics and Computer Science
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
/* LAST MODIFICATION
/* 92/02/15 18:59:56
/* VERSION/RELEASE
/* 1.4
/*--*/
static char symbol_sccsid[] = "@(#) symbol.c 1.4 92/02/15 18:59:56";
/* C library */
extern char *strcpy();
extern char *malloc();
/* Application-specific stuff */
#include "error.h"
#include "token.h"
#include "symbol.h"
#define SYM_TABSIZE 20
static struct symbol *sym_tab[SYM_TABSIZE] = {0,};
/* More string stuff. Maybe it should go to an #include file. */
#define STREQ(x,y) (*(x) == *(y) && strcmp((x),(y)) == 0)
/* sym_enter - enter symbol into table */
void sym_enter(name, type)
char *name;
int type;
{
struct symbol *s;
int where;
if ((s = (struct symbol *) malloc(sizeof(*s))) == 0
|| (s->name = malloc(strlen(name) + 1)) == 0)
fatal("out of memory");
(void) strcpy(s->name, name);
s->type = type;
where = hash(name, SYM_TABSIZE);
s->next = sym_tab[where];
sym_tab[where] = s;
}
/* sym_find - locate symbol definition */
struct symbol *sym_find(name)
register char *name;
{
register struct symbol *s;
/*
* This function is called for almost every "word" token, so it better be
* fast.
*/
for (s = sym_tab[hash(name, SYM_TABSIZE)]; s; s = s->next)
if (STREQ(name, s->name))
return (s);
return (0);
}
/*
* Initialization data for symbol table. We do not enter keywords for types.
* We use a different strategy to detect type declarations because we do not
* keep track of typedef names.
*/
struct sym {
char *name;
int tokno;
};
static struct sym syms[] = {
"if", TOK_CONTROL,
"else", TOK_CONTROL,
"for", TOK_CONTROL,
"while", TOK_CONTROL,
"do", TOK_CONTROL,
"switch", TOK_CONTROL,
"case", TOK_CONTROL,
"default", TOK_CONTROL,
"return", TOK_CONTROL,
"continue", TOK_CONTROL,
"break", TOK_CONTROL,
"goto", TOK_CONTROL,
"struct", TOK_COMPOSITE,
"union", TOK_COMPOSITE,
"__DATE__", TOK_DATE,
"__TIME__", TOK_TIME,
#if defined(MAP_VOID_STAR) || defined(MAP_VOID)
"void", TOK_VOID,
#endif
"asm", TOK_OTHER,
0,
};
/* sym_init - enter known keywords into symbol table */
void sym_init()
{
register struct sym *p;
for (p = syms; p->name; p++)
sym_enter(p->name, p->tokno);
}
|