-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprparse.c
More file actions
399 lines (333 loc) · 8.74 KB
/
Copy pathprparse.c
File metadata and controls
399 lines (333 loc) · 8.74 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* prparse.c */
/* recursive descent parser for lisp-like syntax
* Makes use of scan.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "prtypes.h"
#include "prlex.h"
#include "prmachine.h"
#include "prcnsult.h"
#define TOOMANYVARS "too many vars"
#define PARSERRMSG "parsing error"
#define SCAN_ERRMSG "scan error"
#define EOFINEXP "EOF in expression"
#define VARSTOOLONG "the total length of the variable names is too long"
#define BADINT "bad integer"
#ifdef REAL
#define BADREAL "bad real"
#else
#define NOREALS "no reals in this version"
#endif
#define UNEXPECTED "unexpected symbol"
#define NONLISTARG "expected a list"
#define CLOSEBEXPECTED " ) expected"
extern char *Read_buffer;/* pralloc.c */
extern char *Print_buffer;/* pralloc.c */
extern char *parserr();/* error.c */
extern atom_ptr_t Nil;
extern unsigned int Inp_linecount;
extern int scan();
#ifdef CHARACTER
extern CHAR Char_scanned;
#endif
varindx Nvars;
static char *VarNames[MAX_VAR]; /*names of vars used to attribute offsets */
char *Var2Names[MAX_VAR];/* copy of VarNames used to display solution */
static char VarNameBuff[VARBUFSIZE]; /* used to allocate names */
static char *Varbufptr; /* moves along VarNameBuff */
static int Last_token; /* used by parse so as to avoid lookahead */
/**********************************************************************
read_list()
Main function of this file.
Reads a list and complains if not a list (and returns NULL).
Returns node_ptr_t to list parsed.
Updates VarNames, Nvars.
**************************************************************************/
node_ptr_t read_list(status)
int status;
{
void ini_parse();
node_ptr_t nodeptr, get_node(), parse();
ini_parse();
nodeptr = get_node(status);
if(parse(FALSE, status, nodeptr) == NULL)
return(NULL);
if(NODEPTR_TYPE(nodeptr) != PAIR)
{
errmsg(NONLISTARG);
return(NULL);
}
return(nodeptr);
}
/****************************************************************************
ini_parse()
****************************************************************************/
void ini_parse()
{
register int i;
for(i = 0; i < MAX_VAR; i++)
VarNames[i] = NULL;
Varbufptr = VarNameBuff;
Nvars = 0;
}
/****************************************************************************
parse()
Returns NULL if parse failed.
****************************************************************************/
node_ptr_t parse(use_Last_token, status, nodeptr)
int use_Last_token, /* a flag: use the global, dont start with a scan */
status; /* PERMANENT OR DYNAMIC etc */
node_ptr_t nodeptr;/* *nodeptr gets modified by this function*/
{
atom_ptr_t intern();
real_ptr_t find_real();
string_ptr_t find_string();
varindx find_offset();
pair_ptr_t the_list, parse_list();
int toktype, next_token;
objtype_t type;
if(use_Last_token == FALSE)
do{ /* skip spaces */
toktype = scan();
if(toktype == EOF)
return(NULL);
}
while(toktype < 33 && isspace(toktype));
else
toktype = Last_token;
switch(toktype)
{
case TOKEN_INT:
type = INT;
if(!sscanf(Read_buffer, "%lld", &(NODEPTR_INT(nodeptr))))
return (node_ptr_t)parserr(BADINT);
break;
case TOKEN_REAL:
#ifdef REAL
type = REAL;
NODEPTR_REALP(nodeptr) = find_real(Read_buffer, status);
break;
#else
return(node_ptr_t) parserr(NOREALS);
#endif
case TOKEN_ATOM:
type = ATOM;
NODEPTR_ATOM(nodeptr) = intern(Read_buffer);
break;
case TOKEN_VAR:
type = VAR;
if((NODEPTR_OFFSET(nodeptr) = find_offset(Read_buffer)) == -1)
{
return(NULL);
}
break;
case TOKEN_STRING:
type = STRING;
NODEPTR_STRING(nodeptr) = find_string(Read_buffer, status);
break;
#ifdef CHARACTER
case TOKEN_CHAR:
type = CHARACTER;
NODEPTR_CHARACTER(nodeptr) = Char_scanned;
break;
#endif
case SCAN_ERR:
return (node_ptr_t)parserr(UNEXPECTED);
case '(':
next_token = scan();
if(next_token == ')')
{
type = ATOM;
NODEPTR_ATOM(nodeptr) = Nil;
break;
}
else
type = PAIR;
Last_token = next_token;
the_list = parse_list(status);
if(the_list == NULL)
{
return(NULL);
}
NODEPTR_PAIR(nodeptr) = the_list;
break;
case EOF:
return((node_ptr_t)parserr(EOFINEXP));
default:
return (node_ptr_t)parserr(UNEXPECTED);
} /* end switch */
NODEPTR_TYPE(nodeptr) = type;
return(nodeptr);
}
/***************************************************************************
getvarname()
****************************************************************************/
char *getvarname(s)
char *s;
{
char *ret;
int how_long;
how_long = (int)strlen(s) + 1;
ret = Varbufptr;
if(how_long >= (VarNameBuff + VARBUFSIZE) -ret )
{
return parserr(VARSTOOLONG);
}
else
strcpy(ret, s);
Varbufptr += how_long;
return(ret);
}
/******************************************************************
copy_varnames()
Keep a copy of the names of the variables for an answer to a query.
*******************************************************************/
void copy_varnames()
{
int i;
char *get_string();
for(i = 0; i < Nvars; i++)
{
Var2Names[i] = get_string((my_alloc_size_t)(strlen(VarNames[i]) + 1), DYNAMIC);
strcpy(Var2Names[i], VarNames[i]);
}
}
/****************************************************************************
find_offset()
Finds an offset for a variable.
****************************************************************************/
varindx find_offset(s)
char *s;
{
int i;
char *the_name;
if(!strcmp(s, "_"))
{
if(Nvars >= MAX_VAR)
{
parserr(TOOMANYVARS);
return( -1);
}
else
VarNames[Nvars] = getvarname(s);
Nvars++;
return((Nvars - 1) * sizeof(struct subst));
}
for(i = 0; i < Nvars; i++)
{
if(VarNames[i] == NULL)break;
if(!strcmp(s, VarNames[i]))
{
return(sizeof(struct subst) * i);
}
}
if(Nvars == MAX_VAR)
{
parserr(TOOMANYVARS);
return(-1);
}
if((the_name = getvarname(s)) == NULL)
return(-1);
VarNames[i] = the_name;
Nvars++;
return(sizeof(struct subst) * i);/* do this multiplication once now
rather than at runtime*/
}
#ifdef REAL
/****************************************************************************
find_real()
Find a real corresponding to a string.
****************************************************************************/
real_ptr_t find_real(s, status)
char *s;
{
double atof();
real_ptr_t dp, get_real();
dp = get_real(status);
*dp = atof(s);
/* on error return (real_ptr_t)parserr(BADREAL); */
return(dp);
}
#endif
/****************************************************************************
find_string()
Allocate a string for an input.
****************************************************************************/
string_ptr_t find_string(s, status)
char *s;
int status;
{
string_ptr_t s1, get_string();
if(status == PERMANENT)
status = PERM_STRING;
s1 = get_string((my_alloc_size_t)(strlen(s) + 1), status);
strcpy(s1, s);
return(s1);
}
/****************************************************************************
parse_list()
Called by parse.
****************************************************************************/
pair_ptr_t parse_list(status)
{
pair_ptr_t the_list, pairptr, get_pair();
node_ptr_t headptr, tailptr;
int next_token;
the_list = get_pair(status);
pairptr = the_list;
do{
headptr = &(pairptr->head);
tailptr = &(pairptr->tail);
if(parse(TRUE, status, headptr) == NULL)
{
return(NULL);
}
else
next_token = scan();
if(next_token == ')')
{
NODEPTR_TYPE(tailptr) = ATOM;
NODEPTR_ATOM(tailptr) = Nil;
return(the_list);
}
if(next_token == CONS)
{
if(!parse(FALSE, status, tailptr))
{
return(NULL);
}
if(scan() != ')')
{
return (pair_ptr_t)parserr(CLOSEBEXPECTED);/* move past */
}
else
return(the_list);
}
else
pairptr = get_pair(status);
NODEPTR_TYPE(tailptr) = PAIR;
NODEPTR_PAIR(tailptr) = pairptr;
Last_token = next_token;
/* continue */
}
while (1);
return(the_list);/* for dumb lints only */
}
/* This can be used to return the name of the nth var but
* since it relies on the global variable VarNames this
* function must be called before the next call of ini_parse
* as in read_goals or read_list.
* It is used by a builtin
*/
char *var_name(i)
varindx i;
{
extern varindx Nvars;
if(i >= Nvars)
return(NULL);
else
return(VarNames[i]);
}
/* end of file */