예제 #1
0
        protected virtual string GetStateLabel(State s)
        {
            if (s == null)
            {
                return("null");
            }
            string stateLabel = s.StateNumber.ToString();

            if (s is DFAState)
            {
                StringBuffer buf = new StringBuffer(250);
                buf.Append('s');
                buf.Append(s.StateNumber);
                if (AntlrTool.internalOption_ShowNFAConfigsInDFA)
                {
                    if (s is DFAState)
                    {
                        if (((DFAState)s).AbortedDueToRecursionOverflow)
                        {
                            buf.Append("\\n");
                            buf.Append("abortedDueToRecursionOverflow");
                        }
                    }
                    var alts = ((DFAState)s).AltSet;
                    if (alts != null)
                    {
                        buf.Append("\\n");
                        // separate alts
                        //List altList = new ArrayList();
                        //altList.addAll( alts );
                        //Collections.sort( altList );
                        List <int> altList = alts.OrderBy(i => i).ToList();
                        ICollection <NFAConfiguration> configurations = ((DFAState)s).NfaConfigurations;
                        for (int altIndex = 0; altIndex < altList.Count; altIndex++)
                        {
                            object altI = altList[altIndex];
                            int    alt  = (int)altI;
                            if (altIndex > 0)
                            {
                                buf.Append("\\n");
                            }
                            buf.Append("alt");
                            buf.Append(alt);
                            buf.Append(':');
                            // get a list of configs for just this alt
                            // it will help us print better later
                            IList <NFAConfiguration> configsInAlt = new List <NFAConfiguration>();
                            foreach (NFAConfiguration c in configurations)
                            {
                                if (c.Alt != alt)
                                {
                                    continue;
                                }
                                configsInAlt.Add(c);
                            }

                            int n = 0;
                            for (int cIndex = 0; cIndex < configsInAlt.Count; cIndex++)
                            {
                                NFAConfiguration c = configsInAlt[cIndex];
                                n++;
                                buf.Append(c.ToString(false));
                                if ((cIndex + 1) < configsInAlt.Count)
                                {
                                    buf.Append(", ");
                                }

                                if (n % 5 == 0 && (configsInAlt.Count - cIndex) > 3)
                                {
                                    buf.Append("\\n");
                                }
                            }
                        }
                    }
                }
                stateLabel = buf.ToString();
            }
            if ((s is NFAState) && ((NFAState)s).IsDecisionState)
            {
                stateLabel = stateLabel + ",d=" +
                             ((NFAState)s).DecisionNumber;
                if (((NFAState)s).endOfBlockStateNumber != State.INVALID_STATE_NUMBER)
                {
                    stateLabel += ",eob=" + ((NFAState)s).endOfBlockStateNumber;
                }
            }
            else if ((s is NFAState) &&
                     ((NFAState)s).endOfBlockStateNumber != State.INVALID_STATE_NUMBER)
            {
                NFAState n = ((NFAState)s);
                stateLabel = stateLabel + ",eob=" + n.endOfBlockStateNumber;
            }
            else if (s is DFAState && ((DFAState)s).IsAcceptState)
            {
                stateLabel = stateLabel +
                             "=>" + ((DFAState)s).GetUniquelyPredictedAlt();
            }
            return('"' + stateLabel + '"');
        }
예제 #2
0
        private string GetStateLabel(State state)
        {
            if (state == null)
            {
                return("null");
            }

            string   stateLabel = state.StateNumber.ToString();
            DFAState dfaState   = state as DFAState;
            NFAState nfaState   = state as NFAState;

            if (dfaState != null)
            {
                StringBuilder builder = new StringBuilder(250);
                builder.Append('s');
                builder.Append(state.StateNumber);
                if (AntlrTool.internalOption_ShowNFAConfigsInDFA)
                {
                    if (dfaState.AbortedDueToRecursionOverflow)
                    {
                        builder.AppendLine();
                        builder.AppendLine("AbortedDueToRecursionOverflow");
                    }

                    var alts = dfaState.AltSet;
                    if (alts != null)
                    {
                        builder.AppendLine();
                        List <int> altList = alts.OrderBy(i => i).ToList();
                        ICollection <NFAConfiguration> configurations = dfaState.NfaConfigurations;
                        for (int i = 0; i < altList.Count; i++)
                        {
                            int alt = altList[i];
                            if (i > 0)
                            {
                                builder.AppendLine();
                            }
                            builder.AppendFormat("alt{0}:", alt);
                            // get a list of configs for just this alt
                            // it will help us print better later
                            List <NFAConfiguration> configsInAlt = new List <NFAConfiguration>();
                            foreach (NFAConfiguration c in configurations)
                            {
                                if (c.Alt != alt)
                                {
                                    continue;
                                }

                                configsInAlt.Add(c);
                            }

                            int n = 0;
                            for (int cIndex = 0; cIndex < configsInAlt.Count; cIndex++)
                            {
                                NFAConfiguration c = configsInAlt[cIndex];
                                n++;
                                builder.Append(c.ToString(false));
                                if ((cIndex + 1) < configsInAlt.Count)
                                {
                                    builder.Append(", ");
                                }
                                if (n % 5 == 0 && (configsInAlt.Count - cIndex) > 3)
                                {
                                    builder.Append("\\n");
                                }
                            }
                        }
                    }
                }

                if (dfaState.IsAcceptState)
                {
                    builder.Append("⇒" + dfaState.GetUniquelyPredictedAlt());
                }

                stateLabel = builder.ToString();
            }
            else if (nfaState != null)
            {
                if (nfaState.IsDecisionState)
                {
                    stateLabel += ",d=" + nfaState.DecisionNumber;
                }

                if (nfaState.endOfBlockStateNumber != State.INVALID_STATE_NUMBER)
                {
                    stateLabel += ",eob=" + nfaState.endOfBlockStateNumber;
                }
            }

            return(stateLabel);
        }