git @ Cat's Eye Technologies OpenZz / master src / rule.c
master

Tree @master (Download .tar.gz)

rule.c @masterraw · history · blame

  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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* 
    Zz Dynamic Parser Library
    Copyright (C) 1989 - I.N.F.N - S.Cabasino, P.S.Paolucci, G.M.Todesco

    The Zz Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    The Zz Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*==============================================================================

	rule.C

	() gestione non-terminali:

		struct s_nt *start_symbol;
		struct s_nt *find_nt();


	() per creare una regola:	
	
		open_rule(headname);
		append_bead(cnt,name);
		append_t_bead(cnt);
		append_nt_bead(ntname,name);

		setaction_exelist(list);
		setaction_exeproc(proc);
		setaction_exesproc(proc);
		setaction_return(value);
		setaction_pass(proc);
		setaction_list(proc);
		setaction_merge_all(proc);
		setaction_merge(proc);
		setaction_append(proc);

	   rule=close_rule();	


	() per distruggere una regola:
		free_rule(rule);



==============================================================================*/


#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "avl.h"
#include "rule.h"
#include "list.h"
#include "err.h"
#include "mem.h"
#include "trace.h"
#include "table.h"
#include "printz.h"

/*PROTOTYPES*/
int param_substitute();  /* signature int param_substitute(struct s_content *token,char **paramname) in param.h is incompatible */
int parse(struct s_nt *);
int source_list();  /* signature int source_list(struct s_content *, void *); is incompatible */
void pop_source(void);

static int nt_mem=0;
static int rule_mem=0;
struct s_nt *nt_any=0,*nt_param=0,*nt_gparam;

/* forwards, now exported in rule.h */
/*void init_rule();*/
/*void free_rule(struct s_rule *);*/
/*----------------------------------------------------------------------------*/

/*
extern struct s_dot *create_dot();
*/
/* now in table.h
  extern int link_rule(),unlink_rule();
*/

struct s_nt *start_symbol=0;

/*----------------------------------------------------------------------------*/

static TREE *nttree=0;

static struct s_rule *cur_rule=0,*last_rule=0;

#define RULES_SIZE 100
static TREE *rules[RULES_SIZE];
static int rules_sp=1;
static int init_rule_done=0;
int kernel_flag=1;

struct s_rule_class {
	char *name;
	TREE *rules;
	};

TREE *rule_classes=0;


/*----------------------------------------------------------------------------*/

void zz_set_kernel_flag(int bool_value)
{
  kernel_flag = bool_value;
}

/*----------------------------------------------------------------------------*/

struct s_nt *find_nt(char *name)
{
  struct s_nt *nt;
  zz_assert(name);
  if(!nttree)
    {
      nttree = avl_tree_nodup_str (struct s_nt, name);
      nt_param=find_nt("param");
      nt_gparam=find_nt("gparam");
      nt_any=find_nt("any");
    }
  nt = avl_locate(nttree,name);
  if(!nt)
    {
      nt = (struct s_nt*)calloc(1,sizeof(struct s_nt));
      nt->name = (char*)malloc(strlen(name)+1);
      nt_mem+=sizeof(struct s_nt)+strlen(name)+1;
      strcpy(nt->name,name);
      nt->prompt=0;
      avl_insert(nttree,nt);
      nt->first_dot = 0;
    }
  return nt;
}


/*----------------------------------------------------------------------------*/

int rulecmp(struct s_rule *r1,struct s_rule *r2)
{
  int i;
  zz_assert(r1 && r2);
  for(i=0;i<r1->bead_n && i<r2->bead_n;i++)
    {
      if(r1->beads[i].cnt.tag < r2->beads[i].cnt.tag)
	return -1;
      else if(r1->beads[i].cnt.tag > r2->beads[i].cnt.tag) 
	return 1;
      else if(s_content_value(r1->beads[i].cnt) < s_content_value(r2->beads[i].cnt))
	return -1;
      else if(s_content_value(r1->beads[i].cnt) > s_content_value(r2->beads[i].cnt))
	return 1;     
    }
  if(i<r1->bead_n) return -1;
  else if(i<r2->bead_n) return 1;
  else return 0;
}


/*----------------------------------------------------------------------------*/


void open_rule(char *ntname)
{
  struct s_content tmp1,tmp2;
  zz_assert(ntname);
  tmp1.tag = tag_ident;
  s_content_value(tmp1) = (long)ntname;
  param_substitute(&tmp1,&tmp2);
  ntname = (char*)s_content_value(tmp1);
  if(!init_rule_done) init_rule();
  if(cur_rule)
    free_rule(cur_rule, NULL);
  cur_rule = (struct s_rule *)calloc(1,sizeof(struct s_rule));
  rule_mem += sizeof(struct s_rule);
  cur_rule->bead_n=1;
  cur_rule->beads = (struct s_bead*)
    calloc(MAX_RULE_LENGTH,sizeof(struct s_bead));
  cur_rule->bead_size = MAX_RULE_LENGTH;
  rule_mem += sizeof(struct s_bead)*cur_rule->bead_size;
  cur_rule->beads[0].cnt.tag=tag_sint;
  s_content_value(cur_rule->beads[0].cnt)=(long)find_nt(ntname);
  cur_rule->beads[0].name=0;
  cur_rule->cnt_prec.tag = tag_sint;
  s_content_value(cur_rule->cnt_prec) = 0;
  cur_rule->when_exit_scope.tag = 0;
  cur_rule->when_change_action.tag = 0;
  cur_rule->kernel=kernel_flag;
}

/*---------------------------------------------------------------------------*/

void setaction_exelist(struct s_content *list)
{
  zz_assert(list);
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  if(list->tag!=tag_list)
    {
      zz_error(INTERNAL_ERROR, "setaction_exelist. argument is not a list\n");
      return;
    }
  cur_rule->action_type = ACT_T_EXECUTE_LIST;
  cur_rule->action = *list;
  cur_rule->sproc_tag = 0;
}

/*----------------------------------------------------------------------------*/

void setaction_exeproc(int (*proc)(), struct s_tag *tag)
{
  zz_assert(proc);
  //zz_assert(tag);
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_EXECUTE_PROC;
  cur_rule->action.tag = tag_procedure;
  s_content_value(cur_rule->action) = (long)proc;
  cur_rule->sproc_tag = tag;
}

/*----------------------------------------------------------------------------*/

void setaction_exesproc(int (*sproc)(), struct s_tag *tag)
{
  zz_assert(sproc && tag);
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_EXECUTE_SPROC;
  cur_rule->action.tag = tag_procedure;
  s_content_value(cur_rule->action) = (long)sproc;
  cur_rule->sproc_tag = tag;
}

/*---------------------------------------------------------------------------*/

void setaction_return(struct s_content *value, char *tag_name)
{
  //struct s_tag *find_tag();
  zz_assert(value);

  // Cannot assert tag_name or else statements
  //  like "/float -> foo float^n : return n" fail.
  // zz_assert(tag_name);

  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "rule not open\n");return;}

  cur_rule->action_type = ACT_T_RETURN;
  cur_rule->action = *value;
  cur_rule->sproc_tag = (tag_name)?find_tag(tag_name):0;
}

/*----------------------------------------------------------------------------*/

void setaction_pass()
{
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_PASS;
  cur_rule->action.tag = 0;
  s_content_value(cur_rule->action) = 0;
  cur_rule->sproc_tag = 0;
}

/*---------------------------------------------------------------------------*/

void setaction_rreturn()
{
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_RRETURN;
  cur_rule->action.tag = 0;
  s_content_value(cur_rule->action) = 0;
  cur_rule->sproc_tag = 0;
}

/*---------------------------------------------------------------------------*/

void setaction_assign()
{
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_ASSIGN;
  cur_rule->action.tag = 0;
  s_content_value(cur_rule->action) = 0;
  cur_rule->sproc_tag = 0;
}

/*----------------------------------------------------------------------------*/

void setaction_list()
{
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_LIST;
  cur_rule->action.tag = 0;
  s_content_value(cur_rule->action) = 0;
  cur_rule->sproc_tag = 0;
}

/*----------------------------------------------------------------------------*/

void setaction_merge_all()
{
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_MERGE_ALL;
  cur_rule->action.tag = 0;
  s_content_value(cur_rule->action) = 0;
  cur_rule->sproc_tag = 0;
}

/*----------------------------------------------------------------------------*/

void setaction_merge()
{
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_MERGE;
  cur_rule->action.tag = 0;
  s_content_value(cur_rule->action) = 0;
  cur_rule->sproc_tag = 0;
}

/*----------------------------------------------------------------------------*/

void setaction_append()
{
  if(!cur_rule)
    {zz_error(INTERNAL_ERROR, "setaction: rule not open\n");return;}
  cur_rule->action_type = ACT_T_APPEND;
  cur_rule->action.tag = 0;
  s_content_value(cur_rule->action) = 0;
  cur_rule->sproc_tag = 0;
}

/*----------------------------------------------------------------------------*/

struct s_rule *close_rule()
{
  int i;
  struct s_rule *rule;
  struct s_bead *beads;
  if(!cur_rule)
    {
      zz_error(ERROR,"close_rule: rule not open");
      return 0;
    }
  rule = cur_rule;
  cur_rule = 0;
  beads = rule->beads;
  rule->beads = (struct s_bead*)
    calloc(rule->bead_n,sizeof(struct s_bead));
  rule_mem+=sizeof(struct s_bead)*(rule->bead_n-rule->bead_size);
  rule->bead_size=rule->bead_n;
  for(i=0;i<rule->bead_n;i++)
    rule->beads[i]=beads[i];
  free(beads);
  rule->segment_id=0;
  return rule;
}


/*----------------------------------------------------------------------------*/

void append_bead(struct s_content *cnt, char *name)
{
  int i;
  if(!cur_rule)
    {
      zz_error(ERROR,"append_bead: rule not open");
      return;
    }
  if(cur_rule->bead_n>=MAX_RULE_LENGTH)
    {
      zz_error(ERROR,"append_bead: rule too long");
      return;   
    }
  i=cur_rule->bead_n++;
  cur_rule->beads[i].cnt = *cnt;
  cur_rule->beads[i].name = name;
  if(cnt->tag==tag_qstring || cnt->tag==tag_ident || cnt->tag==tag_char)
    cur_rule->cnt_prec = *cnt;
}

/*----------------------------------------------------------------------------*/

void append_t_bead(struct s_content *cnt)
{
  char *s;
  struct s_content tmp;
  if(cnt->tag==tag_qstring)
    {
      s = (char*)s_content_value(*cnt);
      while(*s==' ' || *s=='\t')s++;
      while(*s)
	{
	  zlex(&s,&tmp);
	  append_t_bead(&tmp);
	  while(*s==' ' || *s=='\t')s++;
	}
    }
  else
    append_bead(cnt,0);
}

/*----------------------------------------------------------------------------*/

void append_nt_bead(char *ntname, char *beadname)
{
struct s_content tmp;
static char *dollar=0;
if(!beadname)
  {
   if(!dollar) dollar = zlex_strsave("$");
   beadname = dollar;   
  }
tmp.tag = tag_sint;
s_content_value(tmp) = (long)find_nt(ntname);
append_bead(&tmp,beadname);
}


/*============================================================================*/


static int fprint_rule(FILE *chan, void *_rule)
{
  int i;
  struct s_nt *nt;
  struct s_rule *rule = (struct s_rule *)_rule;
  if(!rule) 
    fprintz(chan,"(nil)");
  else if(rule->bead_n<0)
    fprintz(chan,"(nil)-> ");
  else
    { 
      nt = (struct s_nt*) s_content_value(rule->beads[0].cnt);
      fprintz(chan,"%s ->",nt->name);
      for(i=1;i<rule->bead_n;i++)
	{
	  if(rule->beads[i].cnt.tag==tag_sint)
	    fprintz(chan," %s^%s",
		    ((struct s_nt*)s_content_value(rule->beads[i].cnt))->name,
		    rule->beads[i].name);
	  else
	    fprintz(chan," %z",&rule->beads[i].cnt);
	}
    }
  return 1;
}

/*----------------------------------------------------------------------------*/

static int sprint_rule(char* buffer, void *_rule)
{
  struct s_nt *nt;
  int i;
  char *s;
  struct s_rule *rule=(struct s_rule *)_rule;

  if(!rule) 
    strcpy(buffer,"(nil)");
  else if(rule->bead_n<0)
    strcpy(buffer,"(nil)-> ");
  else
    { 
      nt = (struct s_nt*) s_content_value(rule->beads[0].cnt);
      strcpy(buffer,nt->name);
      strcat(buffer,"  ->");
      s = buffer+strlen(buffer);
      for(i=1;i<rule->bead_n;i++)
	{
	  if(rule->beads[i].cnt.tag==tag_sint)
	    sprintz(s," %s^%s",
		    ((struct s_nt*)s_content_value(rule->beads[i].cnt))->name,
		    rule->beads[i].name);
	  else
	    sprintz(s," %z",&rule->beads[i].cnt);
	  while(*s)s++;
	}
    }
  return 1;
}

/*----------------------------------------------------------------------------*/

void print_rule(struct s_rule *rule)
{
  printz("  %r\n",rule);
}

/*----------------------------------------------------------------------------*/

void init_rule()
{
  if(init_rule_done) return;
  init_rule_done=1;
  printz_code('r',fprint_rule,sprint_rule);
}

/*----------------------------------------------------------------------------*/

static int do_delete_scope_action(rule)
struct s_rule *rule;
{
   zz_trace("do_delete_scope_action()\n");

if(rule->when_exit_scope.tag==tag_list)
  {
   source_list(&rule->when_exit_scope);
   parse(find_nt("root"));
   pop_source();
   zz_trace("do_delete_scope_action() exit scope (%r): %z\n",rule,&rule->when_exit_scope);
  }
}

void free_rule(void *_rule, void *dummy_param)
{
  struct s_rule *rule = (struct s_rule *)_rule;
  do_delete_scope_action(rule);
  rule_mem-=sizeof(struct s_rule)-sizeof(struct s_bead)*rule->bead_size;
  free(rule->beads);
  free(rule);
}

/*============================================================================*/

void show_rule_memory()
{
  PRINTMEM("rule",rule_mem);
  PRINTMEM("non-terminal",nt_mem);
}

/*============================================================================*/

/*

azioni:

		  sproc_tag    action_type          action.tag 
	action	:              ACT_T_EXECUTE_PROC   tag_procedure
	saction : tag_xxx      ACT_T_EXECUTE_SPROC  tag_procedure
	pass    :              ACT_T_PASS
	list    :              ACT_T_LIST
	append  :              ACT_T_APPEND
        ret/proc:	       ACT_T_RETURN	    tag_procedure

*/

static char rcsid[] = "$Id: rule.c,v 1.18 2002/06/03 11:06:13 kibun Exp $ ";