diff --git a/cfg_utils.py b/cfg_utils.py index f9e9940f98b2ac155ad8e25321a888a1f6c6ee5c..09c073d59e38c75b790c1f10c173ada8636f0950 100644 --- a/cfg_utils.py +++ b/cfg_utils.py @@ -57,7 +57,10 @@ CFG = collections.namedtuple("CFG", "nonterminals terminals rules start") nonterminals = Enum("Nonterminals", "EXPRESSION TERM FACTOR") -terminals = Enum("Terminals", " INTEGER ADDOP MULTOP OPENPAREN CLOSEPAREN") +terminals = Enum("Terminals", "INTEGER ADDOP MULTOP OPENPAREN CLOSEPAREN") + +list_of_nonterminals = [e for e in nonterminals] +list_of_terminals = [e for e in terminals] start = nonterminals.EXPRESSION @@ -358,8 +361,10 @@ if __name__ == '__main__': class CFGBoltzmann: def __init__(self, rules, nonterminals, terminals): self.unitary_rules = rules + print("nonterminal list is", nonterminals) self.nonterminals = nonterminals self.terminals = terminals + print("terminal list is", terminals) def preprocessor(self): unitary_rules = self.unitary_rules @@ -386,11 +391,9 @@ class CFGBoltzmann: def Fzero(self, nonterminal_index, exact_length_total): - processed_rulepack = self.processed_rulepack # First find the nonterminal in the rulepack - possible_RHSes = processed_rulepack[nonterminal_index][1] - + possible_RHSes = self.processed_rulepack[nonterminal_index][1] print(possible_RHSes) assert(possible_RHSes != []) @@ -398,7 +401,7 @@ class CFGBoltzmann: for rhs_index in range(len(possible_RHSes)): print(rhs_index) - sum += self.Fprim(nonterminal_index, rhs_index, exact_length_total) # we index arrays starting at zero. like everyone else. + sum += self.Fprim(nonterminal_index, rhs_index, 0, exact_length_total) # we index arrays starting at zero. like everyone else. return sum @@ -406,20 +409,24 @@ class CFGBoltzmann: # Fprim is where the complicated case analysis lives. - def Fprim(self, nonterminal_index, how_far_into_the_RHS, exact_length_total): - print("arguments are", nonterminal_index, how_far_into_the_RHS, exact_length_total) + def Fprim(self, nonterminal_index, which_RHS, how_far_into_the_RHS, exact_length_total): + print("arguments are", nonterminal_index, which_RHS, how_far_into_the_RHS, exact_length_total) # first, handle the ultimate degenerate case: if (exact_length_total == 0): return [] - # The case analysis hinges on what is at X_ijk. + RHS_in_question = self.processed_rulepack[nonterminal_index][1][which_RHS] + print(RHS_in_question) + + + return 0 -z = CFGBoltzmann(rules, nonterminals, terminals) +z = CFGBoltzmann(rules, list_of_nonterminals, list_of_terminals) rulepack_cooked = z.preprocessor()