1- """
2- Representation of a recursive automaton
3- """
1+ """Representation of a recursive automaton."""
42
53from typing import Dict , Set , AbstractSet , Optional , Hashable , Any
64
1311
1412
1513class RecursiveAutomaton :
16- """ Represents a recursive automaton
17-
18- This class represents a recursive automaton.
14+ """Representation of a recursive automaton.
1915
2016 Parameters
2117 ----------
22- start_box : :class:`~pyformlang.rsa.Box`
23- Start box
24- boxes : set of :class:`~pyformlang.rsa.Box`
25- A finite set of boxes
26-
18+ start_box:
19+ A start box of the recursive automaton.
20+ boxes:
21+ A finite set of boxes.
2722 """
2823
2924 def __init__ (self ,
3025 start_box : Box ,
3126 boxes : AbstractSet [Box ]) -> None :
27+ """Initializes the recursive automaton."""
3228 self ._nonterminal_to_box : Dict [Symbol , Box ] = {}
3329 self ._start_nonterminal = start_box .nonterminal
3430 if start_box not in boxes :
@@ -38,62 +34,58 @@ def __init__(self,
3834
3935 @property
4036 def nonterminals (self ) -> Set [Symbol ]:
41- """ The set of nonterminals """
37+ """Gets the set of nonterminals of the automaton. """
4238 return set (self ._nonterminal_to_box .keys ())
4339
4440 @property
4541 def boxes (self ) -> Set [Box ]:
46- """ The set of boxes """
42+ """Gets the set of boxes of the automaton. """
4743 return set (self ._nonterminal_to_box .values ())
4844
4945 @property
5046 def start_nonterminal (self ) -> Symbol :
51- """ The start nonterminal """
47+ """Gets the start nonterminal of the automaton. """
5248 return self ._start_nonterminal
5349
5450 @property
5551 def start_box (self ) -> Box :
56- """ The start box """
52+ """Gets the start box of the automaton. """
5753 return self ._nonterminal_to_box [self .start_nonterminal ]
5854
5955 def get_box_by_nonterminal (self , nonterminal : Hashable ) -> Optional [Box ]:
60- """
61- Box by nonterminal
56+ """Gets a box by the given nonterminal.
6257
6358 Parameters
6459 ----------
65- nonterminal: :class:`~pyformlang.finite_automaton.Symbol` | str
66- the nonterminal of which represents a box
60+ nonterminal:
61+ A nonterminal representing a box.
6762
6863 Returns
69- -----------
70- box : :class:`~pyformlang.rsa.Box` | None
71- box represented by given nonterminal
64+ -------
65+ The box represented by the given nonterminal.
7266 """
73-
7467 nonterminal = to_symbol (nonterminal )
7568 return self ._nonterminal_to_box .get (nonterminal , None )
7669
7770 def get_number_boxes (self ) -> int :
78- """ Size of set of boxes """
71+ """Gets the number of boxes in the current automaton. """
7972 return len (self ._nonterminal_to_box )
8073
8174 @classmethod
8275 def from_regex (cls , regex : Regex , start_nonterminal : Hashable ) \
8376 -> "RecursiveAutomaton" :
84- """ Create a recursive automaton from regular expression
77+ """Creates a recursive automaton from regular expression.
8578
8679 Parameters
87- -----------
88- regex : :class:`~pyformlang.regular_expression.Regex`
89- The regular expression
90- start_nonterminal : :class:`~pyformlang.finite_automaton.Symbol` | str
91- The start nonterminal for the recursive automaton
80+ ----------
81+ regex:
82+ The regular expression to create automaton from.
83+ start_nonterminal:
84+ The start nonterminal for the recursive automaton.
9285
9386 Returns
94- -----------
95- rsa : :class:`~pyformlang.rsa.RecursiveAutomaton`
96- The new recursive automaton built from regular expression
87+ -------
88+ The new recursive automaton built from regular expression.
9789 """
9890 start_nonterminal = to_symbol (start_nonterminal )
9991 box = Box (regex .to_minimal_dfa (), start_nonterminal )
@@ -102,21 +94,18 @@ def from_regex(cls, regex: Regex, start_nonterminal: Hashable) \
10294 @classmethod
10395 def from_ebnf (cls , text : str , start_nonterminal : Hashable = "S" ) \
10496 -> "RecursiveAutomaton" :
105- """ Create a recursive automaton from ebnf \
106- (ebnf = Extended Backus-Naur Form)
97+ """Creates a recursive automaton from Extended Backus-Naur Form.
10798
10899 Parameters
109100 -----------
110- text : str
111- The text of transform
112- start_nonterminal : \
113- :class:`~pyformlang.finite_automaton.Symbol` | str, optional
114- The start nonterminal, S by default
101+ text:
102+ The text of transform.
103+ start_nonterminal:
104+ The start nonterminal.
115105
116106 Returns
117- -----------
118- rsa : :class:`~pyformlang.rsa.RecursiveAutomaton`
119- The new recursive automaton built from context-free grammar
107+ -------
108+ The new recursive automaton built from context-free grammar.
120109 """
121110 start_nonterminal = to_symbol (start_nonterminal )
122111 productions : Dict [Hashable , str ] = {}
@@ -148,29 +137,34 @@ def from_ebnf(cls, text: str, start_nonterminal: Hashable = "S") \
148137 return RecursiveAutomaton (start_box , boxes )
149138
150139 def is_equal_to (self , other : "RecursiveAutomaton" ) -> bool :
151- """
152- Check whether two recursive automata are equals by boxes.
140+ """Check whether two recursive automata are equal by boxes.
141+
153142 Not equivalency in terms of formal languages theory, just mapping boxes
154143
155144 Parameters
156145 ----------
157- other : :class:`~pyformlang.rsa.RecursiveAutomaton`
158- The input recursive automaton
146+ other:
147+ The other recursive automaton.
159148
160149 Returns
161- ----------
162- are_equivalent : bool
163- Whether the two recursive automata are equals or not
150+ -------
151+ Whether the two recursive automata are equal or not.
164152 """
165153 return self .boxes == other .boxes
166154
167155 def __eq__ (self , other : Any ) -> bool :
156+ """Checks if the current automaton is equal to the given object."""
168157 if not isinstance (other , RecursiveAutomaton ):
169158 return False
170159 return self .is_equal_to (other )
171160
172161 def to_dot (self ) -> str :
173- """ Create dot representation of recursive automaton """
162+ """Creates dot representation of recursive automaton.
163+
164+ Returns
165+ -------
166+ The dot representation of current recursive automaton.
167+ """
174168 dot_string = 'digraph "" {'
175169 for box in self ._nonterminal_to_box .values ():
176170 dot_string += f'\n { box .to_subgraph_dot ()} '
0 commit comments