77
88# pylint: disable=cyclic-import
99from pyformlang import pda
10- from pyformlang .finite_automaton import FiniteAutomaton
10+ from pyformlang .finite_automaton import DeterministicFiniteAutomaton
1111# pylint: disable=cyclic-import
1212from pyformlang .pda import cfg_variable_converter as cvc
13- from pyformlang import regular_expression
1413from .cfg_object import CFGObject
1514# pylint: disable=cyclic-import
1615from .cyk_table import CYKTable , DerivationDoesNotExist
@@ -788,7 +787,7 @@ def to_pda(self) -> "pda.PDA":
788787 state , [])
789788 return new_pda
790789
791- def intersection (self , other : Any ) -> "CFG" :
790+ def intersection (self , other : DeterministicFiniteAutomaton ) -> "CFG" :
792791 """ Gives the intersection of the current CFG with an other object
793792
794793 Equivalent to:
@@ -810,13 +809,6 @@ def intersection(self, other: Any) -> "CFG":
810809 When trying to intersect with something else than a regex or a
811810 finite automaton
812811 """
813- if isinstance (other , regular_expression .Regex ):
814- other = other .to_epsilon_nfa ().to_deterministic ()
815- elif isinstance (other , FiniteAutomaton ):
816- if not other .is_deterministic ():
817- other = other .to_deterministic ()
818- else :
819- raise NotImplementedError
820812 if other .is_empty ():
821813 return CFG ()
822814 generate_empty = self .contains ([]) and other .accepts ([])
@@ -845,10 +837,12 @@ def intersection(self, other: Any) -> "CFG":
845837 return res_cfg
846838
847839 @staticmethod
848- def _intersection_starting_rules (cfg , other , cv_converter ):
840+ def _intersection_starting_rules (cfg : "CFG" ,
841+ other : DeterministicFiniteAutomaton ,
842+ cv_converter ):
849843 start = Variable ("Start" )
850844 productions_temp = []
851- start_other = list ( other .start_states )[ 0 ] # it is deterministic
845+ start_other = other .start_state
852846 for final_state in other .final_states :
853847 new_body = [
854848 cv_converter .to_cfg_combined_variable (
@@ -860,15 +854,17 @@ def _intersection_starting_rules(cfg, other, cv_converter):
860854 return productions_temp
861855
862856 @staticmethod
863- def _intersection_when_terminal (other_fst , production ,
857+ def _intersection_when_terminal (other : DeterministicFiniteAutomaton ,
858+ production ,
864859 cv_converter , states ):
865860 productions_temp = []
866861 for state_p in states :
867- next_states = other_fst (state_p , production .body [0 ].value )
868- if next_states :
862+ next_state = other .get_next_state (
863+ state_p , production .body [0 ].value )
864+ if next_state :
869865 new_head = \
870866 cv_converter .to_cfg_combined_variable (
871- state_p , production .head , next_states [ 0 ] )
867+ state_p , production .head , next_state )
872868 productions_temp .append (
873869 Production (new_head ,
874870 [production .body [0 ]],
@@ -904,7 +900,7 @@ def _get_all_bodies(production, state_p, state_r, states, cv_converter):
904900 state_r )]
905901 for state_q in states ]
906902
907- def __and__ (self , other ) :
903+ def __and__ (self , other : DeterministicFiniteAutomaton ) -> "CFG" :
908904 """ Gives the intersection of the current CFG with an other object
909905
910906 Parameters
0 commit comments