Exemplo n.º 1
0
 // what is exact? it seems to only add sets from above on stack
 // if EOR is in set i.  When it sees a set w/o EOR, it stops adding.
 // Why would we ever want them all?  Maybe no viable alt instead of
 // mismatched token?
 protected virtual BitSet CombineFollows(bool exact)
 {
     int top = state._fsp;
     BitSet followSet = new BitSet();
     for ( int i = top; i >= 0; i-- )
     {
         BitSet localFollowSet = (BitSet)state.following[i];
         /*
         System.out.println("local follow depth "+i+"="+
                            localFollowSet.toString(getTokenNames())+")");
          */
         followSet.OrInPlace( localFollowSet );
         if ( exact )
         {
             // can we see end of rule?
             if ( localFollowSet.Member( TokenTypes.EndOfRule ) )
             {
                 // Only leave EOR in set if at top (start rule); this lets
                 // us know if have to include follow(start rule); i.e., EOF
                 if ( i > 0 )
                 {
                     followSet.Remove( TokenTypes.EndOfRule );
                 }
             }
             else
             { // can't see end of rule, quit
                 break;
             }
         }
     }
     return followSet;
 }
Exemplo n.º 2
0
        public virtual bool MismatchIsMissingToken( IIntStream input, BitSet follow )
        {
            if ( follow == null )
            {
                // we have no information about the follow; we can only consume
                // a single token and hope for the best
                return false;
            }
            // compute what can follow this grammar element reference
            if ( follow.Member( TokenTypes.EndOfRule ) )
            {
                BitSet viableTokensFollowingThisRule = ComputeContextSensitiveRuleFOLLOW();
                follow = follow.Or( viableTokensFollowingThisRule );
                if ( state._fsp >= 0 )
                { // remove EOR if we're not the start symbol
                    follow.Remove( TokenTypes.EndOfRule );
                }
            }
            // if current token is consistent with what could come after set
            // then we know we're missing a token; error recovery is free to
            // "insert" the missing token

            //System.out.println("viable tokens="+follow.toString(getTokenNames()));
            //System.out.println("LT(1)="+((TokenStream)input).LT(1));

            // BitSet cannot handle negative numbers like -1 (EOF) so I leave EOR
            // in follow set to indicate that the fall of the start symbol is
            // in the set (EOF can follow).
            if ( follow.Member( input.LA( 1 ) ) || follow.Member( TokenTypes.EndOfRule ) )
            {
                //System.out.println("LT(1)=="+((TokenStream)input).LT(1)+" is consistent with what follows; inserting...");
                return true;
            }
            return false;
        }