home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts1.zip / ANTLR / FSET2.C < prev    next >
C/C++ Source or Header  |  1993-09-02  |  22KB  |  1,023 lines

  1. /*
  2.  * fset2.c
  3.  *
  4.  * $Id: fset2.c,v 1.8 1993/08/24 14:44:32 pccts Exp pccts $
  5.  * $Revision: 1.8 $
  6.  *
  7.  * Compute FIRST sets for full LL(k)
  8.  *
  9.  * SOFTWARE RIGHTS
  10.  *
  11.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  12.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  13.  * company may do whatever they wish with source code distributed with
  14.  * PCCTS or the code generated by PCCTS, including the incorporation of
  15.  * PCCTS, or its output, into commerical software.
  16.  * 
  17.  * We encourage users to develop software with PCCTS.  However, we do ask
  18.  * that credit is given to us for developing PCCTS.  By "credit",
  19.  * we mean that if you incorporate our source code into one of your
  20.  * programs (commercial product, research project, or otherwise) that you
  21.  * acknowledge this fact somewhere in the documentation, research report,
  22.  * etc...  If you like PCCTS and have developed a nice tool with the
  23.  * output, please mention that you developed it using PCCTS.  In
  24.  * addition, we ask that this header remain intact in our source code.
  25.  * As long as these guidelines are kept, we expect to continue enhancing
  26.  * this system and expect to make other tools available as they are
  27.  * completed.
  28.  *
  29.  * ANTLR 1.10
  30.  * Terence Parr
  31.  * Purdue University
  32.  * 1989-1993
  33.  */
  34. #include <stdio.h>
  35. #ifdef __cplusplus
  36. #ifndef __STDC__
  37. #define __STDC__
  38. #endif
  39. #endif
  40. #ifdef __STDC__
  41. #include <stdarg.h>
  42. #else
  43. #include <varargs.h>
  44. #endif
  45. #include "set.h"
  46. #include "syn.h"
  47. #include "hash.h"
  48. #include "generic.h"
  49. #include "dlgdef.h"
  50.  
  51. extern char tokens[];
  52.  
  53. /* ick! globals.  Used by permute() to track which elements of a set have been used */
  54. static int *findex;
  55. static set *fset;
  56. static unsigned **ftbl;
  57. static set *constrain; /* pts into fset. constrains tToken() to 'constrain' */
  58. int ConstrainSearch;
  59. static int maxk; /* set to initial k upon tree construction request */
  60. static Tree *FreeList = NULL;
  61.  
  62. #ifdef __STDC__
  63. Tree *tmake(Tree *root, ...);
  64. static int tmember_of_context(Tree *, Predicate *);
  65. #else
  66. Tree *tmake();
  67. static int tmember_of_context();
  68. #endif
  69.  
  70. /* Do root
  71.  * Then each sibling
  72.  */
  73. void
  74. #ifdef __STDC__
  75. preorder( Tree *tree )
  76. #else
  77. preorder( tree )
  78. Tree *tree;
  79. #endif
  80. {
  81.     if ( tree == NULL ) return;
  82.     if ( tree->down != NULL ) fprintf(stderr, " (");
  83.     if ( tree->token == ALT ) fprintf(stderr, " J");
  84.     else fprintf(stderr, " %s", TerminalString(tree->token));
  85.     if ( tree->token==EpToken ) fprintf(stderr, "(%d)", tree->v.rk);
  86.     preorder(tree->down);
  87.     if ( tree->down != NULL ) fprintf(stderr, " )");
  88.     preorder(tree->right);
  89. }
  90.  
  91. /* check the depth of each primary sibling to see that it is exactly
  92.  * k deep. e.g.;
  93.  *
  94.  *    ALT
  95.  *   |
  96.  *   A ------- B
  97.  *   |         |
  98.  *   C -- D    E
  99.  *
  100.  * Remove all branches <= k deep.
  101.  *
  102.  * Added by TJP 9-23-92 to correct the LL(k) constraint mechanism to work.
  103.  */
  104. Tree *
  105. #ifdef __STDC__
  106. prune( Tree *t, int k )
  107. #else
  108. prune( t, k )
  109. Tree *t;
  110. int k;
  111. #endif
  112. {
  113.     if ( t == NULL ) return NULL;
  114.     if ( t->token == ALT ) fatal("prune: ALT node in FIRST tree");
  115.     t->right = prune(t->right, k);
  116.     t->down = prune(t->down, k-1);
  117.     if ( t->down == NULL && k > 1 )
  118.     {
  119.         Tree *r = t->right;
  120.         t->right = NULL;
  121.         Tfree(t);
  122.         return r;
  123.     }
  124.     return t;
  125. }
  126.  
  127. /* build a tree (root child1 child2 ... NULL) */
  128. #ifdef __STDC__
  129. Tree *tmake(Tree *root, ...)
  130. #else
  131. Tree *tmake(va_alist)
  132. va_dcl
  133. #endif
  134. {
  135.     va_list ap;
  136.     Tree *child, *sibling=NULL, *tail;
  137. #ifndef __STDC__
  138.     Tree *root;
  139. #endif
  140. #ifdef __STDC__
  141.     require(root!=NULL, "tmake: NULL root impossible");
  142. #endif
  143.  
  144. #ifdef __STDC__
  145.     va_start(ap, root);
  146. #else
  147.     va_start(ap);
  148.     root = va_arg(ap, Tree *);
  149. #endif
  150.     child = va_arg(ap, Tree *);
  151.     while ( child != NULL )
  152.     {
  153.         if ( sibling == NULL ) sibling = tail = child;
  154.         else {tail->right = child; tail = child;}
  155.         child = va_arg(ap, Tree *);
  156.     }
  157.     root->down = sibling;
  158.     va_end(ap);
  159.     return root;
  160. }
  161.  
  162. Tree *
  163. #ifdef __STDC__
  164. tnode( int tok )
  165. #else
  166. tnode( tok )
  167. int tok;
  168. #endif
  169. {
  170.     Tree *p, *newblk;
  171.     static int n=0;
  172.     
  173.     if ( FreeList == NULL )
  174.     {
  175.         /*fprintf(stderr, "tnode: %d more nodes\n", TreeBlockAllocSize);*/
  176.         if ( TreeResourceLimit > 0 )
  177.         {
  178.             if ( (n+TreeBlockAllocSize) >= TreeResourceLimit )
  179.             {
  180.                 fprintf(stderr, ErrHdr, FileStr[CurAmbigfile], CurAmbigline);
  181.                 fprintf(stderr, " hit analysis resource limit while analyzing alts %d and %d %s\n",
  182.                                 CurAmbigAlt1,
  183.                                 CurAmbigAlt2,
  184.                                 CurAmbigbtype);
  185.                 exit(1);
  186.             }
  187.         }
  188.         newblk = (Tree *)calloc(TreeBlockAllocSize, sizeof(Tree));
  189.         if ( newblk == NULL )
  190.         {
  191.             fprintf(stderr, ErrHdr, FileStr[CurAmbigfile], CurAmbigline);
  192.             fprintf(stderr, " out of memory while analyzing alts %d and %d %s\n",
  193.                             CurAmbigAlt1,
  194.                             CurAmbigAlt2,
  195.                             CurAmbigbtype);
  196.             exit(1);
  197.         }
  198.         n += TreeBlockAllocSize;
  199.         for (p=newblk; p<&(newblk[TreeBlockAllocSize]); p++)
  200.         {
  201.             p->right = FreeList;    /* add all new Tree nodes to Free List */
  202.             FreeList = p;
  203.         }
  204.     }
  205.     p = FreeList;
  206.     FreeList = FreeList->right;        /* remove a tree node */
  207.     p->right = NULL;                /* zero out ptrs */
  208.     p->down = NULL;
  209.     p->token = tok;
  210.     return p;
  211. }
  212.  
  213. static Tree *
  214. #ifdef __STDC__
  215. eofnode( int k )
  216. #else
  217. eofnode( k )
  218. int k;
  219. #endif
  220. {
  221.     Tree *t=NULL;
  222.     int i;
  223.  
  224.     for (i=1; i<=k; i++)
  225.     {
  226.         t = tmake(tnode(EofToken), t, NULL);
  227.     }
  228.     return t;
  229. }
  230.  
  231.  
  232.  
  233. void
  234. #ifdef __STDC__
  235. _Tfree( Tree *t )
  236. #else
  237. _Tfree( t )
  238. Tree *t;
  239. #endif
  240. {
  241.     if ( t!=NULL )
  242.     {
  243.         t->right = FreeList;
  244.         FreeList = t;
  245.     }
  246. }
  247.  
  248. /* tree duplicate */
  249. Tree *
  250. #ifdef __STDC__
  251. tdup( Tree *t )
  252. #else
  253. tdup( t )
  254. Tree *t;
  255. #endif
  256. {
  257.     Tree *u;
  258.     
  259.     if ( t == NULL ) return NULL;
  260.     u = tnode(t->token);
  261.     u->v.rk = t->v.rk;
  262.     u->right = tdup(t->right);
  263.     u->down = tdup(t->down);
  264.     return u;
  265. }
  266.  
  267. /* tree duplicate (assume tree is a chain downwards) */
  268. Tree *
  269. #ifdef __STDC__
  270. tdup_chain( Tree *t )
  271. #else
  272. tdup_chain( t )
  273. Tree *t;
  274. #endif
  275. {
  276.     Tree *u;
  277.     
  278.     if ( t == NULL ) return NULL;
  279.     u = tnode(t->token);
  280.     u->v.rk = t->v.rk;
  281.     u->down = tdup(t->down);
  282.     return u;
  283. }
  284.  
  285. Tree *
  286. #ifdef __STDC__
  287. tappend( Tree *t, Tree *u )
  288. #else
  289. tappend( t, u )
  290. Tree *t;
  291. Tree *u;
  292. #endif
  293. {
  294.     Tree *w;
  295.  
  296.     /*fprintf(stderr, "tappend(");
  297.     preorder(t); fprintf(stderr, ",");
  298.     preorder(u); fprintf(stderr, " )\n");*/
  299.     if ( t == NULL ) return u;
  300.     if ( t->token == ALT && t->right == NULL ) return tappend(t->down, u);
  301.     for (w=t; w->right!=NULL; w=w->right) {;}
  302.     w->right = u;
  303.     return t;
  304. }
  305.  
  306. /* dealloc all nodes in a tree */
  307. void
  308. #ifdef __STDC__
  309. Tfree( Tree *t )
  310. #else
  311. Tfree( t )
  312. Tree *t;
  313. #endif
  314. {
  315.     if ( t == NULL ) return;
  316.     Tfree( t->down );
  317.     Tfree( t->right );
  318.     _Tfree( t );
  319. }
  320.  
  321. /* find all children (alts) of t that require remaining_k nodes to be LL_k
  322.  * tokens long.
  323.  *
  324.  * t-->o
  325.  *     |
  326.  *     a1--a2--...--an        <-- LL(1) tokens
  327.  *     |   |        |
  328.  *     b1  b2  ...  bn        <-- LL(2) tokens
  329.  *     |   |        |
  330.  *     .   .        .
  331.  *     .   .        .
  332.  *     z1  z2  ...  zn        <-- LL(LL_k) tokens
  333.  *
  334.  * We look for all [Ep] needing remaining_k nodes and replace with u.
  335.  * u is not destroyed or actually used by the tree (a copy is made).
  336.  */
  337. Tree *
  338. #ifdef __STDC__
  339. tlink( Tree *t, Tree *u, int remaining_k )
  340. #else
  341. tlink( t, u, remaining_k )
  342. Tree *t;
  343. Tree *u;
  344. int remaining_k;
  345. #endif
  346. {
  347.     Tree *p;
  348.     require(remaining_k!=0, "tlink: bad tree");
  349.  
  350.     if ( t==NULL ) return NULL;
  351.     /*fprintf(stderr, "tlink: u is:"); preorder(u); fprintf(stderr, "\n");*/
  352.     if ( t->token == EpToken && t->v.rk == remaining_k )
  353.     {
  354.         require(t->down==NULL, "tlink: invalid tree");
  355.         if ( u == NULL ) return t->right;
  356.         p = tdup( u );
  357.         p->right = t->right;
  358.         _Tfree( t );
  359.         return p;
  360.     }
  361.     t->down = tlink(t->down, u, remaining_k);
  362.     t->right = tlink(t->right, u, remaining_k);
  363.     return t;
  364. }
  365.  
  366. /* remove as many ALT nodes as possible while still maintaining semantics */
  367. Tree *
  368. #ifdef __STDC__
  369. tshrink( Tree *t )
  370. #else
  371. tshrink( t )
  372. Tree *t;
  373. #endif
  374. {
  375.     if ( t == NULL ) return NULL;
  376.     t->down = tshrink( t->down );
  377.     t->right = tshrink( t->right );
  378.     if ( t->down == NULL )
  379.     {
  380.         if ( t->token == ALT )
  381.         {
  382.             Tree *u = t->right;
  383.             _Tfree(t);
  384.             return u;            /* remove useless alts */
  385.         }
  386.         return t;
  387.     }
  388.  
  389.     /* (? (ALT (? ...)) s) ==> (? (? ...) s) where s = sibling, ? = match any */
  390.     if ( t->token == ALT && t->down->right == NULL)
  391.     {
  392.         Tree *u = t->down;
  393.         u->right = t->right;
  394.         _Tfree( t );
  395.         return u;
  396.     }
  397.     /* (? (A (ALT t)) s) ==> (? (A t) s) where A is a token; s,t siblings */
  398.     if ( t->token != ALT && t->down->token == ALT && t->down->right == NULL )
  399.     {
  400.         Tree *u = t->down->down;
  401.         _Tfree( t->down );
  402.         t->down = u;
  403.         return t;
  404.     }
  405.     return t;
  406. }
  407.  
  408. Tree *
  409. #ifdef __STDC__
  410. tflatten( Tree *t )
  411. #else
  412. tflatten( t )
  413. Tree *t;
  414. #endif
  415. {
  416.     if ( t == NULL ) return NULL;
  417.     t->down = tflatten( t->down );
  418.     t->right = tflatten( t->right );
  419.     if ( t->down == NULL ) return t;
  420.     
  421.     if ( t->token == ALT )
  422.     {
  423.         Tree *u;
  424.         /* find tail of children */
  425.         for (u=t->down; u->right!=NULL; u=u->right) {;}
  426.         u->right = t->right;
  427.         u = t->down;
  428.         _Tfree( t );
  429.         return u;
  430.     }
  431.     return t;
  432. }
  433.  
  434. Tree *
  435. #ifdef __STDC__
  436. tJunc( Junction *p, int k, set *rk )
  437. #else
  438. tJunc( p, k, rk )
  439. Junction *p;
  440. int k;
  441. set *rk;
  442. #endif
  443. {
  444.     Tree *t=NULL, *u=NULL;
  445.     Junction *alt;
  446.     Tree *tail, *r;
  447.  
  448. #ifdef DBG_TRAV
  449.     fprintf(stderr, "tJunc(%d): %s in rule %s\n", k,
  450.             decodeJType[p->jtype], ((Junction *)p)->rname);
  451. #endif
  452.     if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  453.          p->jtype==aPlusBlk || p->jtype==aSubBlk || p->jtype==aOptBlk )
  454.     {
  455.         if ( p->jtype!=aSubBlk && p->jtype!=aOptBlk ) {
  456.             require(p->lock!=NULL, "rJunc: lock array is NULL");
  457.             if ( p->lock[k] ) return NULL;
  458.             p->lock[k] = TRUE;
  459.         }
  460.         TRAV(p->p1, k, rk, tail);
  461.         if ( p->jtype==RuleBlk ) {p->lock[k] = FALSE; return tail;}
  462.         r = tmake(tnode(ALT), tail, NULL);
  463.         for (alt=(Junction *)p->p2; alt!=NULL; alt = (Junction *)alt->p2)
  464.         {
  465.             if ( tail==NULL ) {TRAV(alt->p1, k, rk, tail); r->down = tail;}
  466.             else
  467.             {
  468.                 TRAV(alt->p1, k, rk, tail->right);
  469.                 /* TJP -- lost info when tail->right was NULL
  470.                 tail = tail->right;*/
  471.                 if ( tail->right != NULL ) tail = tail->right;
  472.             }
  473.         }
  474.         if ( p->jtype!=aSubBlk && p->jtype!=aOptBlk ) p->lock[k] = FALSE;
  475. #ifdef DBG_TREES
  476.         fprintf(stderr, "blk(%s) returns:",((Junction *)p)->rname); preorder(r); fprintf(stderr, "\n");
  477. #endif
  478.         if ( r->down == NULL ) {_Tfree(r); return NULL;}
  479.         return r;
  480.     }
  481.  
  482.     if ( p->jtype==EndRule )
  483.     {
  484.         if ( p->halt )                        /* don't want FOLLOW here? */
  485.         {
  486.             set_orel(k, rk);                /* indicate this k value needed */
  487.             t = tnode(EpToken);
  488.             t->v.rk = k;
  489.             return t;
  490.         }
  491.         require(p->lock!=NULL, "rJunc: lock array is NULL");
  492.         if ( p->lock[k] ) return NULL;
  493.         /* if no FOLLOW assume k EOF's */
  494.         if ( p->p1 == NULL ) return eofnode(k);
  495.         p->lock[k] = TRUE;
  496.     }
  497.  
  498.     if ( p->p2 == NULL )
  499.     {
  500.         TRAV(p->p1, k, rk,t);
  501.         if ( p->jtype==EndRule ) p->lock[k]=FALSE;
  502.         return t;
  503.     }
  504.     TRAV(p->p1, k, rk, t);
  505.     if ( p->jtype!=RuleBlk ) TRAV(p->p2, k, rk, u);
  506.     if ( p->jtype==EndRule ) p->lock[k] = FALSE;/* unlock node */
  507.  
  508.     if ( t==NULL ) return tmake(tnode(ALT), u, NULL);
  509.     return tmake(tnode(ALT), t, u, NULL);
  510. }
  511.  
  512. Tree *
  513. #ifdef __STDC__
  514. tRuleRef( RuleRefNode *p, int k, set *rk_out )
  515. #else
  516. tRuleRef( p, k, rk_out )
  517. RuleRefNode *p;
  518. int k;
  519. set *rk_out;
  520. #endif
  521.     int k2;
  522.     Tree *t, *u;
  523.     Junction *r;
  524.     set rk, rk2;
  525.     int save_halt;
  526.     RuleEntry *q = (RuleEntry *) hash_get(Rname, p->text);
  527.     
  528. #ifdef DBG_TRAV
  529.     fprintf(stderr, "tRuleRef: %s\n", p->text);
  530. #endif
  531.     if ( q == NULL )
  532.     {
  533.         TRAV(p->next, k, rk_out, t);/* ignore undefined rules */
  534.         return t;
  535.     }
  536.     rk = rk2 = empty;
  537.     r = RulePtr[q->rulenum];
  538.     if ( r->lock[k] ) return NULL;
  539.     save_halt = r->end->halt;
  540.     r->end->halt = TRUE;        /* don't let reach fall off end of rule here */
  541.     TRAV(r, k, &rk, t);
  542.     r->end->halt = save_halt;
  543. #ifdef DBG_TREES
  544.     fprintf(stderr, "after ruleref, t is:"); preorder(t); fprintf(stderr, "\n");
  545. #endif
  546.     t = tshrink( t );
  547.     while ( !set_nil(rk) ) {    /* any k left to do? if so, link onto tree */
  548.         k2 = set_int(rk);
  549.         set_rm(k2, rk);
  550.         TRAV(p->next, k2, &rk2, u);
  551.         t = tlink(t, u, k2);    /* any alts missing k2 toks, add u onto end */
  552.     }
  553.     set_free(rk);                /* rk is empty, but free it's memory */
  554.     set_orin(rk_out, rk2);        /* remember what we couldn't do */
  555.     set_free(rk2);
  556.     return t;
  557. }
  558.  
  559. Tree *
  560. #ifdef __STDC__
  561. tToken( TokNode *p, int k, set *rk )
  562. #else
  563. tToken( p, k, rk )
  564. TokNode *p;
  565. int k;
  566. set *rk;
  567. #endif
  568. {
  569.     Tree *t;
  570.  
  571.     if ( ConstrainSearch )
  572.     {
  573.         require(constrain>=fset&&constrain<=&(fset[LL_k]),"tToken: constrain is not a valid set");
  574.         constrain = &fset[maxk-k+1];
  575.     }
  576.  
  577. #ifdef DBG_TRAV
  578.     fprintf(stderr, "tToken(%d): %s\n", k, TerminalString(p->token));
  579.     if ( ConstrainSearch ) {
  580.         fprintf(stderr, "constrain is:"); s_fprT(stderr, *constrain); fprintf(stderr, "\n");
  581.     }
  582. #endif
  583.     if ( ConstrainSearch )
  584.         if ( !set_el(p->token, *constrain) )
  585.         {
  586. /*            fprintf(stderr, "ignoring token %s(%d)\n", TerminalString(p->token),
  587.                             k);*/
  588.             return NULL;
  589.         }
  590.     if ( k == 1 ) return tnode(p->token);
  591.     TRAV(p->next, k-1, rk, t);
  592.     /* here, we are positive that, at least, this tree will not contribute
  593.      * to the LL(2) tree since it will be too shallow, IF t==NULL
  594.      */
  595.     if ( t == NULL )    /* tree will be too shallow */
  596.     {
  597.         return NULL;
  598.     }
  599. #ifdef DBG_TREES
  600.     fprintf(stderr, "tToken(%d)->next:",k); preorder(t); fprintf(stderr, "\n");
  601. #endif
  602.     return tmake(tnode(p->token), t, NULL);
  603. }
  604.  
  605. Tree *
  606. #ifdef __STDC__
  607. tAction( ActionNode *p, int k, set *rk )
  608. #else
  609. tAction( p, k, rk )
  610. ActionNode *p;
  611. int k;
  612. set *rk;
  613. #endif
  614. {
  615.     Tree *t;
  616.     
  617.     /*fprintf(stderr, "tAction\n");*/
  618.     TRAV(p->next, k, rk, t);
  619.     return t;
  620. }
  621.  
  622. /* see if e exists in s as a possible input permutation (e is always a chain) */
  623. int
  624. #ifdef __STDC__
  625. tmember( Tree *e, Tree *s )
  626. #else
  627. tmember( e, s )
  628. Tree *e;
  629. Tree *s;
  630. #endif
  631. {
  632.     if ( e==NULL||s==NULL ) return 0;
  633.     /*fprintf(stderr, "tmember(");
  634.     preorder(e); fprintf(stderr, ",");
  635.     preorder(s); fprintf(stderr, " )\n");*/
  636.     if ( s->token == ALT && s->right == NULL ) return tmember(e, s->down);
  637.     if ( e->token!=s->token )
  638.     {
  639.         if ( s->right==NULL ) return 0;
  640.         return tmember(e, s->right);
  641.     }
  642.     if ( e->down==NULL && s->down == NULL ) return 1;
  643.     if ( tmember(e->down, s->down) ) return 1;
  644.     if ( s->right==NULL ) return 0;
  645.     return tmember(e, s->right);
  646. }
  647.  
  648. /* combine (? (A t) ... (A u) ...) into (? (A t u)) */
  649. Tree *
  650. #ifdef __STDC__
  651. tleft_factor( Tree *t )
  652. #else
  653. tleft_factor( t )
  654. Tree *t;
  655. #endif
  656. {
  657.     Tree *u, *v, *trail, *w;
  658.  
  659.     /* left-factor what is at this level */
  660.     if ( t == NULL ) return NULL;
  661.     for (u=t; u!=NULL; u=u->right)
  662.     {
  663.         trail = u;
  664.         v=u->right;
  665.         while ( v!=NULL )
  666.         {
  667.             if ( u->token == v->token )
  668.             {
  669.                 if ( u->down!=NULL )
  670.                 {
  671.                     for (w=u->down; w->right!=NULL; w=w->right) {;}
  672.                     w->right = v->down;    /* link children together */
  673.                 }
  674.                 else u->down = v->down;
  675.                 trail->right = v->right;        /* unlink factored node */
  676.                 _Tfree( v );
  677.                 v = trail->right;
  678.             }
  679.             else {trail = v; v=v->right;}
  680.         }
  681.     }
  682.     /* left-factor what is below */
  683.     for (u=t; u!=NULL; u=u->right) u->down = tleft_factor( u->down );
  684.     return t;
  685. }
  686.  
  687. /* remove the permutation p from t if present */
  688. Tree *
  689. #ifdef __STDC__
  690. trm_perm( Tree *t, Tree *p )
  691. #else
  692. trm_perm( t, p )
  693. Tree *t;
  694. Tree *p;
  695. #endif
  696. {
  697.     /*
  698.     fprintf(stderr, "trm_perm(");
  699.     preorder(t); fprintf(stderr, ",");
  700.     preorder(p); fprintf(stderr, " )\n");
  701.     */
  702.     if ( t == NULL || p == NULL ) return NULL;
  703.     if ( t->token == ALT )
  704.     {
  705.         t->down = trm_perm(t->down, p);
  706.         if ( t->down == NULL )                 /* nothing left below, rm cur node */
  707.         {
  708.             Tree *u = t->right;
  709.             _Tfree( t );
  710.             return trm_perm(u, p);
  711.         }
  712.         t->right = trm_perm(t->right, p);    /* look for more instances of p */
  713.         return t;
  714.     }
  715.     if ( p->token != t->token )                /* not found, try a sibling */
  716.     {
  717.         t->right = trm_perm(t->right, p);
  718.         return t;
  719.     }
  720.     t->down = trm_perm(t->down, p->down);
  721.     if ( t->down == NULL )                     /* nothing left below, rm cur node */
  722.     {
  723.         Tree *u = t->right;
  724.         _Tfree( t );
  725.         return trm_perm(u, p);
  726.     }
  727.     t->right = trm_perm(t->right, p);        /* look for more instances of p */
  728.     return t;
  729. }
  730.  
  731. /* add the permutation 'perm' to the LL_k sets in 'fset' */
  732. void
  733. #ifdef __STDC__
  734. tcvt( set *fset, Tree *perm )
  735. #else
  736. tcvt( fset, perm )
  737. set *fset;
  738. Tree *perm;
  739. #endif
  740. {
  741.     if ( perm==NULL ) return;
  742.     set_orel(perm->token, fset);
  743.     tcvt(fset+1, perm->down);
  744. }
  745.  
  746. /* for each element of ftbl[k], make it the root of a tree with permute(ftbl[k+1])
  747.  * as a child.
  748.  */
  749. Tree *
  750. #ifdef __STDC__
  751. permute( int k )
  752. #else
  753. permute( k )
  754. int k;
  755. #endif
  756. {
  757.     Tree *t, *u;
  758.     
  759.     if ( k>LL_k ) return NULL;
  760.     if ( ftbl[k][findex[k]] == nil ) return NULL;
  761.     t = permute(k+1);
  762.     if ( t==NULL&&k<LL_k )        /* no permutation left below for k+1 tokens? */
  763.     {
  764.         findex[k+1] = 0;
  765.         (findex[k])++;            /* try next token at this k */
  766.         return permute(k);
  767.     }
  768.     
  769.     u = tmake(tnode(ftbl[k][findex[k]]), t, NULL);
  770.     if ( k == LL_k ) (findex[k])++;
  771.     return u;
  772. }
  773.  
  774. /* Compute LL(k) trees for alts alt1 and alt2 of p.
  775.  * function result is tree of ambiguous input permutations
  776.  *
  777.  * ALGORITHM may change to look for something other than LL_k size
  778.  * trees ==> maxk will have to change.
  779.  */
  780. Tree *
  781. #ifdef __STDC__
  782. VerifyAmbig( Junction *alt1, Junction *alt2, unsigned **ft, set *fs, Tree **t, Tree **u, int *numAmbig )
  783. #else
  784. VerifyAmbig( alt1, alt2, ft, fs, t, u, numAmbig )
  785. Junction *alt1;
  786. Junction *alt2;
  787. unsigned **ft;
  788. set *fs;
  789. Tree **t;
  790. Tree **u;
  791. int *numAmbig;
  792. #endif
  793. {
  794.     set rk;
  795.     Tree *perm, *ambig=NULL;
  796.     Junction *p;
  797.     int k;
  798.  
  799.     maxk = LL_k;                /* NOTE: for now, we look for LL_k */
  800.     ftbl = ft;
  801.     fset = fs;
  802.     constrain = &(fset[1]);
  803.     findex = (int *) calloc(LL_k+1, sizeof(int));
  804.     if ( findex == NULL )
  805.     {
  806.         fprintf(stderr, ErrHdr, CurAmbigfile, CurAmbigline);
  807.         fprintf(stderr, " out of memory while analyzing alts %d and %d of %s\n",
  808.                         CurAmbigAlt1,
  809.                         CurAmbigAlt2,
  810.                         CurAmbigbtype);
  811.         exit(1);
  812.     }
  813.     for (k=1; k<=LL_k; k++) findex[k] = 0;
  814.  
  815.     rk = empty;
  816.     ConstrainSearch = 1;    /* consider only tokens in ambig sets */
  817.  
  818.     p = analysis_point((Junction *)alt1->p1);
  819.     TRAV(p, LL_k, &rk, *t);
  820.     *t = tshrink( *t );
  821.     *t = tflatten( *t );
  822.     *t = prune(*t, LL_k);
  823.     *t = tleft_factor( *t );
  824. /*    fprintf(stderr, "after shrink&flatten&prune&left_factor:"); preorder(*t); fprintf(stderr, "\n");*/
  825.     if ( *t == NULL )
  826.     {
  827. /*        fprintf(stderr, "TreeIncomplete --> no LL(%d) ambiguity\n", LL_k);*/
  828.         Tfree( *t );    /* kill if impossible to have ambig */
  829.         *t = NULL;
  830.     }
  831.  
  832.     p = analysis_point((Junction *)alt2->p1);
  833.     TRAV(p, LL_k, &rk, *u);
  834.     *u = tshrink( *u );
  835.     *u = tflatten( *u );
  836.     *u = prune(*u, LL_k);
  837.     *u = tleft_factor( *u );
  838. /*    fprintf(stderr, "after shrink&flatten&prune&lfactor:"); preorder(*u); fprintf(stderr, "\n");*/
  839.     if ( *u == NULL )
  840.     {
  841. /*        fprintf(stderr, "TreeIncomplete --> no LL(%d) ambiguity\n", LL_k);*/
  842.         Tfree( *u );
  843.         *u = NULL;
  844.     }
  845.  
  846.     for (k=1; k<=LL_k; k++) set_clr( fs[k] );
  847.  
  848.     ambig = tnode(ALT);
  849.     k = 0;
  850.     if ( *t!=NULL && *u!=NULL )
  851.     {
  852.         while ( (perm=permute(1))!=NULL )
  853.         {
  854. /*            fprintf(stderr, "chk perm:"); preorder(perm); fprintf(stderr, "\n");*/
  855.             if ( tmember(perm, *t) && tmember(perm, *u) )
  856.             {
  857. /*                fprintf(stderr, "ambig upon"); preorder(perm); fprintf(stderr, "\n");*/
  858.                 k++;
  859.                 perm->right = ambig->down;
  860.                 ambig->down = perm;
  861.                 tcvt(&(fs[1]), perm);
  862.             }
  863.             else Tfree( perm );
  864.         }
  865.     }
  866.  
  867.     *numAmbig = k;
  868.     if ( ambig->down == NULL ) {_Tfree(ambig); ambig = NULL;}
  869.     free( findex );
  870.     /*fprintf(stderr, "final ambig:"); preorder(ambig); fprintf(stderr, "\n");*/
  871.     return ambig;
  872. }
  873.  
  874. static Tree *
  875. #ifdef __STDC__
  876. bottom_of_chain( Tree *t )
  877. #else
  878. bottom_of_chain( t )
  879. Tree *t;
  880. #endif
  881. {
  882.     if ( t==NULL ) return NULL;
  883.     for (; t->down != NULL; t=t->down) {;}
  884.     return t;
  885. }
  886.  
  887. /*
  888.  * Make a tree from k sets where the degree of the first k-1 sets is 1.
  889.  */
  890. Tree *
  891. #ifdef __STDC__
  892. make_tree_from_sets( set *fset1, set *fset2 )
  893. #else
  894. make_tree_from_sets( fset1, fset2 )
  895. set *fset1;
  896. set *fset2;
  897. #endif
  898. {
  899.     set inter;
  900.     int i;
  901.     Tree *t=NULL, *n, *u;
  902.     unsigned *p,*q;
  903.     require(LL_k>1, "make_tree_from_sets: LL_k must be > 1");
  904.  
  905.     /* do the degree 1 sets first */
  906.     for (i=1; i<=LL_k-1; i++)
  907.     {
  908.         inter = set_and(fset1[i], fset2[i]);
  909.         require(set_deg(inter)==1, "invalid set to tree conversion");
  910.         n = tnode(set_int(inter));
  911.         if (t==NULL) t=n; else tmake(t, n, NULL);
  912.         set_free(inter);
  913.     }
  914.  
  915.     /* now add the chain of tokens at depth k */
  916.     u = bottom_of_chain(t);
  917.     inter = set_and(fset1[LL_k], fset2[LL_k]);
  918.     if ( (q=p=set_pdq(inter)) == NULL ) fatal("Can't alloc space for set_pdq");
  919.     /* first one is linked to bottom, then others are sibling linked */
  920.     n = tnode(*p++);
  921.     u->down = n;
  922.     u = u->down;
  923.     while ( *p != nil )
  924.     {
  925.         n = tnode(*p);
  926.         u->right = n;
  927.         u = u->right;
  928.         p++;
  929.     }
  930.     free(q);
  931.  
  932.     return t;
  933. }
  934.  
  935. /* create and return the tree of lookahead k-sequences that are in t, but not
  936.  * in the context of predicates in predicate list p.
  937.  */
  938. Tree *
  939. #ifdef __STDC__
  940. tdif( Tree *t, Predicate *p, set *fset1, set *fset2 )
  941. #else
  942. tdif( t, p, fset1, fset2 )
  943. Tree *t;
  944. Predicate *p;
  945. set *fset1;
  946. set *fset2;
  947. #endif
  948. {
  949.     unsigned **ft;
  950.     Tree *dif=NULL, *last=NULL;
  951.     Tree *perm;
  952.     set b;
  953.     int i,k;
  954.  
  955.     if ( p == NULL ) return tdup(t);
  956.  
  957.     ft = (unsigned **) calloc(CLL_k+1, sizeof(unsigned *));
  958.     require(ft!=NULL, "cannot allocate ft");
  959.     for (i=1; i<=CLL_k; i++)
  960.     {
  961.         b = set_and(fset1[i], fset2[i]);
  962.         ft[i] = set_pdq(b);
  963.         set_free(b);
  964.     }
  965.     findex = (int *) calloc(LL_k+1, sizeof(int));
  966.     if ( findex == NULL )
  967.     {
  968.         fatal("out of memory in tdif while checking predicates");
  969.     }
  970.     for (k=1; k<=LL_k; k++) findex[k] = 0;
  971.  
  972. /*    fprintf(stderr, "tdif[");
  973.     preorder(t);
  974.     fprintf(stderr, ",");
  975.     preorder(p->tcontext);
  976.     fprintf(stderr, "] =");*/
  977.  
  978.     ftbl = ft;
  979.     while ( (perm=permute(1))!=NULL )
  980.     {
  981. /*        fprintf(stderr, "test perm:"); preorder(perm); fprintf(stderr, "\n");*/
  982.         if ( tmember(perm, t) && !tmember_of_context(perm, p) )
  983.         {
  984. /*            fprintf(stderr, "satisfied upon"); preorder(perm); fprintf(stderr, "\n");*/
  985.             k++;
  986.             if ( dif==NULL ) dif = perm;
  987.             else
  988.             {
  989.                 perm->right = dif;
  990.                 dif = perm;
  991.             }
  992.         }
  993.         else Tfree( perm );
  994.     }
  995.  
  996. /*    preorder(dif);
  997.     fprintf(stderr, "\n");*/
  998.  
  999.     for (i=1; i<=CLL_k; i++) free( ft[i] );
  1000.     free(ft);
  1001.     free(findex);
  1002.  
  1003.     return dif;
  1004. }
  1005.  
  1006. /* is lookahead sequence t a member of any context tree for any predicate in p? */
  1007. static int
  1008. #ifdef __STDC__
  1009. tmember_of_context( Tree *t, Predicate *p )
  1010. #else
  1011. tmember_of_context( t, p )
  1012. Tree *t;
  1013. Predicate *p;
  1014. #endif
  1015. {
  1016.     for (; p!=NULL; p=p->next)
  1017.     {
  1018.         if ( tmember(t, p->tcontext) ) return 1;
  1019.     }
  1020.     return 0;
  1021. }
  1022.