Example #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected void addAvailableTransitionToTable(org.maltparser.parser.transition.TransitionTable ttable) throws org.maltparser.core.exception.MaltChainedException
        protected internal override void addAvailableTransitionToTable(TransitionTable ttable)
        {
            ttable.addTransition(SHIFT, "SH", false, null);
            ttable.addTransition(REDUCE, "RE", false, null);
            ttable.addTransition(RIGHTARC, "RA", true, null);
            ttable.addTransition(LEFTARC, "LA", true, null);
        }
Example #2
0
        public static string GetSourceCode(ExpressionTree tree)
        {
            DFA             automata    = new DFA(tree);
            TransitionTable transitions = new TransitionTable(automata.states);

            string sourceCode = Resources.Resource1.Program;

            //GetValues
            string TitleColor            = getColor();
            string Character_Token_First = getFistPositionsWithCharacters(tree.tokens);
            string Character_Token_Last  = getLastPositionsWithCharacters(tree.tokens);
            string Reservadas_Values     = getReservedValues(tree.actions);
            string TokensConReferencia   = getReferences(tree.actionReference);
            string Estados_Aceptacion    = getAcceptedStates(transitions);
            string States = getTransitions(transitions, tree.sets);

            //Replace Values
            sourceCode = sourceCode.Replace("</TitleColor>", TitleColor);
            sourceCode = sourceCode.Replace("</FirstPos>", Character_Token_First);
            sourceCode = sourceCode.Replace("</LastPos>", Character_Token_Last);
            sourceCode = sourceCode.Replace("</Reservadas>", Reservadas_Values);
            sourceCode = sourceCode.Replace("</Referencias>", TokensConReferencia);
            sourceCode = sourceCode.Replace("</States>", States);
            sourceCode = sourceCode.Replace("</Aceptacion>", Estados_Aceptacion);

            return(sourceCode);
        }
Example #3
0
        private void OnInitialize()
        {
            m_lexicon = new Lexicon();

            OnDefineLexer(m_lexicon, m_triviaTokens);

            m_scannerInfo = OnCreateScannerInfo();

            var production = OnDefineGrammar();

            if (production == null)
            {
                throw new InvalidOperationException("Root producton is not defined");
            }

            var rootProduction = production.SuffixedBy(Grammar.Eos());

            m_productionInfoManager = new ProductionInfoManager(rootProduction);

            m_transitionTable = OnCreateTransitionTable(OnCreateAutomaton(m_productionInfoManager), m_scannerInfo);

            OnDefineParserErrors(m_errorDefinition, m_errorManager);

            m_isInitialized = true;
        }
Example #4
0
        public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation)
        {
            while (true)
            {
                int currentStateIndex = m_topStack.StateIndex;
                for (int i = 0; i < transitions.ProductionCount; i++)
                {
                    int gotoState = transitions.GetGoto(currentStateIndex, i);

                    if (gotoState > 0)
                    {
                        var recoverNT = transitions.NonTerminals[i];

                        m_topStack = m_topStack.PrevNode;

                        var newNode = new StackNode(gotoState, m_topStack, null);
                        IncreaseErrorRecoverLevel();
                        AddError(new ErrorRecord(null, lastLocation));
                        m_topStack = newNode;

                        return(recoverNT);
                    }
                }

                if (m_topStack.PrevNode == null)
                {
                    throw new ParsingFailureException("There's no way to recover from parser error");
                }

                m_topStack = m_topStack.PrevNode;
            }
        }
        private void MenuItem7_Click(object sender, EventArgs e)
        {
            IEnumerator enumerator = null;

            Transition.Transition transition      = new Transition.Transition();
            TransitionTable       transitionTable = new TransitionTable();

            if (this.Select_Group_A != null)
            {
                if (this.Select_Group_B != null)
                {
                    if (ObjectType.ObjTst(LateBinding.LateGet(this.Select_Group_A.SelectedItem, null, "Name", new object[0], null, null), LateBinding.LateGet(this.Select_Group_B.SelectedItem, null, "Name", new object[0], null, null), false) != 0)
                    {
                        #region Label Formatting

                        //Edit The '↔' To Change How You Want Your Labels To Look
                        //Original 'To' Was The Transition Keyword For The Labels
                        //This Keyword Was Changed To Arrows To Show The Back And Forth Between Transitions
                        string str = string.Format("{0} ↔ {1}", RuntimeHelpers.GetObjectValue(LateBinding.LateGet(this.Select_Group_A.SelectedItem, null, "Name", new object[0], null, null)), RuntimeHelpers.GetObjectValue(LateBinding.LateGet(this.Select_Group_B.SelectedItem, null, "Name", new object[0], null, null)));

                        #endregion

                        string      str1        = string.Format("{0}Data\\Engine\\Templates\\2Way_Template.xml", AppDomain.CurrentDomain.BaseDirectory);
                        XmlDocument xmlDocument = new XmlDocument();
                        transitionTable.Clear();
                        try
                        {
                            xmlDocument.Load(str1);
                            try
                            {
                                enumerator = xmlDocument.SelectNodes("//Wizard/Tile").GetEnumerator();
                                while (enumerator.MoveNext())
                                {
                                    XmlElement            current     = (XmlElement)enumerator.Current;
                                    string                attribute   = current.GetAttribute("Pattern");
                                    string                attribute1  = current.GetAttribute("MapTile");
                                    string                attribute2  = current.GetAttribute("StaticTile");
                                    Transition.Transition transition1 = new Transition.Transition(str, attribute, (ClsTerrain)this.Select_Group_A.SelectedItem, (ClsTerrain)this.Select_Group_B.SelectedItem, this.Get_MapTiles(attribute1), this.Get_StaticTiles(attribute2));
                                    transitionTable.Add(transition1);
                                }
                            }
                            finally
                            {
                                if (enumerator is IDisposable)
                                {
                                    ((IDisposable)enumerator).Dispose();
                                }
                            }
                        }
                        catch (Exception exception)
                        {
                            ProjectData.SetProjectError(exception);
                            Interaction.MsgBox(exception.ToString(), MsgBoxStyle.OkOnly, null);
                            ProjectData.ClearProjectError();
                        }
                        transitionTable.Save(string.Format("{0}.xml", str));
                    }
                }
            }
        }
Example #6
0
        public ParserEngine(TransitionTable transitions, SyntaxErrors errorDef)
        {
            CodeContract.RequiresArgumentNotNull(transitions, "transitions");
            CodeContract.RequiresArgumentNotNull(errorDef, "errorDef");

            m_transitions = transitions;
            m_reducer     = new ReduceVisitor(transitions);

            m_heads               = new List <ParserHead>();
            m_shiftedHeads        = new List <ParserHead>();
            m_acceptedHeads       = new List <ParserHead>();
            m_errorCandidates     = new List <ParserHead>();
            m_tempHeads           = new List <ParserHead>();
            m_reducedHeads        = new List <ParserHead>();
            m_recoverReducedHeads = new List <ParserHead>();

            m_errorDef = errorDef;

            m_cleaner = new ParserHeadCleaner();
            EnableDeletionRecovery    = true;
            EnableInsertionRecovery   = true;
            EnableReplacementRecovery = true;

            //init state
            m_heads.Add(new ParserHead(new StackNode(0, null, null)));
        }
Example #7
0
        private void OnInitialize()
        {
            m_lexicon = new Lexicon();

            OnDefineLexer(m_lexicon, m_triviaTokens);

            m_scannerInfo = OnCreateScannerInfo();

            var production = OnDefineGrammar();

            if (production == null)
            {
                throw new InvalidOperationException("Root producton is not defined");
            }

            var rootProduction = production.SuffixedBy(Grammar.Eos());

            m_productionInfoManager = new Generator.ProductionInfoManager(rootProduction);

            m_transitionTable = OnCreateTransitionTable();

            OnDefineParserErrors(m_errorDefinition, m_errorManager);

            m_scanner = new Scanner(m_scannerInfo);
            m_scanner.SetTriviaTokens(m_triviaTokens.Select(t => t.Index).ToArray());
            m_scanner.ErrorManager   = m_errorManager;
            m_scanner.RecoverErrors  = true;
            m_scanner.LexicalErrorId = m_errorDefinition.LexicalErrorId;

            m_isInitialized = true;
        }
        public void FromFileTest()
        {
            var expected = new TransitionTable(
                head: 0,
                transitions: new Dictionary <char, ushort>[]
            {
                new Dictionary <char, ushort> {
                    ['a'] = 1
                },
                new Dictionary <char, ushort> {
                    ['b'] = 2
                },
                new Dictionary <char, ushort> {
                    ['c'] = 3
                },
                new Dictionary <char, ushort> {
                }
            },
                aliases: new Dictionary <ushort, string>
            {
                [3] = "ID"
            }
                );
            var actual = TransitionTable.FromVocabularyFile(Path.Combine(AppContext.BaseDirectory, "Content/vocabulary.txt"));

            actual.Should().Be(expected);
        }
Example #9
0
        public ParserEngine(TransitionTable transitions, SyntaxErrors errorDef)
        {
            CodeContract.RequiresArgumentNotNull(transitions, "transitions");
            CodeContract.RequiresArgumentNotNull(errorDef, "errorDef");

            m_transitions = transitions;
            m_reducer = new ReduceVisitor(transitions);

            m_heads = new List<ParserHead>();
            m_shiftedHeads = new List<ParserHead>();
            m_acceptedHeads = new List<ParserHead>();
            m_errorCandidates = new List<ParserHead>();
            m_tempHeads = new List<ParserHead>();
            m_reducedHeads = new List<ParserHead>();
            m_recoverReducedHeads = new List<ParserHead>();

            m_errorDef = errorDef;

            m_cleaner = new ParserHeadCleaner();
            EnableDeletionRecovery = true;
            EnableInsertionRecovery = true;
            EnableReplacementRecovery = true;

            //init state
            m_heads.Add(new ParserHead(new StackNode(0, null, null)));
        }
Example #10
0
        public TreeDetails(ExpressionTree tree, FollowTable follows, TransitionTable transitions)
        {
            InitializeComponent();
            this.tree        = tree;
            this.follows     = follows;
            this.transitions = transitions;

            expressionTextBox.Text = tree.expression;
        }
 public CreateTransitions()
 {
     this.Load            += new EventHandler(this.TEdit_Load);
     this.ImageTest        = false;
     this.iGroups          = new ClsTerrainTable();
     this.iSelected        = false;
     this.iTransition      = new Transition.Transition();
     this.iTransitionTable = new TransitionTable();
     InitializeComponent();
 }
Example #12
0
        /// <summary>
        /// Override the GetNextStates methdo in the base
        /// class to add the lambda feature
        /// It gets all the next states of the specified state
        /// and that's reachable through the symbol <see cref="S"/>
        /// or the lambda, lambda is represented as null.
        /// </summary>
        /// <param name="state">The state for which to get the next states</param>
        /// <param name="symbol">The connector symbol</param>
        /// <returns>List of all next states if any, empty list otherwise</returns>
        public override List <NfaState <Q> > GetNextStates(NfaState <Q> state, S symbol)
        {
            var list = new List <NfaState <Q> >();

            if (state != null && IsExists(state.Id))
            {
                IEnumerable <TransitionFunction <NfaState <Q>, S, NfaState <Q> > > transitions = (from t in TransitionTable
                                                                                                  where
                                                                                                  t.From.Id.Equals(state.Id) &&
                                                                                                  t.TransitionSymbols != null &&
                                                                                                  t.TransitionSymbols.Contains(
                                                                                                      symbol)
                                                                                                  select t).Union(
                    from t in TransitionTable.OfType <PredicateTransitionFunction <NfaState <Q>, S, NfaState <Q> > >()
                    where t.From.Id.Equals(state.Id) && t.PredicateLambda != null && t.PredicateLambda(symbol)
                    select(TransitionFunction <NfaState <Q>, S, NfaState <Q> >) t
                    ).Distinct();

                foreach (var tf in transitions)
                {
                    list.Add(tf.To);
                    // For each one check to see of there are some states
                    // reachable via lambda
                    List <NfaState <Q> > nextLambdaStates = GetNextLambdaStates(tf.To);
                    // add the states reachable via lambda
                    if (nextLambdaStates != null)
                    {
                        list.AddRange(nextLambdaStates);
                    }
                }

                // Search for lambda transtion from the state and other states
                List <NfaState <Q> > nextStateLambdaStates = GetNextLambdaStates(state);

                if (nextStateLambdaStates != null)
                {
                    // if found, iterate over them and check if
                    // they have a transition with the same symbol
                    foreach (var st in nextStateLambdaStates)
                    {
                        list.AddRange(from t in TransitionTable
                                      where t.From.Id.Equals(st.Id) &&
                                      t.TransitionSymbols != null &&
                                      t.TransitionSymbols.Contains(symbol)
                                      select t.To);
                    }
                }
            }
            else
            {
                return(null);
            }

            return(list);
        }
Example #13
0
        public void ScanTest_NotAcceptInputString()
        {
            string content    = "#";
            string vocabulary = "\\w ID";

            var table = TransitionTable.FromVocabularyString(vocabulary);

            Assert.Throws <NotAcceptLexemeException>(() => Scanner.Scan(content, new ScannerOptions {
                Transitions = table
            }));
        }
        public void AddTransition(S initial_state, S end_state, StateHandler call)
        {
            StateTransition <S> temp_transition = new StateTransition <S>(initial_state, end_state);

            if (TransitionTable.ContainsKey(temp_transition))
            {
                return;
            }

            TransitionTable.Add(temp_transition, call);
        }
Example #15
0
        public void ScanTest_StringLiteral()
        {
            string content    = "\"hello, world!\"";
            string vocabulary = "\"\\.*\" STRING_LITERAL";

            var table  = TransitionTable.FromVocabularyString(vocabulary);
            var tokens = Scanner.Scan(content, new ScannerOptions {
                Transitions = table
            });

            tokens.Should().BeSingle(STRING_LITERAL("hello, world!"));
        }
Example #16
0
        protected virtual TransitionTable OnCreateTransitionTable()
        {
            LR0Model lr0 = new LR0Model(m_productionInfoManager);

            lr0.BuildModel();

#if DEBUG
            Automaton = lr0.ToString();
#endif

            return(TransitionTable.Create(lr0, m_scannerInfo));
        }
        public void TestMethod1()
        {
            var list = MockFactory.TransitionListMocks.CreateDefaultTransitionList()
                       .WithSimpleRouteOf(HttpState.BeforeStart, HttpPipelineAction.NEXT, HttpState.Complete);

            var tt = new TransitionTable(list, CreateCreator());

            Assert.IsFalse(tt.IsComplete);
            tt.Move(HttpPipelineAction.NEXT);

            Assert.IsTrue(tt.IsComplete);
        }
        //Save
        private void MenuItem7_Click(object sender, EventArgs e)
        {
            IEnumerator enumerator = null;

            Transition.Transition transition      = new Transition.Transition();
            TransitionTable       transitionTable = new TransitionTable();

            if (this.Select_Group_A != null)
            {
                if (this.Select_Group_B != null)
                {
                    if (ObjectType.ObjTst(LateBinding.LateGet(this.Select_Group_A.SelectedItem, null, "Name", new object[0], null, null), LateBinding.LateGet(this.Select_Group_B.SelectedItem, null, "Name", new object[0], null, null), false) != 0)
                    {
                        string      str         = string.Format("{0} To {1}", RuntimeHelpers.GetObjectValue(LateBinding.LateGet(this.Select_Group_A.SelectedItem, null, "Name", new object[0], null, null)), RuntimeHelpers.GetObjectValue(LateBinding.LateGet(this.Select_Group_B.SelectedItem, null, "Name", new object[0], null, null)));
                        string      str1        = string.Format("{0}Data\\System\\2Way_Template.xml", AppDomain.CurrentDomain.BaseDirectory);
                        XmlDocument xmlDocument = new XmlDocument();
                        transitionTable.Clear();
                        try
                        {
                            xmlDocument.Load(str1);
                            try
                            {
                                enumerator = xmlDocument.SelectNodes("//Wizard/Tile").GetEnumerator();
                                while (enumerator.MoveNext())
                                {
                                    XmlElement            current     = (XmlElement)enumerator.Current;
                                    string                attribute   = current.GetAttribute("Pattern");
                                    string                attribute1  = current.GetAttribute("MapTile");
                                    string                attribute2  = current.GetAttribute("StaticTile");
                                    Transition.Transition transition1 = new Transition.Transition(str, attribute, (ClsTerrain)this.Select_Group_A.SelectedItem, (ClsTerrain)this.Select_Group_B.SelectedItem, this.Get_MapTiles(attribute1), this.Get_StaticTiles(attribute2));
                                    transitionTable.Add(transition1);
                                }
                            }
                            finally
                            {
                                if (enumerator is IDisposable)
                                {
                                    ((IDisposable)enumerator).Dispose();
                                }
                            }
                        }
                        catch (Exception exception)
                        {
                            ProjectData.SetProjectError(exception);
                            Interaction.MsgBox(exception.ToString(), MsgBoxStyle.OkOnly, null);
                            ProjectData.ClearProjectError();
                        }
                        transitionTable.Save(string.Format("{0}.xml", str));
                    }
                }
            }
        }
Example #19
0
        private static Dictionary <int, bool> getAcceptedStatesDictionary(TransitionTable transitions)
        {
            Dictionary <int, bool> acceptedStates = new Dictionary <int, bool>();

            //(States)
            for (int i = 0; i < transitions.states.Count; i++)
            {
                //If it is Accepted status
                acceptedStates.Add(i, transitions.states[i].Contains(transitions._followTable.nodes.Count - 1));
            }

            return(acceptedStates);
        }
    public void inputget()
    {
        str  = symbol + str + symbol;
        word = str.ToList();
        diplayInput();
        TransitionTable TT = new TransitionTable();

        tm = new TuringMachine(TT.transition_table(), word, symbol);
        input.gameObject.SetActive(false);
        btn.gameObject.SetActive(false);
        mainmenu.gameObject.SetActive(true);
        state.text = "Current State: q0";
        steps.text = "step: 0";
    }
Example #21
0
        public IEnumerable <Tuple <ParserHead, IProduction> > PanicRecover(TransitionTable transitions, SourceSpan lastLocation, bool isEos)
        {
            while (true)
            {
                int currentStateIndex = m_topStack.StateIndex;
                for (int i = 0; i < transitions.ProductionCount; i++)
                {
                    int gotoState = transitions.GetGoto(currentStateIndex, i);

                    if (gotoState > 0)
                    {
                        var recoverNT = transitions.NonTerminals[i];

                        if (isEos)
                        {
                            //the recoverNT must have EOS in its follow, otherwise continue
                            var follow = ((ProductionBase)recoverNT).Info.Follow;

                            if (!follow.Contains(EndOfStream.Instance))
                            {
                                continue;
                            }
                        }

                        //m_topStack = m_topStack.PrevNode;

                        var newNode = new StackNode(gotoState, m_topStack, ((ProductionBase)recoverNT).GetDefaultResult());

                        var newHead = Clone();
                        newHead.m_topStack = newNode;

                        newHead.IncreaseErrorRecoverLevel();
                        newHead.AddError(new ErrorRecord(null, lastLocation));


                        yield return(Tuple.Create(newHead, recoverNT));
                    }
                }

                if (m_topStack.PrevNode == null)
                {
                    yield break;
                    //throw new ParsingFailureException("There's no way to recover from parser error");
                }

                m_topStack       = m_topStack.PrevNode;
                m_lastShiftStack = m_topStack;
            }
        }
Example #22
0
        private void detailsButton_Click(object sender, EventArgs e)
        {
            try
            {
                FollowTable     follows     = new FollowTable(tree);
                TransitionTable transitions = new TransitionTable(follows);

                TreeDetails window = new TreeDetails(tree, follows, transitions);
                window.Show();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
Example #23
0
        /// <summary>
        /// Get all the next states of the specified state
        /// </summary>
        /// <param name="state">The state for which to get the next states</param>
        /// <returns>List of all next states if any, empty list otherwise</returns>
        public List <NfaState <Q> > GetNextStates(NfaState <Q> state)
        {
            var list = new List <NfaState <Q> >();

            if (state != null && IsExists(state.Id))
            {
                list.AddRange(from s in
                              TransitionTable.FindAll(tf => tf.From.Id.Equals(state.Id))
                              select s.To);
            }
            else
            {
                return(null);
            }

            return(list);
        }
Example #24
0
        public ParserEngine(TransitionTable transitions, SyntaxErrors errorDef)
        {
            CodeContract.RequiresArgumentNotNull(transitions, "transitions");
            CodeContract.RequiresArgumentNotNull(errorDef, "errorDef");

            m_transitions = transitions;
            m_reducer = new ReduceVisitor(transitions);

            m_heads = new List<ParserHead>();
            m_tempHeads = new List<ParserHead>();
            m_acceptedHeads = new List<ParserHead>();
            m_errorCandidates = new List<ParserHead>();
            m_errorDef = errorDef;

            //init state
            m_heads.Add(new ParserHead(new StackNode(0, null, null)));
        }
Example #25
0
        private static string getAcceptedStates(TransitionTable transitions)
        {
            string output = "";

            //Rows (States)
            for (int i = 0; i < transitions.states.Count; i++)
            {
                int Estado = i;

                //If it is Accepted status
                if (transitions.states[i].Contains(transitions._followTable.nodes.Count - 1))
                {
                    output += (output == "") ? $"Estado=={Estado}" : $" || Estado=={Estado}";
                }
            }

            return(output);
        }
Example #26
0
        private string CreateTransitionTable(TransitionTable transitionTable)
        {
            StringBuilder result = new StringBuilder();

            transitionTable.CalculateColumnSizes();

            result.AppendLine("//******************************************************");
            result.AppendLine("// Transition table");
            result.AppendLine("//******************************************************");
            result.AppendLine("const TransitionRow trans[] = {");
            // todo [cb] write a formatter for this
            result.AppendLine("// CURRENT ACTIVITY " + insertSpaces(transitionTable.GetColOneLen() - "CURRENT ACTIVITY".Length)
                              + "EVENT" + insertSpaces(transitionTable.GetColTwoLen() - "EVENT".Length + "  ".Length)
                              + "ACTION" + insertSpaces(transitionTable.GetColThreeLen() - "ACTION".Length + "  ".Length)
                              + "NEXT ACTIVITY" + insertSpaces(transitionTable.GetColFourLen() - "NEXT ACTIVITY".Length + "  ".Length)
                              + "GUARD");

            foreach (Row r in transitionTable.GetRows())
            {
                Int32 lenColFive = (transitionTable.GetColFiveLen() - r.GetGuard().Length - (r.GetGuard().Contains("(") ? 2 : 0) - 10);

                if (ConsumeGuardForTransitionTable(r.GetGuard()) == "NULL")
                {
                    if (r.GetGuard() != "else")
                    {
                        lenColFive -= 4;
                    }
                }
                // Todo [cb] maybe take "activity::" to the repository handler??
                result.AppendLine("{ ::Activity::" + r.GetCurrentActivity().GetName() + insertSpaces(transitionTable.GetColOneLen() - r.GetCurrentActivity().GetName().Length - 12)
                                  + ", ::Events::Any"
                                  + ", &FsmData::" + r.GetAction() + insertSpaces(transitionTable.GetColThreeLen() - r.GetAction().Length - 10)
                                  + ", ::Activity::" + r.GetNextActivity().GetName() + insertSpaces(transitionTable.GetColFourLen() - r.GetNextActivity().GetName().Length - 12)
                                  + GuardHelper(r) + insertSpaces(lenColFive > 0 ? lenColFive : 0)
                                  + " },");
            }

            RemoveLastSign(result);

            result.AppendLine("};");
            result.AppendLine("");

            return(result.ToString());
        }
Example #27
0
        public void ScanTest_MultiLineComment()
        {
            string content =
                @"/* comment */";

            string vocabulary = @"
                \s WHITE_SPACE
                \$ NEW_LINE
                /\*(\.|\$)*\*/ MULTI_LINE_COMMENT";

            var table   = TransitionTable.FromVocabularyString(vocabulary);
            var options = new ScannerOptions {
                Transitions = table, BlackList = Empty <TokenTag> .Array
            };
            var tokens = Scanner.Scan(content, options);

            tokens.Should().Be(new[] {
                MULTI_LINE_COMMENT("/* comment */"),
            });
        }
        public void Advance(S next_state, ActionTakerMythObject taker)
        {
            StateTransition <S> temp_state_transition = new StateTransition <S>(currentState, next_state);

            System.Delegate temp_delegate;
            if (TransitionTable.TryGetValue(temp_state_transition, out temp_delegate))
            {
                if (temp_delegate != null)
                {
                    StateHandler call = temp_delegate as StateHandler;
                    call(taker);
                }

                previousState = currentState;
                currentState  = next_state;
            }
            else
            {
                throw new FiniteStateMachineException();
            }
        }
Example #29
0
        public ILexer <T> CreateLexer()
        {
            // For each token, create a NFA
            IList <NFA> nfas = tokens.Select(token => NfaBuilder.Create(new ShuntingYard(new RegExLexer(new StringReader(token.Item1))))).ToList();

            foreach (var ignoreExpr in ignore)
            {
                nfas.Add(NfaBuilder.Create(new ShuntingYard(new RegExLexer(new StringReader(ignoreExpr)))));
            }

            // Create a merged NFA
            NFA mergedNfa = NFA.Merge(nfas);

            // If we desire a NFA based lexer, stop now
            if (Runtime == LexerRuntime.Nfa)
            {
                return(new NfaLexer <T>(mergedNfa, nfas, tokens, EndOfInputTokenNumber));
            }

            // Convert the NFA to a DFA
            DFA dfa = DFA.Create(mergedNfa);

            // Minimize the DFA if required
            dfa.Minimize();

            // If we desire a DFA based lexer, stop
            if (Runtime == LexerRuntime.Dfa)
            {
                // TODO:
                // The input ranges which will have been previously split into the smallest distinct
                // units will need to be recombined in order for this to work as fast as possible.
                //dfa.CombineInputRanges();
                return(new DfaLexer <T>(dfa, nfas, tokens, EndOfInputTokenNumber));
            }

            // Convert the dfa to table form
            var transitionTable = new TransitionTable <T>(dfa, nfas, tokens);

            return(new TabularLexer <T>(transitionTable, EndOfInputTokenNumber));
        }
Example #30
0
            public IEnumerable <Token> Accept(char symbol)
            {
                EState nextState;

                if (TransitionTable.TryGetValue(Tuple.Create(_state, Categorize(symbol)), out nextState))
                {
                    _state = nextState;
                    if (_state != EState.Start)
                    {
                        _buffer.Append(symbol);
                        Token token;
                        if (InterpretAcceptState(out token))
                        {
                            yield return(token);
                        }
                    }
                }
                else if (DefaultTransitionTable.TryGetValue(_state, out nextState))
                {
                    _state = nextState;
                    Token token;
                    if (InterpretAcceptState(out token))
                    {
                        yield return(token);
                    }

                    //Don't consume the symbol, run the symbol again.
                    foreach (var t in Accept(symbol))
                    {
                        yield return(t);
                    }
                }
                else
                {
                    throw new TokenizerException(string.Format("Unexpected symbol '{0}' in state {1}", symbol, _state));
                }
            }
        public void FromStringTest()
        {
            var expected = new TransitionTable(
                head: 0,
                transitions: new Dictionary <char, ushort>[]
            {
                new Dictionary <char, ushort> {
                    ['a'] = 1, ['b'] = 2
                },
                new Dictionary <char, ushort> {
                },
                new Dictionary <char, ushort> {
                }
            },
                aliases: new Dictionary <ushort, string>
            {
                [1] = "ID",
                [2] = "ID"
            }
                );
            var actual = TransitionTable.FromVocabularyString(@"a|b ID");

            actual.Should().Be(expected);
        }
Example #32
0
        /// <summary>
        /// Get all the next states of the specified state
        /// and that's reachable through the symbol <see cref="S"/>
        /// </summary>
        /// <param name="state">The state for which to get the next states</param>
        /// <param name="symbol">The connector symbol</param>
        /// <returns>List of all next states if any, empty list otherwise</returns>
        public virtual List <NfaState <Q> > GetNextStates(NfaState <Q> state, S symbol)
        {
            var list = new List <NfaState <Q> >();

            if (state != null && IsExists(state.Id))
            {
                list.AddRange((from s in
                               TransitionTable.FindAll(tf => tf.From.Id.Equals(state.Id) &&
                                                       tf.TransitionSymbols.Contains(symbol))
                               select s.To).Union(
                                  from t in TransitionTable.OfType <PredicateTransitionFunction <NfaState <Q>, S, NfaState <Q> > >()
                                  where
                                  t.From.Id.Equals(state.Id) && t.PredicateLambda != null &&
                                  t.PredicateLambda(symbol)
                                  select t.To
                                  ).Distinct());
            }
            else
            {
                return(null);
            }

            return(list);
        }
Example #33
0
        public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation, bool isEos)
        {
            while(true)
            {
                int currentStateIndex = m_topStack.StateIndex;
                for (int i = 0; i < transitions.ProductionCount; i++)
                {
                    int gotoState = transitions.GetGoto(currentStateIndex, i);

                    if (gotoState > 0)
                    {
                        var recoverNT = transitions.NonTerminals[i];

                        if (isEos)
                        {
                            //the recoverNT must have EOS in its follow, otherwise continue
                            var follow = ((ProductionBase)recoverNT).Info.Follow;

                            if (!follow.Contains(EndOfStream.Instance))
                            {
                                continue;
                            }
                        }

                        m_topStack = m_topStack.PrevNode;

                        var newNode = new StackNode(gotoState, m_topStack, null);
                        IncreaseErrorRecoverLevel();
                        AddError(new ErrorRecord(null, lastLocation));
                        m_topStack = newNode;

                        return recoverNT;
                    }
                }

                if (m_topStack.PrevNode == null)
                {
                    throw new ParsingFailureException("There's no way to recover from parser error");
                }

                m_topStack = m_topStack.PrevNode;
                m_lastShiftStack = m_topStack;
            }
        }
Example #34
0
        public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation)
        {
            while(true)
            {
                int currentStateIndex = m_topStack.StateIndex;
                for (int i = 0; i < transitions.ProductionCount; i++)
                {
                    int gotoState = transitions.GetGoto(currentStateIndex, i);

                    if (gotoState > 0)
                    {
                        var recoverNT = transitions.NonTerminals[i];

                        m_topStack = m_topStack.PrevNode;

                        var newNode = new StackNode(gotoState, m_topStack, null);
                        IncreaseErrorRecoverLevel();
                        AddError(new ErrorRecord(null, lastLocation));
                        m_topStack = newNode;

                        return recoverNT;
                    }
                }

                if (m_topStack.PrevNode == null)
                {
                    throw new ParsingFailureException("There's no way to recover from parser error");
                }

                m_topStack = m_topStack.PrevNode;
            }
        }