@@ -62,26 +62,23 @@ def __init__(self,
62
62
variables : AbstractSet [Hashable ] = None ,
63
63
terminals : AbstractSet [Hashable ] = None ,
64
64
start_symbol : Hashable = None ,
65
- productions : Iterable [FeatureProduction ] = None ) -> None :
65
+ productions : Iterable [Production ] = None ) -> None :
66
66
super ().__init__ (variables , terminals , start_symbol , productions )
67
67
self ._productions : Set [FeatureProduction ]
68
68
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 )
85
82
86
83
def contains (self , word : Iterable [Hashable ]) -> bool :
87
84
""" Gives the membership of a word to the grammar
@@ -212,6 +209,23 @@ def _read_line(cls,
212
209
production = FeatureProduction (head , body , head_fs , all_body_fs )
213
210
productions .add (production )
214
211
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
+
215
229
216
230
def _split_text_conditions (head_text : str ) -> Tuple [str , str ]:
217
231
if head_text [- 1 ] != "]" :
0 commit comments