1- """ A production or rule of a CFG """
1+ """A production or rule of a CFG. """
22
33from typing import List , Set , Any
44
99
1010
1111class Production :
12- """ A production or rule of a CFG
12+ """A production or rule of a CFG.
1313
1414 Parameters
1515 ----------
16- head : :class:`~pyformlang.cfg.Variable`
17- The head of the production
18- body : iterable of :class:`~pyformlang.cfg.CFGObject`
19- The body of the production
16+ head:
17+ The head of the production.
18+ body:
19+ The body of the production.
2020 """
2121
2222 __slots__ = ["_body" , "_head" , "_hash" ]
@@ -25,6 +25,7 @@ def __init__(self,
2525 head : Variable ,
2626 body : List [CFGObject ],
2727 filtering : bool = True ) -> None :
28+ """Initializes the production."""
2829 if filtering :
2930 self ._body = [x for x in body if not isinstance (x , Epsilon )]
3031 else :
@@ -34,48 +35,48 @@ def __init__(self,
3435
3536 @property
3637 def head (self ) -> Variable :
37- """Gets the head variable"""
38+ """Gets the head variable of the production. """
3839 return self ._head
3940
4041 @property
4142 def body (self ) -> List [CFGObject ]:
42- """Gets the body objects"""
43+ """Gets the body objects of the production. """
4344 return self ._body
4445
4546 @property
4647 def variables (self ) -> Set [Variable ]:
47- """Gets variables used in the production"""
48+ """Gets variables used in the production. """
4849 return {self .head } | {object for object in self .body
4950 if isinstance (object , Variable )}
5051
5152 @property
5253 def terminals (self ) -> Set [Terminal ]:
53- """Gets terminals used in the production"""
54+ """Gets terminals used in the production. """
5455 return {object for object in self .body
5556 if isinstance (object , Terminal ) and object != Epsilon ()}
5657
5758 def __eq__ (self , other : Any ) -> bool :
59+ """Checks if current production is equal to the given object."""
5860 if not isinstance (other , Production ):
5961 return False
6062 return self .head == other .head and self .body == other .body
6163
6264 def __hash__ (self ) -> int :
65+ """Gets the hash of the production."""
6366 if self ._hash is None :
6467 self ._hash = sum (map (hash , self ._body )) + hash (self ._head )
6568 return self ._hash
6669
6770 def __repr__ (self ) -> str :
71+ """Gets the string representation of the production."""
6872 return str (self .head ) + " -> " + " " .join ([str (x ) for x in self .body ])
6973
7074 def is_normal_form (self ) -> bool :
71- """
72- Tells is the production is in Chomsky Normal Form
75+ """Tells if the production is in Chomsky Normal Form.
7376
7477 Returns
7578 -------
76- is_normal_form : bool
77- If the production is in CNF
78-
79+ Whether the production is in CNF.
7980 """
8081 if len (self ._body ) == 2 :
8182 return (isinstance (self ._body [0 ], Variable ) and
0 commit comments