예제 #1
0
파일: Trie.cs 프로젝트: smorken/generictrie
 /// <summary>
 /// Get a child node of this node using its value to find it
 /// </summary>
 /// <param name="Value">The value of the node to find</param>
 /// <returns>The node</returns>
 private TrieNode GetChild(TToken Value)
 {
     if (Children != null && Children.ContainsKey(Value))
     {
         return(Children[Value]);
     }
     return(null);
 }
예제 #2
0
파일: Trie.cs 프로젝트: smorken/generictrie
            /// <summary>
            /// Test if a node contains a particular child
            /// </summary>
            /// <param name="Value">The value to test</param>
            /// <returns>True if the node contains the chil, otherwise false</returns>
            private bool ContainsChild(TToken Value)
            {
                if (Children == null)
                {
                    return(false);
                }

                else if (Children.ContainsKey(Value))
                {
                    return(true);
                }
                return(false);
            }
예제 #3
0
                private LexerDfaState(LexerDfaState state) : base(state)
                {
                    if (SuccessSubGraph == null)
                    {
                        return;
                    }

                    var lexerStateRule = ((LexerState)SuccessSubGraph.State).Rule;

                    Token     = lexerStateRule.Token;
                    TokenCode = lexerStateRule.TokenCode;
                    Skip      = lexerStateRule.Skip;
                }
예제 #4
0
        public void add_word(string word, TToken type)
        {
            flag_exist = false;
            switch (type)
            {
            case TToken.lxmIdentifier:
                for (int i = 0; i < count_letter_words; i++)
                {
                    if (word == letter_words[i])
                    {
                        flag_exist = true;
                    }
                }
                if (!flag_exist)
                {
                    letter_words[count_letter_words] = word;
                    count_letter_words++;
                }
                break;

            case TToken.lxmNumber:
                for (int i = 0; i < count_digit_words; i++)
                {
                    if (word == digit_words[i])
                    {
                        flag_exist = true;
                    }
                }
                if (!flag_exist)
                {
                    digit_words[count_digit_words] = word;
                    count_digit_words++;
                }
                break;

            default:
                for (int i = 0; i < count_symbolic_words; i++)
                {
                    if (word == symbolic_words[i])
                    {
                        flag_exist = true;
                    }
                }
                if (!flag_exist)
                {
                    symbolic_words[count_symbolic_words] = word;
                    count_symbolic_words++;
                }
                break;
            }
        }
예제 #5
0
                public LexerDfaState(DfaNode[] nodes, DfaTransition[] lazyTransitions, DfaTransition successTransition, DfaTransition prevSuccessTransition, int hashCode, LexerDfaBuilder builder, bool build = true,
                                     LexerDfaState[] array = null) : base(nodes, lazyTransitions, successTransition,
                                                                          prevSuccessTransition, hashCode, builder)
                {
                    if (SuccessSubGraph == null)
                    {
                        return;
                    }

                    var lexerStateRule = ((LexerState)SuccessSubGraph.State).Rule;

                    Token     = lexerStateRule.Token;
                    TokenCode = lexerStateRule.TokenCode;
                    Skip      = lexerStateRule.Skip;
                }
예제 #6
0
파일: Trie.cs 프로젝트: smorken/generictrie
            private List <TValue> GetMatchingValues(TToken[] Keys, int Index, List <TValue> Values, bool PrefixSearch)
            {
                if (Index == Keys.Length)
                {
                    if (this.Terminal)
                    {
                        //if (!PrefixSearch)
                        //{
                        Values.Add(this.Value);
                        //}
                        //else
                        //{
                        //    Values.Add(this.Value);
                        //    return Values;
                        //}
                    }
                    if (PrefixSearch && Children != null)
                    {
                        foreach (KeyValuePair <TToken, TrieNode> value in Children)
                        {
                            value.Value.GetMatchingValues(Keys, Index, Values, PrefixSearch);
                        }
                    }
                    return(Values);
                }
                if (Children != null && container.WildCardIsSet && Keys[Index].CompareTo(container.WildCard) == 0)
                {
                    foreach (KeyValuePair <TToken, TrieNode> value in Children)
                    {
                        TToken[] TestValues = new TToken[Keys.Length];
                        Keys.CopyTo(TestValues, 0);
                        TestValues[Index] = value.Key;
                        GetMatchingValues(TestValues, Index, Values, PrefixSearch);
                    }
                }
                else
                {
                    if (this.ContainsChild(Keys[Index]))
                    {
                        return(GetChild(Keys[Index]).GetMatchingValues(Keys, Index + 1, Values, PrefixSearch));
                    }
                }

                return(Values);
            }
예제 #7
0
            internal sealed override Result <TToken, TEnumerable> Parse(IParseState <TToken> state)
            {
                var consumedInput = false;

                foreach (var x in _valueList)
                {
                    var result = state.Peek();
                    if (!result.HasValue)
                    {
                        return(Result.Failure <TToken, TEnumerable>(
                                   new ParseError <TToken>(
                                       result,
                                       true,
                                       Expected,
                                       state.SourcePos,
                                       null
                                       ),
                                   consumedInput
                                   ));
                    }

                    TToken token = result.GetValueOrDefault();
                    if (!token.Equals(x))
                    {
                        return(Result.Failure <TToken, TEnumerable>(
                                   new ParseError <TToken>(
                                       result,
                                       false,
                                       Expected,
                                       state.SourcePos,
                                       null
                                       ),
                                   consumedInput
                                   ));
                    }

                    consumedInput = true;
                    state.Advance();
                }
                return(Result.Success <TToken, TEnumerable>(_value, consumedInput));
            }
예제 #8
0
파일: Trie.cs 프로젝트: smorken/generictrie
            /// <summary>
            /// Recursive sub function for the Contains method group
            /// </summary>
            /// <param name="Values">The value composing the word to check</param>
            /// <param name="Index">The index into the word being evaluated</param>
            /// <returns>True if the word is found, otherwise false</returns>
            private bool Contains(TToken[] Values, int Index)
            {
                if (Index == Values.Length)
                {
                    if (Terminal)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }

                if (Children != null && container.WildCardIsSet && Values[Index].CompareTo(container.WildCard) == 0)
                {
                    foreach (KeyValuePair <TToken, TrieNode> value in Children)
                    {
                        TToken[] TestValues = new TToken[Values.Length];
                        Values.CopyTo(TestValues, 0);
                        TestValues[Index] = value.Key;
                        bool Contained = Contains(TestValues, Index);
                        if (Contained == true)
                        {
                            return(true);
                        }
                    }
                }
                else
                {
                    if (this.ContainsChild(Values[Index]))
                    {
                        return(GetChild(Values[Index]).Contains(Values, Index + 1));
                    }
                }

                return(false);
            }
예제 #9
0
            internal sealed override InternalResult <TEnumerable> Parse(IParseState <TToken> state)
            {
                var consumedInput = false;

                foreach (var x in _valueTokens)
                {
                    var result = state.Peek();
                    if (!result.HasValue)
                    {
                        state.Error = new ParseError <TToken>(
                            result,
                            true,
                            Expected,
                            state.SourcePos,
                            null
                            );
                        return(InternalResult.Failure <TEnumerable>(consumedInput));
                    }

                    TToken token = result.GetValueOrDefault();
                    if (!EqualityComparer <TToken> .Default.Equals(token, x))
                    {
                        state.Error = new ParseError <TToken>(
                            result,
                            false,
                            Expected,
                            state.SourcePos,
                            null
                            );
                        return(InternalResult.Failure <TEnumerable>(consumedInput));
                    }

                    consumedInput = true;
                    state.Advance();
                }
                return(InternalResult.Success <TEnumerable>(_value, consumedInput));
            }
예제 #10
0
파일: Trie.cs 프로젝트: smorken/generictrie
            private List <TToken[]> GetMatchingKeys(TToken[] Keys, int Index, List <TToken[]> matches, bool PrefixSearch)
            {
                if (Index == Keys.Length)
                {
                    if (this.Terminal)
                    {
                        matches.Add(this.Word.ToArray());
                    }
                    if (PrefixSearch && Children != null)
                    {
                        foreach (KeyValuePair <TToken, TrieNode> value in Children)
                        {
                            value.Value.GetMatchingKeys(Keys, Index, matches, PrefixSearch);
                        }
                    }
                    return(matches);
                }
                if (Children != null && container.WildCardIsSet && Keys[Index].CompareTo(container.WildCard) == 0)
                {
                    foreach (KeyValuePair <TToken, TrieNode> value in Children)
                    {
                        TToken[] TestValues = new TToken[Keys.Length];
                        Keys.CopyTo(TestValues, 0);
                        TestValues[Index] = value.Key;
                        GetMatchingKeys(TestValues, Index, matches, PrefixSearch);
                    }
                }
                else
                {
                    if (this.ContainsChild(Keys[Index]))
                    {
                        return(GetChild(Keys[Index]).GetMatchingKeys(Keys, Index + 1, matches, PrefixSearch));
                    }
                }

                return(matches);
            }
예제 #11
0
 public Container(TToken token, TAuth data)
 {
     Token = token;
     Data  = data;
 }
예제 #12
0
 public StateResult(int?state, TToken result)
 {
     State  = state;
     Result = result;
 }
예제 #13
0
파일: CLex.cs 프로젝트: Sl1ed/lab3TFGIA
        public void NextToken()
        {
            strFLexicalUnit = "";
            if (enumFState == TState.Start)
            {
                intFSourceRowSelection = 0;
                intFSourceColSelection = -1;
                GetSymbol();
            }
            while (enumFSelectionCharType == TCharType.Space || enumFSelectionCharType == TCharType.EndRow)
            {
                GetSymbol();
            }
            if (chrFSelection == '/')
            {
                GetSymbol();
                if (chrFSelection == '/')
                {
                    while (enumFSelectionCharType != TCharType.EndRow)
                    {
                        GetSymbol();
                    }
                }
                GetSymbol();
            }
            // Вариант 3
            switch (enumFSelectionCharType)
            {
            case TCharType.Letter:
            {
                //          a       b     c      d
                //  AFin | AFin | CFin | AFin | AFin |
                //  CFin | AFin |      | AFin | AFin |
AFin:
                {
                    if (chrFSelection == 'a' || chrFSelection == 'c' || chrFSelection == 'd')
                    {
                        TakeSymbol();
                        goto AFin;
                    }
                    else if (chrFSelection == 'b')
                    {
                        TakeSymbol();
                        goto CFin;
                    }
                    else
                    {
                        enumFToken = TToken.lxmIdentifier;
                        return;
                    }
                }
CFin:
                {
                    if (chrFSelection == 'a' || chrFSelection == 'c' || chrFSelection == 'd')
                    {
                        TakeSymbol();
                        goto AFin;
                    }
                    else
                    {
                        throw new Exception("Есть bb");
                    }
                }
            }

            case TCharType.Digit:
            {
                //           0     1
                //    A   |  BC |     |
                //    BC  |  D  |  E  |
                //    D   |  A  |     |
                //    E   | FFin|     |
                //   FFin |  G  |     |
                //    G   |     |  H  |
                //    H   |     | FFin|



A:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto BC;
                }

                else
                {
                    throw new Exception("Ожидался 0");
                }

BC:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto D;
                }
                else if (chrFSelection == '1')
                {
                    TakeSymbol();
                    goto E;
                }
                else
                {
                    throw new Exception("Ожидался 0 или 1");
                }

D:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto A;
                }
                else
                {
                    throw new Exception("Ожидался 0");
                }

E:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto FFin;
                }
                else
                {
                    throw new Exception("Ожидался 0");
                }


FFin:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto G;
                }
                else if (enumFSelectionCharType != TCharType.Digit)
                {
                    enumFToken = TToken.lxmNumber; return;
                }
                else
                {
                    throw new Exception("Ожидалась 0");
                }

G:
                if (chrFSelection == '1')
                {
                    TakeSymbol();
                    goto H;
                }
                else
                {
                    throw new Exception("Ожидалась 1");
                }

H:
                if (chrFSelection == '1')
                {
                    TakeSymbol();
                    goto FFin;
                }
                else
                {
                    throw new Exception("Ожидалась 1");
                }
            }

            case TCharType.ReservedSymbol:
            {
                if (chrFSelection == '/')
                {
                    GetSymbol();
                    if (chrFSelection == '/')
                    {
                        while (enumFSelectionCharType != TCharType.EndRow)
                        {
                            GetSymbol();
                        }
                    }
                    GetSymbol();
                }
                if (chrFSelection == '(')
                {
                    enumFToken = TToken.lxmLeftParenth;
                    GetSymbol();
                    return;
                }
                if (chrFSelection == ')')
                {
                    enumFToken = TToken.lxmRightParenth;
                    GetSymbol();
                    return;
                }
                if (chrFSelection == '[')
                {
                    enumFToken = TToken.lxmls;
                    GetSymbol();
                    return;
                }
                if (chrFSelection == ']')
                {
                    enumFToken = TToken.lxmrs;
                    GetSymbol();
                    return;
                }
                if (chrFSelection == ',')
                {
                    enumFToken = TToken.lxmComma;
                    GetSymbol();
                    return;
                }
                if (chrFSelection == ':')
                {
                    enumFToken = TToken.lxmdt;
                    GetSymbol();
                    return;
                }
                if (chrFSelection == '=')
                {
                    enumFToken = TToken.lxmr;
                    GetSymbol();
                    return;
                }
                break;
            }

            case TCharType.EndText:
            {
                enumFToken = TToken.lxmEmpty;
                break;
            }
            }
        }
예제 #14
0
        public void NextToken()
        {
            strFLexicalUnit = "";
            if (enumFState == TState.Start)
            {
                intFSourceRowSelection = 0;
                intFSourceColSelection = -1;
                GetSymbol();
            }

            while (enumFSelectionCharType == TCharType.Space || enumFSelectionCharType == TCharType.EndRow)
            {
                GetSymbol();
            }

            if (chrFSelection == '/')
            {
                GetSymbol();
                if (chrFSelection == '/')
                {
                    while (enumFSelectionCharType != TCharType.EndRow)
                    {
                        GetSymbol();
                    }
                }
                GetSymbol();
            }


            // Вариант 4
            switch (enumFSelectionCharType)
            {
            case TCharType.Letter:
            {
A:
                {
                    if (chrFSelection == 'b' || chrFSelection == 'c' || chrFSelection == 'd')
                    {
                        TakeSymbol();
                        goto AFin;
                    }
                    else if (chrFSelection == 'a')
                    {
                        TakeSymbol();
                        goto BFin;
                    }
                    else
                    {
                        throw new Exception("Ошибка в идентификаторе");
                    }
                }
AFin:
                {
                    if (chrFSelection == 'a' || chrFSelection == 'b' || chrFSelection == 'd' || chrFSelection == 'c')
                    {
                        TakeSymbol();
                        goto AFin;
                    }
                    else
                    {
                        enumFToken = TToken.lxmIdentifier;
                        return;
                    }
                }
BFin:
                {
                    if (chrFSelection == 'b')
                    {
                        TakeSymbol();
                        throw new Exception("Ошибка в идентификаторе");
                    }
                    else if (chrFSelection == 'a' || chrFSelection == 'c' || chrFSelection == 'd')
                    {
                        TakeSymbol();
                        goto AFin;
                    }
                    else
                    {
                        enumFToken = TToken.lxmIdentifier;
                        return;
                    }
                }
            }

            case TCharType.Digit:
            {
A:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto B;
                }
                else
                {
                    throw new Exception("Ожидалось 0");
                }
B:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto C;
                }
                else if (chrFSelection == '1')
                {
                    TakeSymbol();
                    goto DFin;
                }
                else
                {
                    throw new Exception("Ожидалась цифра");
                }
C:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto A;
                }
                else
                {
                    throw new Exception("Ожидалось 0");
                }
DFin:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto E;
                }
                else
                {
                    throw new Exception("Ожидалось 0");
                }
E:
                if (chrFSelection == '1')
                {
                    TakeSymbol();
                    goto F;
                }
                else if (enumFSelectionCharType != TCharType.Digit)
                {
                    enumFToken = TToken.lxmNumber; return;
                }
                else
                {
                    throw new Exception("Ожидалось 1");
                }
F:
                if (chrFSelection == '0')
                {
                    TakeSymbol();
                    goto DFin;
                }
                else
                {
                    throw new Exception("Ожидалось 0");
                }
            }

            case TCharType.ReservedSymbol:
            {
                if (chrFSelection == '/')
                {
                    GetSymbol();
                    if (chrFSelection == '/')
                    {
                        intFSourceColSelection = strFSource[intFSourceRowSelection].Length;
                    }
                }
                if (chrFSelection == ':')
                {
                    enumFToken = TToken.lxmColon;
                }
                if (chrFSelection == '-')
                {
                    enumFToken = TToken.lxmDash;
                }
                if (chrFSelection == '.')
                {
                    enumFToken = TToken.lxmDot;
                }
                if (chrFSelection == ',')
                {
                    enumFToken = TToken.lxmComma;
                }
                if (chrFSelection == '(')
                {
                    enumFToken = TToken.lxmLeftParenth;
                }
                if (chrFSelection == ')')
                {
                    enumFToken = TToken.lxmRightParenth;
                }
                TakeSymbol();
                break;
            }

            case TCharType.EndText:
            {
                enumFToken = TToken.lxmEmpty;
                break;
            }
            }
        }
 public ParserSingleMatchEntry(string name, TToken operand) : base(operand)
 {
     ParserEntryData = new ParserEntryData(name, this);
 }
예제 #16
0
 internal TokenRule(TToken token)
 {
     Token       = token;
     GrammarType = GetGrammarType();
 }