TuringSim
C++ framework to simulate abstract computing models
deterministicSimplePushdownMachine.h
1 #pragma once
2 
3 #include <machine/PDM/simplePushdownMachine.h>
4 #include <transition/PDM/simplePushdownMachineTransition.h>
5 
20  template <
21  typename StateType,
22  typename LetterType,
23  typename StackSymbolType,
24  AcceptingStyle acceptingStyle = AcceptingStyle::Accepting,
26  >
28 
41  template <
42  typename StateType_,
43  typename LetterType_,
44  typename StackSymbolType_,
45  AcceptingStyle acceptingStyle,
46  typename... Args
47  >
49  StateType_,
50  LetterType_,
51  StackSymbolType_,
52  acceptingStyle,
53  std::tuple<Args...>
54  >
55  : public SimplePushdownMachine<StateType_, LetterType_, StackSymbolType_, true, acceptingStyle, std::set, std::set>{
58  public:
59  using typename SimplePushdownMachine_::WordType;
60  using typename SimplePushdownMachine_::StackType;
61  using typename SimplePushdownMachine_::StateType;
62  using typename SimplePushdownMachine_::LetterType;
63  using typename SimplePushdownMachine_::IsAccepting;
64  using typename SimplePushdownMachine_::StorageType;
65  using typename SimplePushdownMachine_::IsAlternating;
66  using typename SimplePushdownMachine_::TransitionType;
67  using typename SimplePushdownMachine_::ApplyHelperType;
68  using typename SimplePushdownMachine_::IsDeterministic;
69  using typename SimplePushdownMachine_::StackSymbolType;
70  using typename SimplePushdownMachine_::TransitionContainer;
71  using typename SimplePushdownMachine_::InitialStateContainer;
72  using typename SimplePushdownMachine_::OptionalHelpedTransition;
73 
74  static_assert(std::is_same_v<StateType, typename TransitionType::StateType>);
75  static_assert(std::is_same_v<StorageType, typename TransitionType::StorageType>);
76  static_assert(std::is_same_v<TransitionType, TransitionType_>);
77  static_assert(std::is_same_v<ApplyHelperType, typename TransitionType::ApplyHelperType>);
78  static_assert(std::is_same_v<ApplyHelperType, std::monostate>);
79  static_assert(std::is_same_v<IsDeterministic, std::true_type>);
80  static_assert(std::is_same_v<TransitionContainer, std::set<std::pair<const TransitionType&, ApplyHelperType>>>);
81  static_assert(std::is_same_v<InitialStateContainer, std::set<StateType>>);
82  static_assert(std::is_same_v<OptionalHelpedTransition, std::optional<std::pair<const TransitionType&, ApplyHelperType>>>);
83 
84  static_assert(std::is_same_v<StorageType, Transition::PDM::PushdownMachineStorage<LetterType, StackSymbolType>>,
85  "StorageType should be a word and a stack.");
86  static_assert(std::is_same_v<TransitionType_, TransitionType>,
87  "TransitionType must be SimplePushdownTransition");
88  static_assert(std::is_same_v<std::tuple<Args...>, typename impl_details_::StateAcceptingMachineArgs<StateType, acceptingStyle>::AcceptingConstructorArgs>,
89  "The only valid value for AcceptingConstructorArgs is the default parameter.");
90 
93 
100  const StateType& initialState,
101  const std::vector<TransitionType>& transitions,
102  Args... acceptingArgs
103  ) :
104  SimplePushdownMachine_(acceptingArgs...),
105  initialState(initialState),
106  transitions()
107  {
108  for(const TransitionType& transition: transitions) {
109  this->transitions[transition.getPreState()].insert({transition.getPreLetter(), transition});
110  }
111  }
112 
126  virtual ~DeterministicSimplePushdownMachine() override = default;
127 
135  virtual StateType getInitialState() const noexcept override final {
136  return initialState;
137  }
138 
153  virtual OptionalHelpedTransition getMatchingTransitionFromCurrentLetter(const StateType& state, const std::optional<LetterType>& letter, const StackType& stack) const noexcept override final {
154  try {
155  return {{transitions.at(state).at(letter.value()).at(stack.top()), std::monostate()}};
156  }
157  catch(std::out_of_range) {
158  return {};
159  }
160  catch(std::bad_optional_access) {
161  return {};
162  }
164  return {};
165  }
166  }
167 
168  protected:
171  StateType initialState;
172 
175  std::map<StateType, std::map<LetterType, std::map<StackSymbolType, TransitionType>>> transitions;
176  };
177 }
TuringSim::Machine::PDM::SimplePushdownMachine
The class to represent a pushdown machine with simple transitions.
Definition: simplePushdownMachine.h:34
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::initialState
StateType initialState
The initial state.
Definition: deterministicSimplePushdownMachine.h:171
TuringSim::Transition::PDM::PushdownMachineStorage
std::tuple< Memory::Word::Word< WordAlphabet >, Memory::Stack::Stack< StackAlphabet > > PushdownMachineStorage
The type of the memory of a pushdown machine from the types of letters and symbols on the stack.
Definition: pushdownMachineTransition.h:15
TuringSim::Machine::impl_details_::StateAcceptingMachineArgs
This class provide a single type AcceptingConstructorArgs that is the tuple of parameters used to dec...
Definition: stateAcceptingMachine.h:38
TuringSim::Machine::PDM
Namespace of pushdown machines, that is whose storage is a word and a stack.
Definition: deterministicSimplePushdownMachine.h:6
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::getInitialState
virtual StateType getInitialState() const noexcept override final
Returns the initial state of the machine.
Definition: deterministicSimplePushdownMachine.h:135
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::transitions
std::map< StateType, std::map< LetterType, std::map< StackSymbolType, TransitionType > > > transitions
Set of transitions in a 3-level map: state to letter to top stack symbol to transition.
Definition: deterministicSimplePushdownMachine.h:175
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::DeterministicSimplePushdownMachine
DeterministicSimplePushdownMachine(const StateType &initialState, const std::vector< TransitionType > &transitions, Args... acceptingArgs)
Builds a DeterministicSimplePushdownMachine from its components.
Definition: deterministicSimplePushdownMachine.h:99
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::~DeterministicSimplePushdownMachine
virtual ~DeterministicSimplePushdownMachine() override=default
The default virtual destructor.
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine
The class to represent a deterministic simple pushdown machine.
Definition: deterministicSimplePushdownMachine.h:27
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::DeterministicSimplePushdownMachine
DeterministicSimplePushdownMachine(const DeterministicSimplePushdownMachine &)=default
The default copy constructor.
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::DeterministicSimplePushdownMachine
DeterministicSimplePushdownMachine()=delete
The deleted default constructor.
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::operator=
DeterministicSimplePushdownMachine & operator=(const DeterministicSimplePushdownMachine &)=default
The default copy assignment operator.
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::DeterministicSimplePushdownMachine
DeterministicSimplePushdownMachine(DeterministicSimplePushdownMachine &&)=default
The default move constructor.
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::operator=
DeterministicSimplePushdownMachine & operator=(DeterministicSimplePushdownMachine &&)=default
The default move assignment operator.
TuringSim::Transition::PDM::SimplePushdownMachineTransition
Transition of pushdown machines, i.e. transition on a word and a stack.
Definition: simplePushdownMachineTransition.h:20
TuringSim::Machine::AcceptingStyle
AcceptingStyle
Whether the machine is accepting, alternating or nothing.
Definition: acceptingMachine.h:11
TuringSim::Machine::PDM::DeterministicSimplePushdownMachine< StateType_, LetterType_, StackSymbolType_, acceptingStyle, std::tuple< Args... > >::getMatchingTransitionFromCurrentLetter
virtual OptionalHelpedTransition getMatchingTransitionFromCurrentLetter(const StateType &state, const std::optional< LetterType > &letter, const StackType &stack) const noexcept override final
Get matching transition for the next step.
Definition: deterministicSimplePushdownMachine.h:153
TuringSim::Memory::Stack::StackTooSmallException
Exception thrown when we try to pop or top an empty stack, or pops or tops a too small stack.
Definition: stack.h:18