private bool IsTokenExpected(ParsingTableState state, int tokenIndex)
        {
            if (state.IsNullable)
            {
                return(true);
            }

            if (tokenIndex >= _pool.Tokens.Count && !state.IsNullable)
            {
                return(false);
            }
            if (tokenIndex >= _pool.Tokens.Count)
            {
                return(true);
            }

            var type = _pool.Tokens[tokenIndex].Class == TokenClass.Identifier
                ? _pool.Identifiers[_pool.Tokens[tokenIndex].Id].Type
                : _pool.Tokens[tokenIndex].Id;

            return(state.ExpectedTokens.Any(pair => pair.Key == _pool.Tokens[tokenIndex].Class &&
                                            pair.Value.Equals(type)));
        }
Exemple #2
0
        public static ICollection <ParsingTableState> ParseTransitionsTable(string text)
        {
            var stateList  = new List <ParsingTableState>();
            var blockLines = text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                             .Select(s => s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

            foreach (var items in blockLines)
            {
                if (items.Length == 3)
                {
                    stateList.Last().ExpectedTokens
                    .Add(new KeyValuePair <TokenClass, int>((TokenClass)int.Parse(items[1]), int.Parse(items[2])));
                    continue;
                }

                var state = new ParsingTableState
                {
                    Id = int.Parse(items[0]) - 1,
                    TransisionStateNumber  = int.Parse(items[3]) - 1,
                    IsAcceptRequired       = items[4].Equals("+"),
                    PushToStack            = items[5].Equals("-") ? null : new int?(int.Parse(items[5]) - 1),
                    IsPopFromStackRequired = items[6].Equals("+"),
                    IsErrorOccured         = items[7].Equals("+"),
                    IsNullable             = items.Length >= 9 && items[8].Equals("!")
                };

                if (!items[1].Equals("?"))
                {
                    state.ExpectedTokens.Add(new KeyValuePair <TokenClass, int>((TokenClass)int.Parse(items[1]),
                                                                                int.Parse(items[2])));
                }

                stateList.Add(state);
            }

            return(stateList);
        }