From b715651483bc60fabbce9f32f0a445abd88fd295 Mon Sep 17 00:00:00 2001 From: Kia <kia@special-circumstanc.es> Date: Sun, 16 Aug 2020 17:13:10 -0600 Subject: [PATCH] unsure why we made the nonterminals and terminals live in the same enum but that was a bad idea. --- cfg_utils.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/cfg_utils.py b/cfg_utils.py index 1d3c1f3..f9e9940 100644 --- a/cfg_utils.py +++ b/cfg_utils.py @@ -53,22 +53,25 @@ CFG = collections.namedtuple("CFG", "nonterminals terminals rules start") # FACTOR -> OPENPAREN EXPRESSION CLOSEPAREN # FACTOR -> INTEGER -symbols = Enum("Symbols", "EXPRESSION TERM FACTOR INTEGER ADDOP MULTOP OPENPAREN CLOSEPAREN") -start = symbols.EXPRESSION +#symbols = Enum("Symbols", "EXPRESSION TERM FACTOR INTEGER ADDOP MULTOP OPENPAREN CLOSEPAREN") +nonterminals = Enum("Nonterminals", "EXPRESSION TERM FACTOR") +terminals = Enum("Terminals", " INTEGER ADDOP MULTOP OPENPAREN CLOSEPAREN") + +start = nonterminals.EXPRESSION # The rules are a list of tuples, each of which represents a rule, as follows: # (Nonterminal symbol, [ordered list of symbols (terminal or nonterminal) that are reduced to aforementioned nonterminal]) rules = [ - (symbols.EXPRESSION, [symbols.EXPRESSION, symbols.ADDOP, symbols.TERM ]), - (symbols.EXPRESSION, [symbols.TERM ]), - (symbols.TERM, [symbols.TERM, symbols.MULTOP, symbols.FACTOR ]), - (symbols.TERM, [symbols.FACTOR ]), - (symbols.FACTOR, [symbols.OPENPAREN, symbols.EXPRESSION, symbols.CLOSEPAREN]), - (symbols.FACTOR, [symbols.INTEGER ]) + (nonterminals.EXPRESSION, [nonterminals.EXPRESSION, terminals.ADDOP, nonterminals.TERM ]), + (nonterminals.EXPRESSION, [nonterminals.TERM ]), + (nonterminals.TERM, [nonterminals.TERM, terminals.MULTOP, nonterminals.FACTOR ]), + (nonterminals.TERM, [nonterminals.FACTOR ]), + (nonterminals.FACTOR, [terminals.OPENPAREN, nonterminals.EXPRESSION, terminals.CLOSEPAREN]), + (nonterminals.FACTOR, [terminals.INTEGER ]) ] #print(rules) @@ -198,7 +201,7 @@ def the_tree_farm(): # -test_tree = TreeNode(symbols.EXPRESSION, []) +test_tree = TreeNode(nonterminals.EXPRESSION, []) #walk_the_tree(test_tree) @@ -353,9 +356,10 @@ if __name__ == '__main__': class CFGBoltzmann: - def __init__(self, rules, symbols): + def __init__(self, rules, nonterminals, terminals): self.unitary_rules = rules - self.symbols = symbols + self.nonterminals = nonterminals + self.terminals = terminals def preprocessor(self): unitary_rules = self.unitary_rules @@ -404,10 +408,18 @@ class CFGBoltzmann: 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) + + # first, handle the ultimate degenerate case: + if (exact_length_total == 0): + return [] + + + # The case analysis hinges on what is at X_ijk. + return 0 -z = CFGBoltzmann(rules, symbols) +z = CFGBoltzmann(rules, nonterminals, terminals) rulepack_cooked = z.preprocessor() -- GitLab