AddTransition() public method

public AddTransition ( DFAState target, Antlr3.Analysis.Label label ) : int
target DFAState
label Antlr3.Analysis.Label
return int
Beispiel #1
0
        /** From list of lookahead sets (one per alt in decision), create
         *  an LL(1) DFA.  One edge per set.
         *
         *  s0-{alt1}->:o=>1
         *  | \
         *  |  -{alt2}->:o=>2
         *  |
         *  ...
         */
        public LL1DFA(int decisionNumber, NFAState decisionStartState, LookaheadSet[] altLook)
            : base(decisionNumber, decisionStartState)
        {
            DFAState s0 = NewState();

            StartState = s0;
            UnreachableAlts.Clear();
            for (int alt = 1; alt < altLook.Length; alt++)
            {
                DFAState acceptAltState = NewState();
                acceptAltState.IsAcceptState = true;
                SetAcceptState(alt, acceptAltState);
                acceptAltState.LookaheadDepth = 1;
                acceptAltState.CachedUniquelyPredicatedAlt = alt;
                Label e = GetLabelForSet(altLook[alt].TokenTypeSet);
                s0.AddTransition(acceptAltState, e);
            }
        }
Beispiel #2
0
        /** From list of lookahead sets (one per alt in decision), create
         *  an LL(1) DFA.  One edge per set.
         *
         *  s0-{alt1}->:o=>1
         *  | \
         *  |  -{alt2}->:o=>2
         *  |
         *  ...
         */
        public LL1DFA(int decisionNumber, NFAState decisionStartState, LookaheadSet[] altLook)
        {
            DFAState s0 = NewState();

            startState                 = s0;
            nfa                        = decisionStartState.nfa;
            NumberOfAlts               = nfa.grammar.GetNumberOfAltsForDecisionNFA(decisionStartState);
            this.decisionNumber        = decisionNumber;
            this.NFADecisionStartState = decisionStartState;
            InitAltRelatedInfo();
            UnreachableAlts = null;
            for (int alt = 1; alt < altLook.Length; alt++)
            {
                DFAState acceptAltState = NewState();
                acceptAltState.acceptState = true;
                SetAcceptState(alt, acceptAltState);
                acceptAltState.LookaheadDepth = 1;
                acceptAltState.cachedUniquelyPredicatedAlt = alt;
                Label e = GetLabelForSet(altLook[alt].tokenTypeSet);
                s0.AddTransition(acceptAltState, e);
            }
        }
Beispiel #3
0
        /** From a set of edgeset->list-of-alts mappings, create a DFA
         *  that uses syn preds for all |list-of-alts|>1.
         */
        public LL1DFA(int decisionNumber,
                      NFAState decisionStartState,
                      MultiMap <IntervalSet, int> edgeMap)
        {
            DFAState s0 = NewState();

            startState                 = s0;
            nfa                        = decisionStartState.nfa;
            NumberOfAlts               = nfa.grammar.GetNumberOfAltsForDecisionNFA(decisionStartState);
            this.decisionNumber        = decisionNumber;
            this.NFADecisionStartState = decisionStartState;
            InitAltRelatedInfo();
            UnreachableAlts = null;
            foreach (var edgeVar in edgeMap)
            {
                IntervalSet edge = edgeVar.Key;
                IList <int> alts = edgeVar.Value;
                alts = alts.OrderBy(i => i).ToList();
                //Collections.sort( alts ); // make sure alts are attempted in order
                //[email protected](edge+" -> "+alts);
                DFAState s = NewState();
                s.LookaheadDepth = 1;
                Label e = GetLabelForSet(edge);
                s0.AddTransition(s, e);
                if (alts.Count == 1)
                {
                    s.acceptState = true;
                    int alt = alts[0];
                    SetAcceptState(alt, s);
                    s.cachedUniquelyPredicatedAlt = alt;
                }
                else
                {
                    // resolve with syntactic predicates.  Add edges from
                    // state s that test predicates.
                    s.IsResolvedWithPredicates = true;
                    for (int i = 0; i < alts.Count; i++)
                    {
                        int alt = (int)alts[i];
                        s.cachedUniquelyPredicatedAlt = NFA.INVALID_ALT_NUMBER;
                        DFAState predDFATarget = GetAcceptState(alt);
                        if (predDFATarget == null)
                        {
                            predDFATarget             = NewState(); // create if not there.
                            predDFATarget.acceptState = true;
                            predDFATarget.cachedUniquelyPredicatedAlt = alt;
                            SetAcceptState(alt, predDFATarget);
                        }
                        // add a transition to pred target from d

                        /*
                         * int walkAlt =
                         *  decisionStartState.translateDisplayAltToWalkAlt(alt);
                         * NFAState altLeftEdge = nfa.grammar.getNFAStateForAltOfDecision(decisionStartState, walkAlt);
                         * NFAState altStartState = (NFAState)altLeftEdge.transition[0].target;
                         * SemanticContext ctx = nfa.grammar.ll1Analyzer.getPredicates(altStartState);
                         * [email protected]("sem ctx = "+ctx);
                         * if ( ctx == null ) {
                         *  ctx = new SemanticContext.TruePredicate();
                         * }
                         * s.addTransition(predDFATarget, new Label(ctx));
                         */
                        SemanticContext.Predicate synpred =
                            GetSynPredForAlt(decisionStartState, alt);
                        if (synpred == null)
                        {
                            synpred = new SemanticContext.TruePredicate();
                        }
                        s.AddTransition(predDFATarget, new PredicateLabel(synpred));
                    }
                }
            }
            //[email protected]("dfa for preds=\n"+this);
        }