1#include <stdio.h>
2#include <ctype.h>
3
4#include "set.h"
5#include "syn.h"
6#include "hash.h"
7#include "generic.h"
8
9#ifdef __USE_PROTOS
10void dumpset1(set s)
11#else
12void dumpset1(s)
13  set   s;
14#endif
15{
16  if (set_nil(s)) {
17    fprintf(stderr,"{}");
18  } else {
19    s_fprT(stderr,s);
20  };
21}
22
23#ifdef __USE_PROTOS
24void dumpset(set s)
25#else
26void dumpset(s)
27  set   s;
28#endif
29{
30  dumpset1(s);
31  fprintf(stderr,"\n");
32}
33
34#ifdef __USE_PROTOS
35int isEndRule(Node * p)
36#else
37int isEndRule(p)
38  Node *    p;
39#endif
40{
41  int       result=0;
42  if ( p->ntype == nJunction &&
43       ( (Junction *) p)->jtype == EndRule) {
44    result=1;
45  };
46  return result;
47}
48
49#ifdef __USE_PROTOS
50void dumppred1(int depth,Predicate *p)
51#else
52void dumppred1(depth,p)
53  int           depth;
54  Predicate     *p;
55#endif
56{
57  int       i;
58  int       k;
59
60  for (i=0; i<depth ; i++) {
61    fprintf(stderr,"  ");
62  };
63  if (p->expr == PRED_AND_LIST ||
64      p->expr == PRED_OR_LIST) {
65    fprintf(stderr," %s", (p->expr == NULL ? "null expr" : p->expr));
66    if (p->inverted) fprintf(stderr," predicate inverted !");
67    if (p->redundant) {
68      fprintf(stderr," Redundant!");
69    };
70    if (p->isConst) fprintf(stderr," const %d !",p->constValue);
71    fprintf(stderr,"\n");
72  } else {
73    fprintf(stderr,"predicate k=%d",p->k);
74    k=set_int(p->completionSet);
75    if (k >= 0) {
76      fprintf(stderr," Incomplete Set=%d !",k);
77    };
78    k=set_int(p->completionTree);
79    if (k >= 0) {
80      fprintf(stderr," Incomplete Tree=%d !",k);
81    };
82    if (p->redundant) {
83      fprintf(stderr," Redundant!");
84    };
85    fprintf(stderr," \"%s\" (%x)", (p->expr == NULL ? "null expr" : p->expr) ,p);
86    if (p->source != NULL) {
87       fprintf(stderr,"line %d",p->source->line);
88    };
89    if (p->inverted) fprintf(stderr," predicate inverted !");
90    fprintf(stderr,"\n");
91    for (i=0; i<depth ; i++) {
92      fprintf(stderr,"  ");
93    };
94    fprintf(stderr,"scontext: ");
95    dumpset(p->scontext[1]);
96    for (i=0; i<depth ; i++) {
97      fprintf(stderr,"  ");
98    };
99    fprintf(stderr,"tcontext: ");
100    preorder(p->tcontext);
101    fprintf(stderr,"\n");
102  };
103  fprintf(stderr,"\n");
104  if (p->down != NULL) {
105    dumppred1(depth+1,p->down);
106  };
107  if (p->right != NULL) {
108    dumppred1(depth,p->right);
109  };
110}
111
112#ifdef __USE_PROTOS
113void dumppred(Predicate *p)
114#else
115void dumppred(p)
116  Predicate     *p;
117#endif
118{
119  fprintf(stderr,"---------------------------------\n");
120  dumppred1(0,p);
121  fprintf(stderr,"\n");
122}
123
124#ifdef __USE_PROTOS
125void dumppredtree(Predicate *p)
126#else
127void dumppredtree(p)
128  Predicate     *p;
129#endif
130{
131  fprintf(stderr,"predicate k=%d \"%s\" line %d\n",p->k,p->expr,p->source->line);
132  dumpset(p->scontext[1]);
133}
134
135#ifdef __USE_PROTOS
136void dumppredexpr(Predicate *p)
137#else
138void dumppredexpr(p)
139  Predicate     *p;
140#endif
141{
142  fprintf(stderr,"    pred expr \"%s\"\n",p->expr);
143}
144
145#ifdef __USE_PROTOS
146void dt(Tree *t)
147#else
148void dt(t)
149  Tree  *t;
150#endif
151{
152  MR_dumpTreeF(stderr,0,t,5);
153}
154
155#ifdef __USE_PROTOS
156void d(Node * p)
157#else
158void d(p)
159  Node *    p;
160#endif
161{
162
163  Junction      *j;
164  RuleRefNode   *r;
165  TokNode       *t;
166  ActionNode    *a;
167
168  if (p==NULL) {
169    fprintf(stderr,"dumpNode: Node is NULL");
170    return;
171  };
172
173  switch (p->ntype) {
174    case nJunction :
175      j = (Junction *) p;
176      fprintf(stderr, "Junction (#%d in rule %s line %d) ",j->seq,j->rname,j->line);
177      if (j->guess) fprintf(stderr,"guess block ");
178      switch (j->jtype ) {
179        case aSubBlk :
180          fprintf(stderr,"aSubBlk");
181          break;
182        case aOptBlk :
183          fprintf(stderr,"aOptBlk");
184          break;
185        case aLoopBegin :
186          fprintf(stderr,"aLoopBeginBlk");
187          break;
188        case aLoopBlk :
189          fprintf(stderr,"aLoopBlk");
190          break;
191        case aPlusBlk :
192          fprintf(stderr,"aPlusBlk");
193          break;
194        case EndBlk :
195          fprintf(stderr,"EndBlk");
196          break;
197        case RuleBlk :
198          fprintf(stderr,"RuleBlk");
199          break;
200        case Generic :
201          fprintf(stderr,"Generic");
202          break;
203        case EndRule :
204          fprintf(stderr,"EndRule");
205          break;
206      };
207      if (j->halt) fprintf(stderr,"  halt!");
208      if (j->p1) fprintf(stderr," p1 valid");
209      if (j->p2) {
210        if (j->p2->ntype == nJunction) {
211           fprintf(stderr," (p2=#%d)",( (Junction *) j->p2)->seq);
212        } else {
213           fprintf(stderr," (p2 valid)");
214        };
215      };
216	  if (j->ignore) fprintf(stderr, " ignore/plus-block-bypass");
217      if (j->fset != NULL && set_deg(*j->fset) != 0) {
218         fprintf(stderr,"\nfset:\n");
219         dumpset(*j->fset);
220      };
221      if (j->ftree != NULL) {
222         fprintf(stderr,"\nftree:\n");
223         preorder(j->ftree);
224      };
225      fprintf(stderr,"\n");
226      break;
227    case nRuleRef :
228       r = (RuleRefNode *) p;
229       fprintf(stderr, "RuleRefNode (in rule %s line %d) to rule %s\n", r->rname,r->line,r->text);
230       break;
231    case nToken :
232       t = (TokNode *) p;
233       fprintf(stderr, "TokNode (in rule %s line %d) token %s\n",t->rname,t->line,TerminalString(t->token));
234       break;
235    case nAction :
236       a =(ActionNode *) p;
237       if (a->is_predicate) {
238         fprintf(stderr, "Predicate (in rule %s line %d) %s",a->rname,a->line,a->action);
239         if (a->inverted) fprintf(stderr," action inverted !");
240         if (a->guardpred != NULL) {
241           fprintf(stderr," guarded");
242           dumppredexpr(a->guardpred);
243           if (a->ampersandPred) {
244             fprintf(stderr," \"&&\" style");
245           } else {
246             fprintf(stderr," \"=>\" style");
247           };
248         };
249         if (a->predEntry != NULL) fprintf(stderr," predEntry \"%s\" ",a->predEntry->str);
250         fprintf(stderr,"\n");
251       } else if (a->init_action) {
252         fprintf(stderr, "Init-Action (in rule %s line %d) %s\n",a->rname,a->line,a->action);
253       } else {
254         fprintf(stderr, "Action (in rule %s line %d) %s\n",a->rname,a->line,a->action);
255       };
256       break;
257   };
258}
259
260#ifdef __USE_PROTOS
261Node * dp1(Node * p)
262#else
263Node * dp1(p)
264  Node *    p;
265#endif
266{
267  Node  *result=NULL;
268
269  if (p->ntype == nJunction) {
270    result=( (Junction *) p )->p1;
271    d(result);
272  } else {
273    fprintf(stderr,"dp1: Not a Junction node");
274  };
275  return result;
276}
277
278#ifdef __USE_PROTOS
279Node * dp2(Node * p)
280#else
281Node * dp2(p)
282  Node *    p;
283#endif
284{
285  Node  *result=NULL;
286
287  if (p->ntype == nJunction) {
288    result=( (Junction *) p )->p2;
289    d(result);
290  } else {
291    fprintf(stderr,"dp2: Not a Junction node");
292  };
293  return result;
294}
295
296#ifdef __USE_PROTOS
297Node * dn(Node * p)
298#else
299Node * dn(p)
300  Node *    p;
301#endif
302
303{
304  Node  *result=NULL;
305
306  if (p->ntype == nRuleRef) {
307    result=( (RuleRefNode *)p )->next;
308  } else if (p->ntype == nAction) {
309    result=( (ActionNode *)p )->next;
310  } else if (p->ntype == nToken) {
311    result=( (TokNode *)p )->next;
312  } else {
313    fprintf(stderr,"No next field: Neither a RuleRefNode, ActionNode, nor TokNode");
314  };
315  if (result != NULL) d(result);
316  return result;
317}
318
319#ifdef __USE_PROTOS
320void df(Node * p)
321#else
322void df(p)
323  Node *    p;
324#endif
325{
326  int       count=0;
327  Node      *next;
328
329  fprintf(stderr,"\n#%d ",++count);
330  d(p);
331
332  for (next=p; next != NULL && !isEndRule(next) ; ) {
333    fprintf(stderr,"#%d ",++count);
334    if (next->ntype == nJunction) {
335      next=dp1(next);
336    } else {
337      next=dn(next);
338    };
339  };
340}
341
342#ifdef __USE_PROTOS
343Node * dfn(Node * p,int target)
344#else
345Node * dfn(p,target)
346  Node *    p;
347  int       target;
348#endif
349{
350  Node      *result=NULL;
351  int       count=0;
352  Node      *next;
353
354  fprintf(stderr,"#%d ",++count);
355  d(p);
356
357  for (next=p; next != NULL && !isEndRule(next) ; ) {
358    fprintf(stderr,"#%d ",++count);
359    if (next->ntype == nJunction) {
360      next=dp1(next);
361    } else {
362      next=dn(next);
363    };
364    if (count == target) {
365      result=next;
366      break;
367    };
368  };
369  return result;
370}
371
372
373static int findnodeMatch;
374
375#ifdef __USE_PROTOS
376Junction *findnode1(Node *n)
377#else
378Junction *findnode1(n)
379  Node  *n;
380#endif
381{
382   Node         *next;
383   Junction     *j;
384   Junction     *match;
385
386   if (n == NULL) return NULL;
387   if (n->ntype == nJunction) {
388     j=(Junction *) n;
389     if (j->seq == findnodeMatch) return j;
390     if (j->jtype == EndRule) return NULL;
391     if (j->jtype != RuleBlk && j->jtype != EndBlk) {
392       if (j->p2 != NULL && !j->ignore) {
393          match=findnode1(j->p2);
394          if (match != NULL) return match;
395       };
396     };
397   };
398   next=MR_advance(n);
399   return findnode1(next);
400}
401
402#ifdef __USE_PROTOS
403Junction *findnode(int match)
404#else
405Junction *findnode(match)
406  int   match;
407#endif
408{
409  Junction  *j;
410  Junction  *result=NULL;
411
412  findnodeMatch=match;
413
414  for (j=SynDiag; j != NULL; j=(Junction *)j->p2) {
415    require (j->ntype == nJunction && j->jtype == RuleBlk,"Not a rule block");
416    result=findnode1( (Node *) j);
417    if (result != NULL) break;
418  };
419  if (result != NULL) {
420    d( (Node *) result);
421  };
422  return result;
423}
424