@@ -59,29 +59,26 @@ class FCFG(CFG):
5959 """
6060
6161 def __init__ (self ,
62- variables : AbstractSet [Variable ] = None ,
63- terminals : AbstractSet [Terminal ] = None ,
64- start_symbol : Variable = None ,
65- productions : Iterable [FeatureProduction ] = None ) -> None :
62+ variables : AbstractSet [Hashable ] = None ,
63+ terminals : AbstractSet [Hashable ] = None ,
64+ start_symbol : Hashable = None ,
65+ productions : Iterable [Production ] = None ) -> None :
6666 super ().__init__ (variables , terminals , start_symbol , productions )
6767 self ._productions : Set [FeatureProduction ]
6868
69- def __predictor (self ,
70- state : State ,
71- chart : List [List [State ]],
72- processed : StateProcessed ) -> None :
73- # We have an incomplete state and the next token is a variable
74- # We must ask to process the variable with another rule
75- end_idx = state .positions [1 ]
76- next_var = state .production .body [state .positions [2 ]]
77- for production in self ._productions :
78- if production .head == next_var :
79- new_state = State (production ,
80- (end_idx , end_idx , 0 ),
81- production .features ,
82- ParseTree (production .head ))
83- if processed .add (end_idx , new_state ):
84- chart [end_idx ].append (new_state )
69+ @property
70+ def feature_productions (self ) -> Set [FeatureProduction ]:
71+ """ Gets the feature productions of the grammar """
72+ return self ._productions
73+
74+ def add_production (self , production : Production ) -> None :
75+ """ Adds given production to the grammar """
76+ if not isinstance (production , FeatureProduction ):
77+ production = FeatureProduction (production .head ,
78+ production .body ,
79+ FeatureStructure (),
80+ [FeatureStructure ()])
81+ super ().add_production (production )
8582
8683 def contains (self , word : Iterable [Hashable ]) -> bool :
8784 """ Gives the membership of a word to the grammar
@@ -212,6 +209,23 @@ def _read_line(cls,
212209 production = FeatureProduction (head , body , head_fs , all_body_fs )
213210 productions .add (production )
214211
212+ def __predictor (self ,
213+ state : State ,
214+ chart : List [List [State ]],
215+ processed : StateProcessed ) -> None :
216+ # We have an incomplete state and the next token is a variable
217+ # We must ask to process the variable with another rule
218+ end_idx = state .positions [1 ]
219+ next_var = state .production .body [state .positions [2 ]]
220+ for production in self ._productions :
221+ if production .head == next_var :
222+ new_state = State (production ,
223+ (end_idx , end_idx , 0 ),
224+ production .features ,
225+ ParseTree (production .head ))
226+ if processed .add (end_idx , new_state ):
227+ chart [end_idx ].append (new_state )
228+
215229
216230def _split_text_conditions (head_text : str ) -> Tuple [str , str ]:
217231 if head_text [- 1 ] != "]" :
0 commit comments