Antlr3.Tool.Grammar.BuildNFA C# (CSharp) Method

BuildNFA() public method

public BuildNFA ( ) : void
return void
        public virtual void BuildNFA()
        {
            if ( nfa == null )
            {
                CreateRuleStartAndStopNFAStates();
            }
            if ( nfa.Complete )
            {
                // don't let it create more than once; has side-effects
                return;
            }
            //[email protected]("### build "+getGrammarTypeString()+" grammar "+name+" NFAs");
            if ( Rules.Count == 0 )
            {
                return;
            }

            Antlr.Runtime.Tree.ITreeNodeStream input = new Antlr.Runtime.Tree.CommonTreeNodeStream( grammarTree );
            TreeToNFAConverter nfaBuilder = new TreeToNFAConverter( this, nfa, factory, input );
            try
            {
                nfaBuilder.grammar_();
            }
            catch ( RecognitionException re )
            {
                ErrorManager.Error( ErrorManager.MSG_BAD_AST_STRUCTURE,
                                   name,
                                   re );
            }
            nfa.Complete = true;
        }

Usage Example

        //throws Exception
        protected void checkDecision( Grammar g,
                                     int decision,
                                     string expecting,
                                     int[] expectingUnreachableAlts )
        {
            Antlr3.AntlrTool tool = new Antlr3.AntlrTool();
            // mimic actions of org.antlr.Tool first time for grammar g
            if ( g.CodeGenerator == null )
            {
                CodeGenerator generator = new CodeGenerator( tool, g, "Java" );
                g.CodeGenerator = generator;
                g.BuildNFA();
                g.CreateLookaheadDFAs( false );
            }

            DFA dfa = g.GetLookaheadDFA( decision );
            Assert.IsNotNull(dfa, "unknown decision #" + decision);
            FASerializer serializer = new FASerializer( g );
            string result = serializer.Serialize( dfa.StartState );
            //System.out.print(result);
            var nonDetAlts = dfa.UnreachableAlts;
            //System.out.println("alts w/o predict state="+nonDetAlts);

            // first make sure nondeterministic alts are as expected
            if ( expectingUnreachableAlts == null )
            {
                if ( nonDetAlts != null && nonDetAlts.Count != 0 )
                {
                    Console.Error.WriteLine( "nondeterministic alts (should be empty): " + ( (IList)nonDetAlts ).ToElementString() );
                }
                Assert.AreEqual(0, nonDetAlts != null ? nonDetAlts.Count : 0, "unreachable alts mismatch");
            }
            else
            {
                for ( int i = 0; i < expectingUnreachableAlts.Length; i++ )
                {
                    Assert.IsTrue(nonDetAlts != null ? nonDetAlts.Contains(expectingUnreachableAlts[i]) : false, "unreachable alts mismatch");
                }
            }
            Assert.AreEqual( expecting, result );
        }
All Usage Examples Of Antlr3.Tool.Grammar::BuildNFA
Grammar