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

  1. /*
  2.  * fset.c
  3.  *
  4.  * $Id: fset.c,v 1.13 1993/08/24 14:44:32 pccts Exp pccts $
  5.  * $Revision: 1.13 $
  6.  *
  7.  * Compute FIRST and FOLLOW sets.
  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. #include "set.h"
  41. #include "syn.h"
  42. #include "hash.h"
  43. #include "generic.h"
  44. #include "dlgdef.h"
  45.  
  46. #ifdef __STDC__
  47. static void ensure_predicates_cover_ambiguous_lookahead_sequences(Junction *, Junction *, char *, Tree *);
  48. #else
  49. static void ensure_predicates_cover_ambiguous_lookahead_sequences();
  50. #endif
  51.  
  52. /*
  53.  * What tokens are k tokens away from junction q?
  54.  *
  55.  * Follow both p1 and p2 paths (unless RuleBlk) to collect the tokens k away from this
  56.  * node.
  57.  * We lock the junction according to k--the lookahead.  If we have been at this
  58.  * junction before looking for the same, k, number of lookahead tokens, we will
  59.  * do it again and again...until we blow up the stack.  Locks are only used on aLoopBlk,
  60.  * RuleBlk, aPlusBlk and EndRule junctions to remove/detect infinite recursion from
  61.  * FIRST and FOLLOW calcs.
  62.  *
  63.  * If p->jtype == EndRule we are going to attempt a FOLLOW.  (FOLLOWs are really defined
  64.  * in terms of FIRST's, however).  To proceed with the FOLLOW, p->halt cannot be
  65.  * set.  p->halt is set to indicate that a reference to the current rule is in progress
  66.  * and the FOLLOW is not desirable.
  67.  *
  68.  * If we attempt a FOLLOW and find that there is no FOLLOW or REACHing beyond the EndRule
  69.  * junction yields an empty set, replace the empty set with EOF.  No FOLLOW means that
  70.  * only EOF can follow the current rule.  This normally occurs only on the start symbol
  71.  * since all other rules are referenced by another rule somewhere.
  72.  *
  73.  * Normally, both p1 and p2 are followed.  However, checking p2 on a RuleBlk node is
  74.  * the same as checking the next rule which is clearly incorrect.
  75.  *
  76.  * Cycles in the FOLLOW sense are possible.  e.g. Fo(c) requires Fo(b) which requires
  77.  * Fo(c).  Both Fo(b) and Fo(c) are defined to be Fo(b) union Fo(c).  Let's say
  78.  * Fo(c) is attempted first.  It finds all of the FOLLOW symbols and then attempts
  79.  * to do Fo(b) which finds of its FOLLOW symbols.  So, we have:
  80.  *
  81.  *                  Fo(c)
  82.  *                 /     \
  83.  *              a set    Fo(b)
  84.  *                      /     \
  85.  *                   a set    Fo(c) .....Hmmmm..... Infinite recursion!
  86.  *
  87.  * The 2nd Fo(c) is not attempted and Fo(b) is left deficient, but Fo(c) is now
  88.  * correctly Fo(c) union Fo(b).  We wish to pick up where we left off, so the fact
  89.  * that Fo(b) terminated early means that we lack Fo(c) in the Fo(b) set already
  90.  * laying around.  SOOOOoooo, we track FOLLOW cycles.  All FOLLOW computations are
  91.  * cached in a hash table.  After the sequence of FOLLOWs finish, we reconcile all
  92.  * cycles --> correct all Fo(rule) sets in the cache.
  93.  *
  94.  * Confused? Good! Read my MS thesis [Purdue Technical Report TR90-30].
  95.  * TJP 8/93 -- can now read PhD thesis from Purdue.
  96.  *
  97.  * Also, FIRST sets are cached in the hash table.  Keys are (rulename,Fi/Fo,k).
  98.  * Only FIRST sets, for which the FOLLOW is not included, are stored.
  99.  */
  100. set
  101. #ifdef __STDC__
  102. rJunc( Junction *p, int k, set *rk )
  103. #else
  104. rJunc( p, k, rk )
  105. Junction *p;
  106. int k;
  107. set *rk;
  108. #endif
  109. {
  110.     set a, b;
  111.     require(p!=NULL,                "rJunc: NULL node");
  112.     require(p->ntype==nJunction,    "rJunc: not junction");
  113.  
  114. #ifdef DBG_LL1
  115.     if ( p->jtype == RuleBlk ) fprintf(stderr, "FIRST(%s,%d) \n",((Junction *)p)->rname,k);
  116.     else fprintf(stderr, "rJunc: %s in rule %s\n",
  117.             decodeJType[p->jtype], ((Junction *)p)->rname);
  118. #endif
  119.     /* locks are valid for aLoopBlk,aPlusBlk,RuleBlk,EndRule junctions only */
  120.     if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  121.          p->jtype==aPlusBlk || p->jtype==EndRule ) 
  122.     {
  123.         require(p->lock!=NULL, "rJunc: lock array is NULL");
  124.         if ( p->lock[k] )
  125.         {
  126.             if ( p->jtype == EndRule )    /* FOLLOW cycle? */
  127.             {
  128.                 /*fprintf(stderr, "FOLLOW cycle to %s: panic!\n", p->rname);*/
  129.                 RegisterCycle(p->rname, k);
  130.             }
  131.             return empty;
  132.         }
  133.         if ( p->jtype == RuleBlk && p->end->halt )    /* check for FIRST cache */
  134.         {
  135.             CacheEntry *q = (CacheEntry *) hash_get(Fcache, Fkey(p->rname,'i',k));
  136.             if ( q != NULL )
  137.             {
  138.                 set_orin(rk, q->rk);
  139.                 return set_dup( q->fset );
  140.             }
  141.         }
  142.         if ( p->jtype == EndRule )        /* FOLLOW set cached already? */
  143.         {
  144.             CacheEntry *q = (CacheEntry *) hash_get(Fcache, Fkey(p->rname,'o',k));
  145.             if ( q != NULL )
  146.             {
  147.                 /*fprintf(stderr, "<->FOLLOW(%s,%d):", p->rname,k);
  148.                 s_fprT(stderr, q->fset);
  149.                 if ( q->incomplete ) fprintf(stderr, " (incomplete)");
  150.                 fprintf(stderr, "\n");
  151.                 */
  152.                 if ( !q->incomplete )
  153.                 {
  154.                     return set_dup( q->fset );
  155.                 }
  156.             }
  157.         }
  158.         p->lock[k] = TRUE;    /* This rule is busy */
  159.     }
  160.  
  161.     a = b = empty;
  162.  
  163.     if ( p->jtype == EndRule )
  164.     {
  165.         if ( p->halt )                                /* don't want FOLLOW here? */
  166.         {
  167.             p->lock[k] = FALSE;
  168.             set_orel(k, rk);                        /* indicate this k value needed */
  169.             return empty;
  170.         }
  171.         FoPush(p->rname, k);                        /* Attempting FOLLOW */
  172.         if ( p->p1 == NULL ) set_orel(EofToken, &a);/* if no FOLLOW assume EOF */
  173.         /*fprintf(stderr, "-->FOLLOW(%s,%d)\n", p->rname,k);*/
  174.     }
  175.  
  176.     if ( p->p1 != NULL ) REACH(p->p1, k, rk, a);
  177.     
  178.     /* C a c h e  R e s u l t s */
  179.     if ( p->jtype == RuleBlk && p->end->halt )        /* can save FIRST set? */
  180.     {
  181.         CacheEntry *q = newCacheEntry( Fkey(p->rname,'i',k) );
  182.         /*fprintf(stderr, "Caching %s FIRST %d\n", p->rname, k);*/
  183.         hash_add(Fcache, Fkey(p->rname,'i',k), (Entry *)q);
  184.         q->fset = set_dup( a );
  185.         q->rk = set_dup( *rk );
  186.     }
  187.  
  188.     if ( p->jtype == EndRule )                        /* just completed FOLLOW? */
  189.     {
  190.         /* Cache Follow set */
  191.         CacheEntry *q = (CacheEntry *) hash_get(Fcache, Fkey(p->rname,'o',k));
  192.         if ( q==NULL )
  193.         {
  194.             q = newCacheEntry( Fkey(p->rname,'o',k) );
  195.             hash_add(Fcache, Fkey(p->rname,'o',k), (Entry *)q);
  196.         }
  197.         /*fprintf(stderr, "Caching %s FOLLOW %d\n", p->rname, k);*/
  198.         set_orin(&(q->fset), a);
  199.         FoPop( k );
  200.         if ( FoTOS[k] == NULL && Cycles[k] != NULL ) ResolveFoCycles(k);
  201.         /*
  202.         fprintf(stderr, "<--FOLLOW(%s,%d):", p->rname, k);
  203.         s_fprT(stderr, q->fset);
  204.         if ( q->incomplete ) fprintf(stderr, " (incomplete)");
  205.         fprintf(stderr, "\n");
  206.         */
  207.     }
  208.     
  209.     if ( p->jtype != RuleBlk && p->p2 != NULL ) REACH(p->p2, k, rk, b);
  210.     if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  211.          p->jtype==aPlusBlk || p->jtype==EndRule ) 
  212.         p->lock[k] = FALSE;                            /* unlock node */
  213.  
  214.     set_orin(&a, b);
  215.     set_free(b);
  216.     return a;
  217. }
  218.  
  219. set
  220. #ifdef __STDC__
  221. rRuleRef( RuleRefNode *p, int k, set *rk_out )
  222. #else
  223. rRuleRef( p, k, rk_out )
  224. RuleRefNode *p;
  225. int k;
  226. set *rk_out;
  227. #endif
  228. {
  229.     set rk;
  230.     Junction *r;
  231.     int k2;
  232.     set a, rk2, b;
  233.     int save_halt;
  234.     RuleEntry *q = (RuleEntry *) hash_get(Rname, p->text);
  235.     require(p!=NULL,            "rRuleRef: NULL node");
  236.     require(p->ntype==nRuleRef,    "rRuleRef: not rule ref");
  237.  
  238. #ifdef DBG_LL1
  239.     fprintf(stderr, "rRuleRef: %s\n", p->text);
  240. #endif
  241.     if ( q == NULL )
  242.     {
  243.         warnNoFL( eMsg1("rule %s not defined",p->text) );
  244.         REACH(p->next, k, rk_out, a);
  245.         return a;
  246.     }
  247.     rk2 = empty;
  248.     r = RulePtr[q->rulenum];
  249.     if ( r->lock[k] )
  250.     {
  251.         errNoFL( eMsg2("infinite left-recursion to rule %s from rule %s",
  252.                         r->rname, p->rname) );
  253.         return empty;
  254.     }
  255.     save_halt = r->end->halt;
  256.     r->end->halt = TRUE;        /* don't let reach fall off end of rule here */
  257.     rk = empty;
  258.     REACH(r, k, &rk, a);
  259.     r->end->halt = save_halt;
  260.     while ( !set_nil(rk) ) {
  261.         k2 = set_int(rk);
  262.         set_rm(k2, rk);
  263.         REACH(p->next, k2, &rk2, b);
  264.         set_orin(&a, b);
  265.         set_free(b);
  266.     }
  267.     set_free(rk);                /* this has no members, but free it's memory */
  268.     set_orin(rk_out, rk2);        /* remember what we couldn't do */
  269.     set_free(rk2);
  270.     return a;
  271. }
  272.  
  273. set
  274. #ifdef __STDC__
  275. rToken( TokNode *p, int k, set *rk )
  276. #else
  277. rToken( p, k, rk )
  278. TokNode *p;
  279. int k;
  280. set *rk;
  281. #endif
  282. {
  283.     set a;
  284.     require(p!=NULL,            "rToken: NULL node");
  285.     require(p->ntype==nToken,    "rToken: not token node");
  286.  
  287. #ifdef DBG_LL1
  288.     fprintf(stderr, "rToken: %s\n", (TokenStr[p->token]!=NULL)?TokenStr[p->token]:
  289.                                     ExprStr[p->token]);
  290. #endif
  291.     if ( k-1 == 0 ) return set_of( p->token );
  292.     REACH(p->next, k-1, rk, a);
  293.     
  294.     return a;
  295. }
  296.  
  297. set
  298. #ifdef __STDC__
  299. rAction( ActionNode *p, int k, set *rk )
  300. #else
  301. rAction( p, k, rk )
  302. ActionNode *p;
  303. int k;
  304. set *rk;
  305. #endif
  306. {
  307.     set a;
  308.     require(p!=NULL,            "rJunc: NULL node");
  309.     require(p->ntype==nAction,    "rJunc: not action");
  310.     
  311.     REACH(p->next, k, rk, a);    /* ignore actions */
  312.     return a;
  313. }
  314.  
  315.                 /* A m b i g u i t y  R e s o l u t i o n */
  316.  
  317.  
  318. void
  319. #ifdef __STDC__
  320. dumpAmbigMsg( set *fset, FILE *f, int want_nls )
  321. #else
  322. dumpAmbigMsg( fset, f, want_nls )
  323. set *fset;
  324. FILE *f;
  325. int want_nls;
  326. #endif
  327. {
  328.     int i;
  329.  
  330.     if ( want_nls ) fprintf(f, "\n\t");
  331.     else fprintf(f, " ");
  332.     for (i=1; i<=CLL_k; i++)
  333.     {
  334.         if ( i>1 )
  335.         {
  336.             if ( !want_nls ) fprintf(f, ", ");
  337.         }
  338.         if ( set_deg(fset[i]) > 3 && elevel == 1 )
  339.         {
  340.             int e,m;
  341.             fprintf(f, "{");
  342.             for (m=1; m<=3; m++)
  343.             {
  344.                 e=set_int(fset[i]);
  345.                 fprintf(f, " %s", TerminalString(e));
  346.                 set_rm(e, fset[i]);
  347.             }
  348.             fprintf(f, " ... }");
  349.         }
  350.         else s_fprT(f, fset[i]);
  351.         if ( want_nls ) fprintf(f, "\n\t");
  352.     }
  353.     fprintf(f, "\n");
  354. }
  355.  
  356. /*
  357.  * If delta is the set of ambiguous lookahead sequences, then make sure that
  358.  * the predicate(s) for productions alt1,alt2 cover the sequences in delta.
  359.  *
  360.  * For example,
  361.  *    a : <<PRED1>>? (A B|A C)
  362.  *      | b
  363.  *    ;
  364.  *    b : <<PRED2>>? A B
  365.  *      | A C
  366.  *      ;
  367.  *
  368.  * This should give a warning that (A C) predicts both productions and alt2
  369.  * does not have a predicate in the production that generates (A C).
  370.  *
  371.  * The warning detection is simple.  Let delta = LOOK(alt1) intersection LOOK(alt2).
  372.  * Now, if ( delta set-difference context(predicates-for-alt1) != empty then
  373.  * alt1 does not "cover" all ambiguous sequences.
  374.  *
  375.  * If ambig is nonempty, then ambig in LL(k) sense -> use tree info; else use fset
  376.  * info.  Actually, sets are used only if k=1 for this grammar.
  377.  */
  378. static void
  379. #ifdef __STDC__
  380. ensure_predicates_cover_ambiguous_lookahead_sequences( Junction *alt1, Junction *alt2, char *sub, Tree *ambig )
  381. #else
  382. ensure_predicates_cover_ambiguous_lookahead_sequences( alt1, alt2, sub, ambig )
  383. Junction *alt1;
  384. Junction *alt2;
  385. char *sub;
  386. Tree *ambig;
  387. #endif
  388. {
  389.     if ( !ParseWithPredicates ) return;
  390.  
  391.     if ( ambig!=NULL )
  392.     {
  393.         Tree *non_covered = NULL;
  394.         if ( alt1->predicate!=NULL )
  395.             non_covered = tdif(ambig, alt1->predicate, alt1->fset, alt2->fset);
  396.         if ( non_covered!=NULL || alt1->predicate==NULL && WarningLevel>1 )
  397.         {
  398.             fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  399.             fprintf(stderr, " warning: alt %d %shas no predicate to resolve ambiguity",
  400.                             alt1->altnum, sub);
  401.             if ( alt2->predicate!=NULL && non_covered!=NULL )
  402.             {
  403.                 fprintf(stderr, " upon");
  404.                 preorder(non_covered);
  405.             }
  406.             fprintf(stderr, "\n");
  407.         }
  408.         Tfree(non_covered);
  409.         if ( alt2->predicate!=NULL )
  410.             non_covered = tdif(ambig, alt2->predicate, alt1->fset, alt2->fset);
  411.         if ( non_covered!=NULL || alt2->predicate==NULL && WarningLevel>1 )
  412.         {
  413.             fprintf(stderr, ErrHdr, FileStr[alt2->file], alt2->line);
  414.             fprintf(stderr, " warning: alt %d %shas no predicate to resolve ambiguity",
  415.                             alt2->altnum, sub);
  416.             if ( alt2->predicate!=NULL && non_covered!=NULL )
  417.             {
  418.                 fprintf(stderr, " upon");
  419.                 preorder(non_covered);
  420.             }
  421.             fprintf(stderr, "\n");
  422.         }
  423.         Tfree(non_covered);
  424.     }
  425.     else if ( !set_nil(alt1->fset[1]) )
  426.     {
  427.         set delta, non_covered;
  428.         delta = set_and(alt1->fset[1], alt2->fset[1]);
  429.         non_covered = set_dif(delta, covered_set(alt1->predicate));
  430.         if ( set_deg(non_covered)>0 && WarningLevel>1 )
  431.         {
  432.             fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  433.             fprintf(stderr, " warning: alt %d %shas no predicate to resolve ambiguity",
  434.                             alt1->altnum, sub);
  435.             if ( alt1->predicate!=NULL )
  436.             {
  437.                 fprintf(stderr, " upon ");
  438.                 s_fprT(stderr, non_covered);
  439.             }
  440.             fprintf(stderr, "\n");
  441.         }
  442.         set_free( non_covered );
  443.         non_covered = set_dif(delta, covered_set(alt2->predicate));
  444.         if ( set_deg(non_covered)>0 && WarningLevel>1 )
  445.         {
  446.             fprintf(stderr, ErrHdr, FileStr[alt2->file], alt2->line);
  447.             fprintf(stderr, " warning: alt %d %shas no predicate to resolve ambiguity",
  448.                             alt2->altnum, sub);
  449.             if ( alt2->predicate!=NULL )
  450.             {
  451.                 fprintf(stderr, " upon ");
  452.                 s_fprT(stderr, non_covered);
  453.             }
  454.             fprintf(stderr, "\n");
  455.         }
  456.         set_free( non_covered );
  457.         set_free( delta );
  458.     }
  459.     else fatal("productions have no lookahead in predicate checking routine");
  460. }
  461.  
  462. void
  463. #ifdef __STDC__
  464. HandleAmbiguity( Junction *alt1, Junction *alt2, int jtype )
  465. #else
  466. HandleAmbiguity( alt1, alt2, jtype )
  467. Junction *alt1;
  468. Junction *alt2;
  469. int jtype;
  470. #endif
  471. {
  472.     unsigned **ftbl;
  473.     set *fset, b;
  474.     int i, numAmbig, n, n2;
  475.     Tree *ambig, *t, *u;
  476.     char *sub = "";
  477.  
  478.     /* These sets are used to constrain LL_k set, but are made CLL_k long anyway */
  479.     fset = (set *) calloc(CLL_k+1, sizeof(set));
  480.     require(fset!=NULL, "cannot allocate fset");
  481.     ftbl = (unsigned **) calloc(CLL_k+1, sizeof(unsigned *));
  482.     require(ftbl!=NULL, "cannot allocate ftbl");
  483.     /* create constraint table and count number of possible ambiguities (use<=LL_k) */
  484.     for (n=1,i=1; i<=CLL_k; i++)
  485.     {
  486.         b = set_and(alt1->fset[i], alt2->fset[i]);
  487.         n *= set_deg(b);
  488.         fset[i] = set_dup(b);
  489.         ftbl[i] = set_pdq(b);
  490.         set_free(b);
  491.     }
  492.  
  493.     switch ( jtype )
  494.     {
  495.         case aSubBlk: sub = "of (..) "; break;
  496.         case aOptBlk: sub = "of {..} "; break;
  497.         case aLoopBegin: sub = "of (..)* "; break;
  498.         case aLoopBlk: sub = "of (..)* "; break;
  499.         case aPlusBlk: sub = "of (..)+ "; break;
  500.         case RuleBlk: sub = "of the rule itself "; break;
  501.         default : sub = ""; break;
  502.     }
  503.  
  504.     /* if all sets have degree 1 for k<LL_k, then must be ambig upon >=1 permutation;
  505.      * don't bother doing full LL(k) analysis.
  506.      * (This "if" block handles the LL(1) case)
  507.      */
  508.     n2 = 0;
  509.     for (i=1; i<LL_k; i++) n2 += set_deg(alt1->fset[i])+set_deg(alt2->fset[i]);
  510.     if ( n2==2*(LL_k-1) )
  511.     {
  512.         ambig = NULL;
  513.         if ( LL_k>1 ) ambig = make_tree_from_sets(alt1->fset, alt2->fset);
  514.         if ( ParseWithPredicates )
  515.         {
  516.             /* NOTE: find_predicates() doesn't know that full LL(k) analysis is
  517.              *       not necessary.
  518.              */
  519.             alt1->predicate = find_predicates((Node *)alt1->p1);
  520.             alt2->predicate = find_predicates((Node *)alt2->p1);
  521.             if (HoistPredicateContext&&(alt1->predicate!=NULL||alt2->predicate!=NULL))
  522.                 ensure_predicates_cover_ambiguous_lookahead_sequences(alt1, alt2, sub, ambig);
  523.             if ( WarningLevel == 1 &&
  524.                 (alt1->predicate!=NULL||alt2->predicate!=NULL))
  525.             {
  526.                 for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  527.                 free(fset);
  528.                 for (i=1; i<=CLL_k; i++) free( ftbl[i] );
  529.                 free(ftbl);
  530.                 Tfree(ambig);
  531.                 return;
  532.             }
  533.         }
  534.  
  535.         fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  536.         if ( jtype == aLoopBegin )
  537.             fprintf(stderr, " warning: optional path and alt(s) of (..)* ambiguous upon");
  538.         else
  539.             fprintf(stderr, " warning: alts %d and %d %sambiguous upon",
  540.                         alt1->altnum, alt2->altnum, sub);
  541.         if ( elevel == 3 && LL_k>1 )
  542.         {
  543.             preorder(ambig);
  544.             fprintf(stderr, "\n");
  545.             Tfree(ambig);
  546.             return;
  547.         }
  548.         Tfree(ambig);
  549.         dumpAmbigMsg(fset, stderr, 0);
  550.  
  551.         for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  552.         free(fset);
  553.         for (i=1; i<=CLL_k; i++) free( ftbl[i] );
  554.         free(ftbl);
  555.         return;
  556.     }
  557.  
  558.     /* in case tree construction runs out of memory, set info to make good err msg */
  559.     CurAmbigAlt1 = alt1->altnum;
  560.     CurAmbigAlt2 = alt2->altnum;
  561.     CurAmbigbtype = sub;
  562.     CurAmbigfile = alt1->file;
  563.     CurAmbigline = alt1->line;
  564.     
  565.     /* Don't do full LL(n) analysis if (...)? block because the block,
  566.        by definition, defies LL(n) analysis.
  567.        If guess (...)? block and ambiguous then don't remove anything from
  568.        2nd alt to resolve ambig.
  569.        Want to predict with LL sup 1 ( n ) decision not LL(n) if guess block
  570.        since it is much cheaper than LL(n).  LL sup 1 ( n ) "covers" the LL(n)
  571.        lookahead information.
  572.  
  573.        Note: LL(n) context cannot be computed for semantic predicates when
  574.        followed by (..)?.
  575.  
  576.        If (..)? then we scream "AAAHHHH!  No LL(n) analysis will help"
  577.        */
  578.     if ( first_item_is_guess_block((Junction *)alt1->p1)!=NULL )
  579.     {
  580.         if ( ParseWithPredicates )
  581.         {
  582.             alt1->predicate = find_predicates((Node *)alt1->p1);
  583.             alt2->predicate = find_predicates((Node *)alt2->p1);
  584.             if ( HoistPredicateContext && (alt1->predicate!=NULL||alt2->predicate!=NULL) )
  585.                 ensure_predicates_cover_ambiguous_lookahead_sequences(alt1, alt2, sub, ambig);
  586.             if ( WarningLevel==1 &&
  587.                 (alt1->predicate!=NULL||alt2->predicate!=NULL))
  588.             {
  589.                 for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  590.                 free(fset);
  591.                 for (i=1; i<=CLL_k; i++) free( ftbl[i] );
  592.                 free(ftbl);
  593.                 return;
  594.             }
  595.         }
  596.  
  597.         if ( WarningLevel>1 )
  598.         {
  599.             fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  600.             if ( jtype == aLoopBegin )
  601.             fprintf(stderr, " warning: optional path and alt(s) of (..)* ambiguous upon");
  602.             else
  603.             fprintf(stderr, " warning: alts %d and %d %sambiguous upon",
  604.                     alt1->altnum, alt2->altnum, sub);
  605.             dumpAmbigMsg(fset, stderr, 0);
  606.         }
  607.  
  608.         for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  609.         free(fset);
  610.         for (i=1; i<=CLL_k; i++) free( ftbl[i] );
  611.         free(ftbl);
  612.         return;
  613.     }
  614.     
  615.     /* Not resolved with (..)? block.  Do full LL(n) analysis */
  616.     
  617.     /* ambig is the set of k-tuples truly in common between alt 1 and alt 2 */
  618.     ambig = VerifyAmbig(alt1, alt2, ftbl, fset, &t, &u, &numAmbig);
  619.     for (i=1; i<=CLL_k; i++) free( ftbl[i] );
  620.     free(ftbl);
  621.  
  622.     /* are all things in intersection really ambigs? */
  623.     if ( numAmbig < n )
  624.     {
  625.         Tree *v;
  626.  
  627.         /* remove ambig permutation from 2nd alternative to resolve ambig;
  628.          * We want to compute the set of artificial tuples, arising from
  629.          * LL sup 1 (n) compression, that collide with real tuples from the
  630.          * 2nd alternative.  This is the set of "special case" tuples that
  631.          * the LL sup 1 (n) decision template maps incorrectly.
  632.          */
  633.         if ( ambig!=NULL )
  634.         {
  635.             for (v=ambig->down; v!=NULL; v=v->right)
  636.             {
  637.                 u = trm_perm(u, v);
  638.             }
  639.             /*fprintf(stderr, "after rm alt2:"); preorder(u); fprintf(stderr, "\n");*/
  640.         }
  641.         Tfree( t );
  642.         alt1->ftree = tappend(alt1->ftree, u);
  643.         alt1->ftree = tleft_factor(alt1->ftree);
  644.     }
  645.  
  646.     if ( ambig==NULL )
  647.     {
  648.         for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  649.         free(fset);
  650.         return;
  651.     }
  652.  
  653.     ambig = tleft_factor(ambig);
  654.  
  655.     fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  656.     if ( jtype == aLoopBegin )
  657.         fprintf(stderr, " warning: optional path and alt(s) of (..)* ambiguous upon");
  658.     else
  659.         fprintf(stderr, " warning: alts %d and %d %sambiguous upon",
  660.                     alt1->altnum, alt2->altnum, sub);
  661.     if ( elevel == 3 )
  662.     {
  663.         preorder(ambig->down);
  664.         fprintf(stderr, "\n");
  665.         Tfree(ambig);
  666.         return;
  667.     }
  668.     Tfree(ambig);
  669.     dumpAmbigMsg(fset, stderr, 0);
  670.     for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  671.     free(fset);
  672. }
  673.  
  674. /* Don't analyze alpha block of (alpha)?beta; if (alpha)? then analyze
  675.  * Return the 1st node of the beta block if present else return j.
  676.  */
  677. Junction *
  678. #ifdef __STDC__
  679. analysis_point( Junction *j )
  680. #else
  681. analysis_point( j )
  682. Junction *j;
  683. #endif
  684. {
  685.     Junction *gblock;
  686.  
  687.     if ( j->ntype!=nJunction ) return j;
  688.     gblock = first_item_is_guess_block((Junction *)j);
  689.  
  690.     if ( gblock!=NULL )
  691.     {
  692.         Junction *past = gblock->end;
  693.         Junction *p;
  694.         require(past!=NULL, "analysis_point: no end block on (...)? block");
  695.  
  696.         for (p=(Junction *)past->p1; p!=NULL; )
  697.         {
  698.             if ( p->ntype==nAction )
  699.             {
  700.                 p=(Junction *)((ActionNode *)p)->next;
  701.                 continue;
  702.             }
  703.             if ( p->ntype!=nJunction )
  704.             {
  705.                 return (Junction *)past->p1;
  706.             }
  707.             if ( p->jtype==EndBlk || p->jtype==EndRule )
  708.             {
  709.                 return j;
  710.             }
  711.             p=(Junction *)p->p1;
  712.         }
  713.     }
  714.     return j;
  715. }
  716.  
  717. set
  718. #ifdef __STDC__
  719. First( Junction *j, int k, int jtype, int *max_k )
  720. #else
  721. First( j, k, jtype, max_k )
  722. Junction *j;
  723. int k;
  724. int jtype;
  725. int *max_k;
  726. #endif
  727. {
  728.     Junction *alt1, *alt2;
  729.     set a, rk, fCurBlk;
  730.     int savek;
  731.     int p1, p2;
  732.     require(j->ntype==nJunction, "First: non junction passed");
  733.  
  734.     /* C o m p u t e  F I R S T  s e t  w i t h  k  l o o k a h e a d */
  735.     fCurBlk = rk = empty;
  736.     for (alt1=j; alt1!=NULL; alt1 = (Junction *)alt1->p2)
  737.     {
  738.         Junction *p = analysis_point((Junction *)alt1->p1);
  739.         REACH(p, k, &rk, alt1->fset[k]);
  740.         require(set_nil(rk), "rk != nil");
  741.         set_free(rk);
  742.         set_orin(&fCurBlk, alt1->fset[k]);
  743.     }
  744.  
  745.     /* D e t e c t  A m b i g u i t i e s */
  746.     *max_k = 1;
  747.     for (p1=1,alt1=j; alt1!=NULL; alt1 = (Junction *)alt1->p2, p1++)
  748.     {
  749.         for (p2=1,alt2=(Junction *)alt1->p2; alt2!=NULL; alt2 = (Junction *)alt2->p2, p2++)
  750.         {
  751.             savek = k;
  752.             a = set_and(alt1->fset[k], alt2->fset[k]);
  753.             while ( !set_nil(a) )
  754.             {
  755.                 if ( k==CLL_k )
  756.                 {
  757.                     int save_LL_k = LL_k;
  758.                     int save_CLL_k = CLL_k;
  759.                     /* Get new LL_k from interactive feature if enabled */
  760.                     if ( AImode )
  761.                         AmbiguityDialog(j, jtype, alt1, alt2, &CLL_k, &LL_k);
  762.                     *max_k = CLL_k;
  763.                     HandleAmbiguity(alt1, alt2, jtype);
  764.                     break;
  765.                 }
  766.                 else
  767.                 {
  768.                     Junction *p = analysis_point((Junction *)alt1->p1);
  769.                     Junction *q = analysis_point((Junction *)alt2->p1);
  770.                     k++;    /* attempt ambig alts again with more lookahead */
  771.                     REACH(p, k, &rk, alt1->fset[k]);
  772.                     require(set_nil(rk), "rk != nil");
  773.                     REACH(q, k, &rk, alt2->fset[k]);
  774.                     require(set_nil(rk), "rk != nil");
  775.                     set_free(a);
  776.                     a = set_and(alt1->fset[k], alt2->fset[k]);
  777.                     if ( k > *max_k ) *max_k = k;
  778.                 }
  779.             }
  780.             set_free(a);
  781.             k = savek;
  782.         }
  783.     }
  784.  
  785.     return fCurBlk;
  786. }
  787.