home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
vol_300
/
355_02
/
slk2.exe
/
SPP
/
RES.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-09
|
7KB
|
399 lines
/*
Sherlock Preprocessor -- reserved word handling.
Source: res.c
Started: September 28, 1987; June 9, 1991.
Version: March 25, 1988; April 5, 1988
April 16, 1990 Fixed bug in is_reserved.
PUBLIC DOMAIN SOFTWARE
Sherlock, including the SPP, SDEL and SDIF programs, was placed in
the public domain on June 15, 1991, by its author,
Edward K. Ream
166 North Prospect Ave.
Madison, WI 53705.
(608) 257-0802
Sherlock may be used for any commercial or non-commercial purpose.
DISCLAIMER OF WARRANTIES
Edward K. Ream (Ream) specifically disclaims all warranties,
expressed or implied, with respect to this computer software,
including but not limited to implied warranties of merchantability
and fitness for a particular purpose. In no event shall Ream be
liable for any loss of profit or any commercial damage, including
but not limited to special, incidental consequential or other damages.
*/
#include "spp.h"
/*
Define the reserved word tables using macros.
*/
static struct entry_struct {
char * e_string;
int e_arg1; /* en_token value. */
};
static struct tab_struct {
struct entry_struct * t_entry;
short t_strlen;
short t_entries;
};
#define table(name, n) struct tab_struct FAR name [n+1] = {
#define sub_tab(name, str_len, n_entries) {name, str_len, n_entries}
#define end_tab {0L, 0}}
#define header(label,n_entries) struct entry_struct FAR label [n_entries] = {
#define key(string,val) {string,val}
#define end_h }
/*
Reserved word tables (One entry per reserved word.)
Each table is made up of zero or more subtables,
one subtable for each length.
Subtables are arranged in increasing length.
Within each subtable, entries are arranged alphabetically.
*/
/* ----- A TABLE ----- */
#define a4_n 1
header(a4_tab, a4_n)
key("auto", K_AUTO)
end_h;
table(a_tab, 1)
sub_tab(a4_tab, 4, a4_n),
end_tab;
/* ----- B TABLE ----- */
#define b5_n 1
header(b5_tab, b5_n)
key("break", K_BREAK)
end_h;
table(b_tab, 1)
sub_tab(b5_tab, 5, b5_n),
end_tab;
/* ----- C TABLE ------ */
#define c4_n 2
header(c4_tab, c4_n)
key("case", K_CASE),
key("char", K_CHAR),
end_h;
#define c5_n 1
header(c5_tab, c5_n)
key("const", K_CONST)
end_h;
#define c8_n 1
header(c8_tab, c8_n)
key("continue", K_CONTINUE)
end_h;
table(c_tab, 3)
sub_tab(c4_tab, 4, c4_n),
sub_tab(c5_tab, 5, c5_n),
sub_tab(c8_tab, 8, c8_n),
end_tab;
/* ----- D TABLE ----- */
#define d2_n 1
header(d2_tab, d2_n)
key("do", K_DO),
end_h;
#define d6_n 1
header(d6_tab, d6_n)
key("double", K_DOUBLE)
end_h;
#define d7_n 1
header(d7_tab, d7_n)
key("default", K_DEFAULT)
end_h;
table(d_tab, 3)
sub_tab(d2_tab, 2, d2_n),
sub_tab(d6_tab, 6, d6_n),
sub_tab(d7_tab, 7, d7_n),
end_tab;
/* ----- E TABLE ----- */
#define e4_n 2
header(e4_tab, e4_n)
key("else", K_ELSE),
key("enum", K_ENUM),
end_h;
#define e5_n 1
header(e5_tab, e5_n)
key("entry", K_ENTRY)
end_h;
#define e6_n 1
header(e6_tab, e6_n)
key("extern", K_EXTERN)
end_h;
table(e_tab, 3)
sub_tab(e4_tab, 4, e4_n),
sub_tab(e5_tab, 5, e5_n),
sub_tab(e6_tab, 6, e6_n),
end_tab;
/* ----- F TABLE ----- */
#define f3_n 1
header(f3_tab, f3_n)
key("for", K_FOR)
end_h;
#define f5_n 1
header(f5_tab, f5_n)
key("float", K_FLOAT)
end_h;
table(f_tab, 2)
sub_tab(f3_tab, 3, f3_n),
sub_tab(f5_tab, 5, f5_n),
end_tab;
/* ----- G TABLE ----- */
#define g4_n 1
header(g4_tab, g4_n)
key("goto", K_GOTO)
end_h;
table(g_tab, 1)
sub_tab(g4_tab, 4, g4_n),
end_tab;
/* ----- I TABLE ----- */
#define i2_n 1
header(i2_tab, i2_n)
key("if", K_IF)
end_h;
#define i3_n 1
header(i3_tab, i3_n)
key("int", K_INT)
end_h;
table(i_tab, 2)
sub_tab(i2_tab, 2, i2_n),
sub_tab(i3_tab, 3, i3_n),
end_tab;
/* ----- L TABLE ----- */
#define l4_n 1
header(l4_tab, l4_n)
key("long", K_LONG)
end_h;
table(l_tab, 1)
sub_tab(l4_tab, 4, l4_n),
end_tab;
/* ----- N TABLE ----- */
#define n7_n 1
header(n7_tab, n7_n)
key("noalias", K_NOALIAS)
end_h;
table(n_tab, 1)
sub_tab(n7_tab, 7, n7_n),
end_tab;
/* ----- R TABLE ----- */
#define r6_n 1
header(r6_tab, r6_n)
key("return", K_RETURN)
end_h;
#define r8_n 1
header(r8_tab, r8_n)
key("register", K_REGISTER)
end_h;
table(r_tab, 2)
sub_tab(r6_tab, 6, r6_n),
sub_tab(r8_tab, 8, r8_n),
end_tab;
/* ----- S TABLE ----- */
#define s5_n 1
header(s5_tab, s5_n)
key("short", K_SHORT),
end_h;
#define s6_n 5
header(s6_tab, s6_n)
key("signed", K_SIGNED),
key("sizeof", K_SIZEOF),
key("static", K_STATIC),
key("struct", K_STRUCT),
key("switch", K_SWITCH)
end_h;
table(s_tab, 2)
sub_tab(s5_tab, 5, s5_n),
sub_tab(s6_tab, 6, s6_n),
end_tab;
/* ----- T TABLE ----- */
#define t7_n 1
header(t7_tab, t7_n)
key("typedef", K_TYPEDEF)
end_h;
table(t_tab, 1)
sub_tab(t7_tab, 7, t7_n),
end_tab;
/* ----- U TABLE ----- */
#define u5_n 1
header(u5_tab, u5_n)
key("union", K_UNION)
end_h;
#define u8_n 1
header(u8_tab, u8_n)
key("unsigned", K_UNSIGNED)
end_h;
table(u_tab, 2)
sub_tab(u5_tab, 5, u5_n),
sub_tab(u8_tab, 8, u8_n),
end_tab;
/* ----- V TABLE ----- */
#define v4_n 1
header(v4_tab, v4_n)
key("void", K_VOID)
end_h;
#define v8_n 1
header(v8_tab, v8_n)
key("volatile", K_VOLATILE)
end_h;
table(v_tab, 2)
sub_tab(v4_tab, 4, v4_n),
sub_tab(v8_tab, 8, v8_n),
end_tab;
/* ----- W TABLE ----- */
#define w5_n 1
header(w5_tab, w5_n)
key("while", K_WHILE)
end_h;
table(w_tab, 1)
sub_tab(w5_tab, 5, w5_n),
end_tab;
/*
Indirect reserved word table
WARNING: this used to be offset from 'A' instead of 'a'.
*/
struct tab_struct * rwx_tab [] = {
a_tab,
b_tab,
c_tab,
d_tab,
e_tab,
f_tab,
g_tab,
0L, /* h */
i_tab,
0L, /* j */
0L, /* k */
l_tab,
0L, /* m */
n_tab,
0L, /* o */
0L, /* p */
0L, /* q */
r_tab,
s_tab,
t_tab,
u_tab,
v_tab,
w_tab,
0L, /* x */
0L, /* y */
0L /* z */
};
/*
See if name[] contains a reserved word.
Return the token or NULL_TOK if not found.
*/
en_tokens
is_reserved(register char *name, int length)
{
char c;
register struct tab_struct * p;
register struct entry_struct * e;
register int i, n;
TRACEP("is_reserved", printf("name: %s, length %d\n", name, length));
c = *name;
if (c < 'a' || c > 'z') {
goto not_found;
}
/* Search for the proper subtable. */
for (p = rwx_tab [c - 'a']; ; p++) {
TRACEP("is_reserved", printf("p = %lx\n", p));
/* 6/9/91 */
if (p == NULL || p -> t_entry == NULL) {
goto not_found;
}
TRACEP("is_reserved",
printf("p -> t_strlen = %d\n", p -> t_strlen));
if (p -> t_strlen > length) {
goto not_found;
}
else if (p -> t_strlen == length) {
break;
}
}
/* A subtable with the right length has been found. */
n = p -> t_entries;
for (i = 0, e = p -> t_entry; i < n; e++, i++) {
TRACEP("is_reserved",
printf("compare: %s, %s\n", name, e -> e_string));
if (str_eq(name, e -> e_string)) {
/* Found. */
token = ((int) (e -> e_arg1)) & 0xff;
TRACEP("is_reserved", printf("returns %d\n", token));
return token;
}
}
not_found:
TRACEP("is_reserved_ret", printf("ISR: returns 0\n"));
return NULL_TOK;
}