Esempio n. 1
0
        /** Given an input stream, return the unique alternative predicted by
         *  matching the input.  Upon error, return NFA.INVALID_ALT_NUMBER
         *  The first symbol of lookahead is presumed to be primed; that is,
         *  input.lookahead(1) must point at the input symbol you want to start
         *  predicting with.
         */
        public int Predict(DFA dfa)
        {
            DFAState   s             = dfa.StartState;
            int        c             = input.LA(1);
            Transition eotTransition = null;

dfaLoop:
            while (!s.IsAcceptState)
            {
                //Console.Out.WriteLine( "DFA.predict(" + s.stateNumber + ", " + dfa.nfa.Grammar.getTokenDisplayName( c ) + ")" );
                // for each edge of s, look for intersection with current char
                for (int i = 0; i < s.NumberOfTransitions; i++)
                {
                    Transition t = s.GetTransition(i);
                    // special case: EOT matches any char
                    if (t.Label.Matches(c))
                    {
                        // take transition i
                        s = (DFAState)t.Target;
                        input.Consume();
                        c = input.LA(1);
                        goto dfaLoop;
                    }
                    if (t.Label.Atom == Label.EOT)
                    {
                        eotTransition = t;
                    }
                }
                if (eotTransition != null)
                {
                    s = (DFAState)eotTransition.Target;
                    goto dfaLoop;
                }

                /*
                 * ErrorManager.error(ErrorManager.MSG_NO_VIABLE_DFA_ALT,
                 *                 s,
                 *                 dfa.nfa.Grammar.getTokenName(c));
                 */
                return(NFA.INVALID_ALT_NUMBER);
            }
            // woohoo!  We know which alt to predict
            // nothing emanates from a stop state; must terminate anyway
            //Console.Out.WriteLine( "DFA stop state " + s.stateNumber + " predicts " + s.getUniquelyPredictedAlt() );
            return(s.GetUniquelyPredictedAlt());
        }
 public virtual StringTemplate GenFixedLookaheadDecision( TemplateGroup templates,
                                                 DFA dfa )
 {
     return WalkFixedDFAGeneratingStateMachine( templates, dfa, dfa.StartState, 1 );
 }
Esempio n. 3
0
        public DFAState( DFA dfa )
        {
            if (dfa == null)
                throw new ArgumentNullException("dfa");

            this.dfa = dfa;
        }
Esempio n. 4
0
 /** Report that at least 2 alts have recursive constructs.  There is
  *  no way to build a DFA so we terminated.
  */
 public virtual void ReportNonLLStarDecision( DFA dfa )
 {
     /*
     [email protected]("non-LL(*) DFA "+dfa.decisionNumber+", alts: "+
                        dfa.recursiveAltSet.toList());
                        */
     _nonLLStarDecision = true;
     _altsWithProblem.addAll( dfa.recursiveAltSet.ToList() );
 }
Esempio n. 5
0
 public DFAState( DFA dfa )
 {
     this.dfa = dfa;
 }
Esempio n. 6
0
        /** Fill a list of all NFA states visited during the parse */
        protected virtual void ParseEngine(string startRule,
                                           NFAState start,
                                           NFAState stop,
                                           IIntStream input,
                                           Stack <object> ruleInvocationStack,
                                           IDebugEventListener actions,
                                           IList <NFAState> visitedStates)
        {
            NFAState s = start;

            if (actions != null)
            {
                actions.EnterRule(s.nfa.Grammar.FileName, start.enclosingRule.Name);
            }
            int t = input.LA(1);

            while (s != stop)
            {
                if (visitedStates != null)
                {
                    visitedStates.Add(s);
                }
                //Console.Out.WriteLine( "parse state " + s.stateNumber + " input=" + s.nfa.Grammar.getTokenDisplayName( t ) );
                // CASE 1: decision state
                if (s.DecisionNumber > 0 && s.nfa.Grammar.GetNumberOfAltsForDecisionNFA(s) > 1)
                {
                    // decision point, must predict and jump to alt
                    DFA dfa = s.nfa.Grammar.GetLookaheadDFA(s.DecisionNumber);
                    //if ( s.nfa.Grammar.type != GrammarType.Lexer )
                    //{
                    //    Console.Out.WriteLine( "decision: " +
                    //                   dfa.getNFADecisionStartState().Description +
                    //                   " input=" + s.nfa.Grammar.getTokenDisplayName( t ) );
                    //}
                    int m            = input.Mark();
                    int predictedAlt = Predict(dfa);
                    if (predictedAlt == NFA.INVALID_ALT_NUMBER)
                    {
                        string description        = dfa.NFADecisionStartState.Description;
                        NoViableAltException nvae =
                            new NoViableAltException(description,
                                                     dfa.NfaStartStateDecisionNumber,
                                                     s.StateNumber,
                                                     input);
                        if (actions != null)
                        {
                            actions.RecognitionException(nvae);
                        }
                        input.Consume(); // recover
                        throw nvae;
                    }
                    input.Rewind(m);
                    int parseAlt =
                        s.TranslateDisplayAltToWalkAlt(predictedAlt);
                    //if ( s.nfa.Grammar.type != GrammarType.Lexer )
                    //{
                    //    Console.Out.WriteLine( "predicted alt " + predictedAlt + ", parseAlt " + parseAlt );
                    //}
                    NFAState alt;
                    if (parseAlt > s.nfa.Grammar.GetNumberOfAltsForDecisionNFA(s))
                    {
                        // implied branch of loop etc...
                        alt = s.nfa.Grammar.nfa.GetState(s.endOfBlockStateNumber);
                    }
                    else
                    {
                        alt = s.nfa.Grammar.GetNFAStateForAltOfDecision(s, parseAlt);
                    }
                    s = (NFAState)alt.transition[0].Target;
                    continue;
                }

                // CASE 2: finished matching a rule
                if (s.IsAcceptState)
                { // end of rule node
                    if (actions != null)
                    {
                        actions.ExitRule(s.nfa.Grammar.FileName, s.enclosingRule.Name);
                    }
                    if (ruleInvocationStack.Count == 0)
                    {
                        // done parsing.  Hit the start state.
                        //Console.Out.WriteLine( "stack empty in stop state for " + s.enclosingRule );
                        break;
                    }
                    // pop invoking state off the stack to know where to return to
                    NFAState invokingState = (NFAState)ruleInvocationStack.Pop();
                    RuleClosureTransition invokingTransition =
                        (RuleClosureTransition)invokingState.transition[0];
                    // move to node after state that invoked this rule
                    s = invokingTransition.FollowState;
                    continue;
                }

                Transition trans = s.transition[0];
                Label      label = trans.Label;
                if (label.IsSemanticPredicate)
                {
                    FailedPredicateException fpe =
                        new FailedPredicateException(input,
                                                     s.enclosingRule.Name,
                                                     "can't deal with predicates yet");
                    if (actions != null)
                    {
                        actions.RecognitionException(fpe);
                    }
                }

                // CASE 3: epsilon transition
                if (label.IsEpsilon)
                {
                    // CASE 3a: rule invocation state
                    if (trans is RuleClosureTransition)
                    {
                        ruleInvocationStack.Push(s);
                        s = (NFAState)trans.Target;
                        //Console.Out.WriteLine( "call " + s.enclosingRule.name + " from " + s.nfa.Grammar.getFileName() );
                        if (actions != null)
                        {
                            actions.EnterRule(s.nfa.Grammar.FileName, s.enclosingRule.Name);
                        }
                        // could be jumping to new grammar, make sure DFA created
                        if (!s.nfa.Grammar.AllDecisionDFAHaveBeenCreated)
                        {
                            s.nfa.Grammar.CreateLookaheadDFAs();
                        }
                    }
                    // CASE 3b: plain old epsilon transition, just move
                    else
                    {
                        s = (NFAState)trans.Target;
                    }
                }

                // CASE 4: match label on transition
                else if (label.Matches(t))
                {
                    if (actions != null)
                    {
                        if (s.nfa.Grammar.type == GrammarType.Parser ||
                            s.nfa.Grammar.type == GrammarType.Combined)
                        {
                            actions.ConsumeToken(((ITokenStream)input).LT(1));
                        }
                    }
                    s = (NFAState)s.transition[0].Target;
                    input.Consume();
                    t = input.LA(1);
                }

                // CASE 5: error condition; label is inconsistent with input
                else
                {
                    if (label.IsAtom)
                    {
                        MismatchedTokenException mte =
                            new MismatchedTokenException(label.Atom, input);
                        if (actions != null)
                        {
                            actions.RecognitionException(mte);
                        }
                        input.Consume(); // recover
                        throw mte;
                    }
                    else if (label.IsSet)
                    {
                        MismatchedSetException mse =
                            new MismatchedSetException(((IntervalSet)label.Set).ToRuntimeBitSet(),
                                                       input);
                        if (actions != null)
                        {
                            actions.RecognitionException(mse);
                        }
                        input.Consume(); // recover
                        throw mse;
                    }
                    else if (label.IsSemanticPredicate)
                    {
                        FailedPredicateException fpe =
                            new FailedPredicateException(input,
                                                         s.enclosingRule.Name,
                                                         label.SemanticContext.ToString());
                        if (actions != null)
                        {
                            actions.RecognitionException(fpe);
                        }
                        input.Consume(); // recover
                        throw fpe;
                    }
                    else
                    {
                        throw new RecognitionException(input);   // unknown error
                    }
                }
            }
            //Console.Out.WriteLine( "hit stop state for " + stop.enclosingRule );
            if (actions != null)
            {
                actions.ExitRule(s.nfa.Grammar.FileName, stop.enclosingRule.Name);
            }
        }
Esempio n. 7
0
 public virtual void SynPredUsedInDFA( DFA dfa, SemanticContext semCtx )
 {
     decisionsWhoseDFAsUsesSynPreds.Add( dfa );
     semCtx.TrackUseOfSyntacticPredicates( this ); // walk ctx looking for preds
 }
Esempio n. 8
0
        protected virtual void Optimize( DFA dfa )
        {
            if ( dfa == null )
            {
                return; // nothing to do
            }
            /*
            [email protected]("Optimize DFA "+dfa.decisionNFAStartState.decisionNumber+
                               " num states="+dfa.getNumberOfStates());
            */
            //long start = JSystem.currentTimeMillis();
            if ( PRUNE_EBNF_EXIT_BRANCHES && dfa.CanInlineDecision )
            {
                _visited.Clear();
                int decisionType =
                    dfa.NFADecisionStartState.decisionStateType;
                if ( dfa.IsGreedy &&
                     ( decisionType == NFAState.OPTIONAL_BLOCK_START ||
                     decisionType == NFAState.LOOPBACK ) )
                {
                    OptimizeExitBranches( dfa.startState );
                }
            }
            // If the Tokens rule has syntactically ambiguous rules, try to prune
            if ( PRUNE_TOKENS_RULE_SUPERFLUOUS_EOT_EDGES &&
                 dfa.IsTokensRuleDecision &&
                 dfa.probe.stateToSyntacticallyAmbiguousTokensRuleAltsMap.Count > 0 )
            {
                _visited.Clear();
                OptimizeEOTBranches( dfa.startState );
            }

            /* ack...code gen needs this, cannot optimize
            visited.clear();
            unlinkUnneededStateData(dfa.startState);
            */
            //long stop = JSystem.currentTimeMillis();
            //[email protected]("minimized in "+(int)(stop-start)+" ms");
        }
Esempio n. 9
0
 public StringTemplate GenExpr( CodeGenerator generator,
                               StringTemplateGroup templates,
                               DFA dfa )
 {
     if ( templates != null )
     {
         return templates.GetInstanceOf( "false" );
     }
     return new StringTemplate( "false" );
 }
Esempio n. 10
0
 public override StringTemplate GenExpr( CodeGenerator generator,
                               StringTemplateGroup templates,
                               DFA dfa )
 {
     StringTemplate eST = null;
     if ( templates != null )
     {
         if ( _synpred )
         {
             eST = templates.GetInstanceOf( "evalSynPredicate" );
         }
         else
         {
             eST = templates.GetInstanceOf( "evalPredicate" );
             generator.grammar.decisionsWhoseDFAsUsesSemPreds.Add( dfa );
         }
         string predEnclosingRuleName = predicateAST.enclosingRuleName;
         /*
         String decisionEnclosingRuleName =
             dfa.getNFADecisionStartState().getEnclosingRule();
         // if these rulenames are diff, then pred was hoisted out of rule
         // Currently I don't warn you about this as it could be annoying.
         // I do the translation anyway.
         */
         //eST.setAttribute("pred", this.toString());
         if ( generator != null )
         {
             eST.SetAttribute( "pred",
                              generator.TranslateAction( predEnclosingRuleName, predicateAST ) );
         }
     }
     else
     {
         eST = new StringTemplate( "$pred$" );
         eST.SetAttribute( "pred", this.ToString() );
         return eST;
     }
     if ( generator != null )
     {
         string description =
             generator.target.GetTargetStringLiteralFromString( this.ToString() );
         eST.SetAttribute( "description", description );
     }
     return eST;
 }
Esempio n. 11
0
 public override StringTemplate GenExpr( CodeGenerator generator,
                               StringTemplateGroup templates,
                               DFA dfa )
 {
     StringTemplate eST = null;
     if ( templates != null )
     {
         eST = templates.GetInstanceOf( "orPredicates" );
     }
     else
     {
         eST = new StringTemplate( "($first(operands)$$rest(operands):{o | ||$o$}$)" );
     }
     foreach ( SemanticContext semctx in _operands )
     {
         eST.SetAttribute( "operands", semctx.GenExpr( generator, templates, dfa ) );
     }
     return eST;
 }
Esempio n. 12
0
 public override StringTemplate GenExpr( CodeGenerator generator,
                               StringTemplateGroup templates,
                               DFA dfa )
 {
     StringTemplate eST = null;
     if ( templates != null )
     {
         eST = templates.GetInstanceOf( "notPredicate" );
     }
     else
     {
         eST = new StringTemplate( "?!($pred$)" );
     }
     eST.SetAttribute( "pred", ctx.GenExpr( generator, templates, dfa ) );
     return eST;
 }
Esempio n. 13
0
 public override StringTemplate GenExpr( CodeGenerator generator,
                               StringTemplateGroup templates,
                               DFA dfa )
 {
     StringTemplate eST = null;
     if ( templates != null )
     {
         eST = templates.GetInstanceOf( "andPredicates" );
     }
     else
     {
         eST = new StringTemplate( "($left$&&$right$)" );
     }
     eST.SetAttribute( "left", _left.GenExpr( generator, templates, dfa ) );
     eST.SetAttribute( "right", _right.GenExpr( generator, templates, dfa ) );
     return eST;
 }
Esempio n. 14
0
 /** Generate an expression that will evaluate the semantic context,
  *  given a set of output templates.
  */
 public abstract StringTemplate GenExpr( CodeGenerator generator,
                                        StringTemplateGroup templates,
                                        DFA dfa );
        protected virtual StringTemplate WalkFixedDFAGeneratingStateMachine(
                TemplateGroup templates,
                DFA dfa,
                DFAState s,
                int k )
        {
            //System.Console.Out.WriteLine( "walk " + s.stateNumber + " in dfa for decision " + dfa.decisionNumber );
            if ( s.IsAcceptState )
            {
                StringTemplate dfaST2 = templates.GetInstanceOf( "dfaAcceptState" );
                dfaST2.SetAttribute( "alt", s.GetUniquelyPredictedAlt() );
                return dfaST2;
            }

            // the default templates for generating a state and its edges
            // can be an if-then-else structure or a switch
            string dfaStateName = "dfaState";
            string dfaLoopbackStateName = "dfaLoopbackState";
            string dfaOptionalBlockStateName = "dfaOptionalBlockState";
            string dfaEdgeName = "dfaEdge";
            if ( _parentGenerator.CanGenerateSwitch( s ) )
            {
                dfaStateName = "dfaStateSwitch";
                dfaLoopbackStateName = "dfaLoopbackStateSwitch";
                dfaOptionalBlockStateName = "dfaOptionalBlockStateSwitch";
                dfaEdgeName = "dfaEdgeSwitch";
            }

            StringTemplate dfaST = templates.GetInstanceOf( dfaStateName );
            if ( dfa.NFADecisionStartState.decisionStateType == NFAState.LOOPBACK )
            {
                dfaST = templates.GetInstanceOf( dfaLoopbackStateName );
            }
            else if ( dfa.NFADecisionStartState.decisionStateType == NFAState.OPTIONAL_BLOCK_START )
            {
                dfaST = templates.GetInstanceOf( dfaOptionalBlockStateName );
            }
            dfaST.SetAttribute( "k", k );
            dfaST.SetAttribute( "stateNumber", s.StateNumber );
            dfaST.SetAttribute( "semPredState",
                               s.IsResolvedWithPredicates );
            /*
            string description = dfa.getNFADecisionStartState().Description;
            description = parentGenerator.target.getTargetStringLiteralFromString( description );
            //System.Console.Out.WriteLine( "DFA: " + description + " associated with AST " + dfa.getNFADecisionStartState() );
            if ( description != null )
            {
                dfaST.SetAttribute( "description", description );
            }
            */
            int EOTPredicts = NFA.INVALID_ALT_NUMBER;
            DFAState EOTTarget = null;
            //System.Console.Out.WriteLine( "DFA state " + s.stateNumber );
            for ( int i = 0; i < s.NumberOfTransitions; i++ )
            {
                Transition edge = (Transition)s.GetTransition( i );
                //System.Console.Out.WriteLine( "edge " + s.stateNumber + "-" + edge.label.ToString() + "->" + edge.target.stateNumber );
                if ( edge.Label.Atom == Label.EOT )
                {
                    // don't generate a real edge for EOT; track alt EOT predicts
                    // generate that prediction in the else clause as default case
                    EOTTarget = (DFAState)edge.Target;
                    EOTPredicts = EOTTarget.GetUniquelyPredictedAlt();
                    /*
                    System.Console.Out.WriteLine("DFA s"+s.stateNumber+" EOT goes to s"+
                                       edge.target.stateNumber+" predicates alt "+
                                       EOTPredicts);
                    */
                    continue;
                }
                StringTemplate edgeST = templates.GetInstanceOf( dfaEdgeName );
                // If the template wants all the label values delineated, do that
                if ( edgeST.impl.TryGetFormalArgument( "labels" ) != null )
                {
                    List<string> labels = edge.Label.Set.Select( value => _parentGenerator.GetTokenTypeAsTargetLabel( value ) ).ToList();
                    edgeST.SetAttribute( "labels", labels );
                }
                else
                { // else create an expression to evaluate (the general case)
                    edgeST.SetAttribute( "labelExpr",
                                        _parentGenerator.GenLabelExpr( templates, edge, k ) );
                }

                // stick in any gated predicates for any edge if not already a pred
                if ( !edge.Label.IsSemanticPredicate )
                {
                    DFAState target = (DFAState)edge.Target;
                    SemanticContext preds =
                        target.GetGatedPredicatesInNFAConfigurations();
                    if ( preds != null )
                    {
                        //System.Console.Out.WriteLine( "preds=" + target.getGatedPredicatesInNFAConfigurations() );
                        StringTemplate predST = preds.GenExpr( _parentGenerator,
                                                              _parentGenerator.Templates,
                                                              dfa );
                        edgeST.SetAttribute( "predicates", predST );
                    }
                }

                StringTemplate targetST =
                    WalkFixedDFAGeneratingStateMachine( templates,
                                                       dfa,
                                                       (DFAState)edge.Target,
                                                       k + 1 );
                edgeST.SetAttribute( "targetState", targetST );
                dfaST.SetAttribute( "edges", edgeST );
                //System.Console.Out.WriteLine( "back to DFA " + dfa.decisionNumber + "." + s.stateNumber );
            }

            // HANDLE EOT EDGE
            if ( EOTPredicts != NFA.INVALID_ALT_NUMBER )
            {
                // EOT unique predicts an alt
                dfaST.SetAttribute( "eotPredictsAlt", EOTPredicts );
            }
            else if ( EOTTarget != null && EOTTarget.NumberOfTransitions > 0 )
            {
                // EOT state has transitions so must split on predicates.
                // Generate predicate else-if clauses and then generate
                // NoViableAlt exception as else clause.
                // Note: these predicates emanate from the EOT target state
                // rather than the current DFAState s so the error message
                // might be slightly misleading if you are looking at the
                // state number.  Predicates emanating from EOT targets are
                // hoisted up to the state that has the EOT edge.
                for ( int i = 0; i < EOTTarget.NumberOfTransitions; i++ )
                {
                    Transition predEdge = (Transition)EOTTarget.GetTransition( i );
                    StringTemplate edgeST = templates.GetInstanceOf( dfaEdgeName );
                    edgeST.SetAttribute( "labelExpr",
                                        _parentGenerator.GenSemanticPredicateExpr( templates, predEdge ) );
                    // the target must be an accept state
                    //System.Console.Out.WriteLine( "EOT edge" );
                    StringTemplate targetST =
                        WalkFixedDFAGeneratingStateMachine( templates,
                                                           dfa,
                                                           (DFAState)predEdge.Target,
                                                           k + 1 );
                    edgeST.SetAttribute( "targetState", targetST );
                    dfaST.SetAttribute( "edges", edgeST );
                }
            }
            return dfaST;
        }
Esempio n. 16
0
 // L O O K A H E A D  D E C I S I O N  G E N E R A T I O N
 /** Generate code that computes the predicted alt given a DFA.  The
  *  recognizerST can be either the main generated recognizerTemplate
  *  for storage in the main parser file or a separate file.  It's up to
  *  the code that ultimately invokes the codegen.g grammar rule.
  *
  *  Regardless, the output file and header file get a copy of the DFAs.
  */
 public virtual StringTemplate GenLookaheadDecision( StringTemplate recognizerST,
                                            DFA dfa )
 {
     StringTemplate decisionST;
     // If we are doing inline DFA and this one is acyclic and LL(*)
     // I have to check for is-non-LL(*) because if non-LL(*) the cyclic
     // check is not done by DFA.verify(); that is, verify() avoids
     // doesStateReachAcceptState() if non-LL(*)
     if ( dfa.CanInlineDecision )
     {
         decisionST =
             acyclicDFAGenerator.GenFixedLookaheadDecision( Templates, dfa );
     }
     else
     {
         // generate any kind of DFA here (cyclic or acyclic)
         dfa.CreateStateTables( this );
         outputFileST.SetAttribute( "cyclicDFAs", dfa );
         headerFileST.SetAttribute( "cyclicDFAs", dfa );
         decisionST = templates.GetInstanceOf( "dfaDecision" );
         string description = dfa.NFADecisionStartState.Description;
         description = target.GetTargetStringLiteralFromString( description );
         if ( description != null )
         {
             decisionST.SetAttribute( "description", description );
         }
         decisionST.SetAttribute( "decisionNumber",
                                 dfa.DecisionNumber );
     }
     return decisionST;
 }
 public AnalysisTimeoutException(DFA abortedDFA)
 {
     _abortedDFA = abortedDFA;
 }
Esempio n. 18
0
 /** Set the lookahead DFA for a particular decision.  This means
  *  that the appropriate AST node must updated to have the new lookahead
  *  DFA.  This method could be used to properly set the DFAs without
  *  using the createLookaheadDFAs() method.  You could do this
  *
  *    Grammar g = new Grammar("...");
  *    g.setLookahead(1, dfa1);
  *    g.setLookahead(2, dfa2);
  *    ...
  */
 public virtual void SetLookaheadDFA( int decision, DFA lookaheadDFA )
 {
     Decision d = CreateDecision( decision );
     d.dfa = lookaheadDFA;
     GrammarAST ast = d.startState.associatedASTNode;
     ast.LookaheadDFA = lookaheadDFA;
 }
Esempio n. 19
0
 /** Given an input stream, return the unique alternative predicted by
  *  matching the input.  Upon error, return NFA.INVALID_ALT_NUMBER
  *  The first symbol of lookahead is presumed to be primed; that is,
  *  input.lookahead(1) must point at the input symbol you want to start
  *  predicting with.
  */
 public int Predict( DFA dfa )
 {
     DFAState s = dfa.startState;
     int c = input.LA( 1 );
     Transition eotTransition = null;
     dfaLoop:
     while ( !s.IsAcceptState )
     {
         //Console.Out.WriteLine( "DFA.predict(" + s.stateNumber + ", " + dfa.nfa.grammar.getTokenDisplayName( c ) + ")" );
         // for each edge of s, look for intersection with current char
         for ( int i = 0; i < s.NumberOfTransitions; i++ )
         {
             Transition t = s.Transition( i );
             // special case: EOT matches any char
             if ( t.label.Matches( c ) )
             {
                 // take transition i
                 s = (DFAState)t.target;
                 input.Consume();
                 c = input.LA( 1 );
                 goto dfaLoop;
             }
             if ( t.label.Atom == Label.EOT )
             {
                 eotTransition = t;
             }
         }
         if ( eotTransition != null )
         {
             s = (DFAState)eotTransition.target;
             goto dfaLoop;
         }
         /*
         ErrorManager.error(ErrorManager.MSG_NO_VIABLE_DFA_ALT,
                            s,
                            dfa.nfa.grammar.getTokenName(c));
         */
         return NFA.INVALID_ALT_NUMBER;
     }
     // woohoo!  We know which alt to predict
     // nothing emanates from a stop state; must terminate anyway
     //Console.Out.WriteLine( "DFA stop state " + s.stateNumber + " predicts " + s.getUniquelyPredictedAlt() );
     return s.GetUniquelyPredictedAlt();
 }
Esempio n. 20
0
 private void UpdateLineColumnToLookaheadDFAMap( DFA lookaheadDFA )
 {
     GrammarAST decisionAST = nfa.Grammar.GetDecisionBlockAST( lookaheadDFA.DecisionNumber );
     int line = decisionAST.Line;
     int col = decisionAST.CharPositionInLine;
     lineColumnToLookaheadDFAMap[line + ":" + col] = lookaheadDFA;
 }
Esempio n. 21
0
        public DecisionProbe( DFA dfa )
        {
            if (dfa == null)
                throw new ArgumentNullException("dfa");

            this._dfa = dfa;
        }
Esempio n. 22
0
 /** Report that at least 2 alts have recursive constructs.  There is
  *  no way to build a DFA so we terminated.
  */
 public virtual void ReportNonLLStarDecision( DFA dfa )
 {
     /*
     [email protected]("non-LL(*) DFA "+dfa.decisionNumber+", alts: "+
                        dfa.recursiveAltSet.toList());
                        */
     _nonLLStarDecision = true;
     dfa.Nfa.Grammar.numNonLLStar++;
     _altsWithProblem.UnionWith( dfa.RecursiveAltSet.ToList() );
 }
Esempio n. 23
0
        public virtual DFA CreateLookaheadDFA( int decision, bool wackTempStructures )
        {
            Decision d = GetDecision( decision );
            string enclosingRule = d.startState.enclosingRule.Name;
            Rule r = d.startState.enclosingRule;

            //[email protected]("createLookaheadDFA(): "+enclosingRule+" dec "+decision+"; synprednames prev used "+synPredNamesUsedInDFA);
            NFAState decisionStartState = GetDecisionNFAStartState( decision );
            DateTime startDFA = DateTime.MinValue;
            DateTime stopDFA = DateTime.MinValue;
            if ( composite.watchNFAConversion )
            {
                Console.Out.WriteLine( "--------------------\nbuilding lookahead DFA (d="
                                   + decisionStartState.DecisionNumber + ") for " +
                                   decisionStartState.Description );
                startDFA = DateTime.Now;
            }

            DFA lookaheadDFA = new DFA( decision, decisionStartState );
            // Retry to create a simpler DFA if analysis failed (non-LL(*),
            // recursion overflow, or time out).
            bool failed =
                lookaheadDFA.AnalysisTimedOut ||
                lookaheadDFA.probe.IsNonLLStarDecision ||
                lookaheadDFA.probe.AnalysisOverflowed;
            if ( failed && lookaheadDFA.OkToRetryWithK1 )
            {
                // set k=1 option and try again.
                // First, clean up tracking stuff
                decisionsWhoseDFAsUsesSynPreds.Remove( lookaheadDFA );
                // TODO: clean up synPredNamesUsedInDFA also (harder)
                d.blockAST.SetBlockOption( this, "k", 1 );
                if ( composite.watchNFAConversion )
                {
                    Console.Out.Write( "trying decision " + decision +
                                     " again with k=1; reason: " +
                                     lookaheadDFA.ReasonForFailure );
                }
                lookaheadDFA = null; // make sure other memory is "free" before redoing
                lookaheadDFA = new DFA( decision, decisionStartState );
            }
            if ( lookaheadDFA.AnalysisTimedOut )
            { // did analysis bug out?
                ErrorManager.InternalError( "could not even do k=1 for decision " +
                                           decision + "; reason: " +
                                           lookaheadDFA.ReasonForFailure );
            }

            SetLookaheadDFA( decision, lookaheadDFA );

            if ( wackTempStructures )
            {
                foreach ( DFAState s in lookaheadDFA.UniqueStates.Values )
                {
                    s.Reset();
                }
            }

            // create map from line:col to decision DFA (for ANTLRWorks)
            UpdateLineColumnToLookaheadDFAMap( lookaheadDFA );

            if ( composite.watchNFAConversion )
            {
                stopDFA = DateTime.Now;
                Console.Out.WriteLine( "cost: " + lookaheadDFA.NumberOfStates +
                                   " states, " + (int)( stopDFA - startDFA ).TotalMilliseconds + " ms" );
            }
            //[email protected]("after create DFA; synPredNamesUsedInDFA="+synPredNamesUsedInDFA);
            return lookaheadDFA;
        }
Esempio n. 24
0
        public static DFA CreateFromNfa(int decisionNumber, NFAState decisionStartState)
        {
            DFA dfa = new DFA(decisionNumber, decisionStartState);

            //long start = JSystem.currentTimeMillis();
            NFAToDFAConverter nfaConverter = new NFAToDFAConverter( dfa );
            try
            {
                nfaConverter.Convert();

                // figure out if there are problems with decision
                dfa.Verify();

                if ( !dfa.Probe.IsDeterministic || dfa.Probe.AnalysisOverflowed )
                {
                    dfa.Probe.IssueWarnings();
                }

                // must be after verify as it computes cyclic, needed by this routine
                // should be after warnings because early termination or something
                // will not allow the reset to operate properly in some cases.
                dfa.ResetStateNumbersToBeContiguous();

                //long stop = JSystem.currentTimeMillis();
                //[email protected]("verify cost: "+(int)(stop-start)+" ms");
            }
            catch ( NonLLStarDecisionException /*nonLL*/ )
            {
                dfa.Probe.ReportNonLLStarDecision( dfa );
                // >1 alt recurses, k=* and no auto backtrack nor manual sem/syn
                if ( !dfa.OkToRetryWithK1 )
                {
                    dfa.Probe.IssueWarnings();
                }
            }

            return dfa;
        }
Esempio n. 25
0
 public DecisionProbe( DFA dfa )
 {
     this.dfa = dfa;
 }