Push-Down Automata
pyformlang.pda
This module deals with push-down automata.
Available Classes
- PDA
A Push-Down Automaton
- State
A push-down automaton state
- Symbol
A push-down automaton symbol
- StackSymbol
A push-down automaton stack symbol
- Epsilon
A push-down automaton epsilon symbol
- class pyformlang.pda.PDA(states: AbstractSet[Any] = None, input_symbols: AbstractSet[Any] = None, stack_alphabet: AbstractSet[Any] = None, transition_function: TransitionFunction = None, start_state: Any = None, start_stack_symbol: Any = None, final_states: AbstractSet[Any] = None)[source]
Representation of a pushdown automaton
- Parameters:
- statesset of
State, optional A finite set of states
- input_symbolsset of
Symbol, optional A finite set of input symbols
- stack_alphabetset of
StackSymbol, optional A finite stack alphabet
- transition_function
TransitionFunction, optional Takes as arguments a state, an input symbol and a stack symbol and returns a state and a string of stack symbols push on the stacked to replace X
- start_state
State, optional A start state, element of states
- start_stack_symbol
StackSymbol, optional The stack is initialized with this stack symbol
- final_statesset of
State, optional A set of final or accepting states. It is a subset of states.
- statesset of
- add_final_state(state: Any)[source]
Adds a final state to the automaton
- Parameters:
- state
State The state to add
- state
- add_transition(s_from: Any, input_symbol: Any, stack_from: Any, s_to: Any, stack_to: Iterable[Any])[source]
Add a transition to the PDA
- Parameters:
- s_from
State The starting symbol
- input_symbol
Symbol The input symbol for the transition
- stack_from
StackSymbol The stack symbol of the transition
- s_to
State The new state
- stack_tolist of
StackSymbol The string of stack symbol which replace the stack_from
- s_from
- add_transitions(transitions)[source]
Adds several transitions
- Parameters:
- transitions
Transitions as they would be given to add_transition
- property final_states
The final states of the PDA :returns: final_states – The final states of the PDA :rtype: iterable of
State
- classmethod from_networkx(graph)[source]
Import a networkx graph into a PDA. The imported graph requires to have the good format, i.e. to come from the function to_networkx
- Parameters:
- graph
The graph representation of the PDA
- Returns:
- pda
A PDA automaton read from the graph
- get_number_transitions() int[source]
Gets the number of transitions in the PDA
- Returns:
- n_transitionsint
The number of transitions
- property input_symbols
The input symbols of the PDA
- Returns:
- input_symbolsiterable of
Symbol The input symbols of the PDA
- input_symbolsiterable of
- intersection(other: Any) PDA[source]
Gets the intersection of the language L generated by the current PDA when accepting by final state with something else
Currently, it only works for regular languages (represented as regular expressions or finite automata) as the intersection between two PDAs is not context-free (it cannot be represented with a PDA).
- Equivalent to:
>> pda and regex
- Parameters:
- otherany
The other part of the intersection
- Returns:
- new_pda
PDA The pda resulting of the intersection
- new_pda
- Raises:
- NotImplementedError
When intersecting with something else than a regex or a finite automaton
- set_start_stack_symbol(start_stack_symbol: Any)[source]
Sets the start stack symbol to the automaton
- Parameters:
- start_stack_symbol
StackSymbol The start stack symbol
- start_stack_symbol
- set_start_state(start_state: Any)[source]
Sets the start state to the automaton
- Parameters:
- start_state
State The start state
- start_state
- property stack_symbols
The stack symbols of the PDA
- Returns:
- stack_symbolsiterable of
StackSymbol The stack symbols of the PDA
- stack_symbolsiterable of
- property start_state
Get start state
- property states
Get the states fo the PDA :returns: states – The states of the PDA :rtype: iterable of
State
- to_cfg() CFG[source]
Turns the language L generated by this PDA when accepting on empty stack into a CFG that accepts the same language L
- Returns:
- new_cfg
CFG The equivalent CFG
- new_cfg
- to_dict()[source]
Get the transitions of the PDA as a dictionary :returns: transitions – The transitions :rtype: dict
- to_empty_stack() PDA[source]
Turns the current PDA that accepts a language L by final state to another PDA that accepts the same language L by empty stack
- Returns:
- new_pda
PDA The new PDA which accepts by empty stack the language that was accepted by final state
- new_pda
- to_final_state() PDA[source]
Turns the current PDA that accepts a language L by empty stack to another PDA that accepts the same language L by final state
- Returns:
- new_pda
PDA The new PDA which accepts by final state the language that was accepted by empty stack
- new_pda
- class pyformlang.pda.StackSymbol(value)[source]
A StackSymbol in a pushdown automaton
- Parameters:
- valueany
The value of the state
- property value
Returns the value of the stack symbol
- Returns:
- value: The value
any