Exemplo n.º 1
0
 public override int GetHashCode()
 {
     return(InputAlphabet.GetHashCode() ^
            StackAlphabet.GetHashCode() ^
            States.GetHashCode() ^
            StartState.GetHashCode() ^
            Transitions.GetHashCode());
 }
Exemplo n.º 2
0
 public override string ToString()
 {
     return(String.Format("PDA: StackAlpha:<{0}> StartState:<{1}>" +
                          "States:<{2}> InputAlpha:<{3}> Transitions:{4}",
                          String.Join(",", StackAlphabet.Select(s => s.ToString())),
                          StartState, String.Join(",", States.Select(s => s.ToString())),
                          String.Join(",", InputAlphabet.Select(s => s.ToString())),
                          String.Join(", ", Transitions.Select(t => t.ToString()))));
 }
Exemplo n.º 3
0
        public override bool Equals(Object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            var pda = (PDA)obj;

            return(InputAlphabet.SequenceEqual(pda.InputAlphabet) &&
                   StackAlphabet.SequenceEqual(pda.StackAlphabet) &&
                   States.SequenceEqual(pda.States) &&
                   StartState == pda.StartState &&
                   Transitions.SequenceEqual(pda.Transitions));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adiciona transição entre 2 estados.
        /// </summary>
        /// <param name="estadoOrigem">O estado origem do autômato.</param>
        /// <param name="consumirFita">O símbolo a ser avaliado.</param>
        /// <param name="estadoDestino">O estado destino.</param>

        public void AdicionarTransicao(string estadoOrigem, string estadoDestino, Symbol consumirPilha,
                                       Symbol consumirFita, params Symbol[] gravarPilha)
        {
            if (consumirPilha == null)
            {
                consumirPilha = Symbol.EmptySymbol;
            }
            SymbolList vars = new SymbolList();

            if (gravarPilha == null || gravarPilha.Length == 0)
            {
                vars.Add(Symbol.EmptySymbol);
            }
            else
            {
                vars.AddRange(gravarPilha);
            }


            foreach (var symbol in vars)
            {
                if (StackAlphabet.IndexOf(symbol) < 0 && symbol != Symbol.EmptySymbol && symbol != Symbol.StackFinal && symbol != Symbol.TapeFinal)
                {
                    StackAlphabet.Add(symbol);
                }
            }

            // Adiciona transições..
            if (States.ContainsKey(estadoOrigem) && States.ContainsKey(estadoDestino))
            {
                if (!Alphabet.Contains(consumirFita))
                {
                    Alphabet.Add(consumirFita);
                }

                States[estadoOrigem].AdicionarTransicao(States[estadoDestino], consumirFita, consumirPilha, vars);
            }
        }
Exemplo n.º 5
0
        private void ParsLine(string line)
        {
            string[] transition = line.Split(',');
            long     source;
            long     destination;

            //Recognize initial and final states and set source and destination.
            if (transition[0][0] == '*')
            {
                source = long.Parse(transition[0].Substring(2));
                if (source >= StateCount)
                {
                    throw InputIncorrectException;
                }

                if (!FinalStates.Contains(source))
                {
                    FinalStates.Add(source);
                }
            }
            else if (transition[0][0] == '-' && transition[0][1] == '>')
            {
                source = long.Parse(transition[0].Substring(3));
                if (!InitialStateSet)
                {
                    InitialState = source;
                }
                else if (InitialState != source || source >= StateCount)
                {
                    throw InputIncorrectException;
                }
            }
            else
            {
                source = long.Parse(transition[0].Substring(1));
            }

            if (transition[4][0] == '*')
            {
                destination = long.Parse(transition[4].Substring(2));
                if (destination >= StateCount)
                {
                    throw InputIncorrectException;
                }

                if (!FinalStates.Contains(destination))
                {
                    FinalStates.Add(destination);
                }
            }
            else if (transition[4][0] == '-' && transition[4][1] == '>')
            {
                destination = long.Parse(transition[4].Substring(3));
                if (!InitialStateSet)
                {
                    InitialState = destination;
                }
                else if (InitialState != destination || destination >= StateCount)
                {
                    throw InputIncorrectException;
                }
            }
            else
            {
                destination = long.Parse(transition[4].Substring(1));
            }

            //Check for transition alphabet correctness
            if (transition[1] != "_" && !Alphabet.Contains(transition[1][0]))
            {
                throw InputIncorrectException;
            }

            //Add transition
            Transitions[source].Item1.Add(destination);
            Transitions[source].Item2.Add(transition[1][0]);

            //Check for stack alphabet correctness
            if (transition[2].Length != 1)   //pop element
            {
                throw InputIncorrectException;
            }
            else if (transition[2][0] != '_' && transition[2][0] != StackInitialSymbol && !StackAlphabet.Contains(transition[2][0]))
            {
                throw InputIncorrectException;
            }

            if (transition[3] != "_" && transition[3].Length != 2)   //push element
            {
                throw InputIncorrectException;
            }
            foreach (char chr in transition[3])
            {
                if (chr != '_' && chr != StackInitialSymbol && !StackAlphabet.Contains(chr))
                {
                    throw InputIncorrectException;
                }
            }

            //Add pop and push elements
            Transitions[source].Item3.Add(transition[2][0]);
            Transitions[source].Item4.Add(transition[3]);
        }