def test_oldStartSymbolShouldStaySame(self): g = Grammar(nonterminals=[A], start_symbol=A) self.assertIsNotNone(g.start) with self.assertRaises(NonterminalDoesNotExistsException): g.start = B self.assertIsNotNone(g.start) self.assertIs(g.start, A)
def test_shouldNotSetStartSymbol(self): g = Grammar(nonterminals=[A, B]) self.assertIsNone(g.start) with self.assertRaises(NotNonterminalException): g.start = 'asdf' self.assertIsNone(g.start) self.assertFalse(g.start == 'asdf')
def test_oldStartSymbolShouldStaySame(self): g = Grammar(nonterminals=[A, B], start_symbol=A) self.assertIsNotNone(g.start) self.assertIs(g.start, A) with self.assertRaises(NotNonterminalException): g.start = 'asdf' self.assertIsNotNone(g.start) self.assertIs(g.start, A)
def test_removeElementNotThere(self): gr = Grammar() gr.nonterminals.add(A, B, C) self.assertEqual(gr.nonterminals.size(), 3) self.assertEqual(len(gr.nonterminals), 3) with self.assertRaises(KeyError): gr.nonterminals.remove(D)
def test_removeEmptyGrammar(self): gr = Grammar() self.assertEqual(gr.nonterminals.size(), 0) self.assertEqual(len(gr.nonterminals), 0) gr.nonterminals.clear() self.assertEqual(gr.nonterminals.size(), 0) self.assertEqual(len(gr.nonterminals), 0)
def test_removeEmptyGrammar(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N]) self.assertEqual(gr.rules.size(), 0) self.assertEqual(len(gr.rules), 0) gr.nonterminals.clear() self.assertEqual(gr.rules.size(), 0) self.assertEqual(len(gr.rules), 0)
def test_removeOneOfTheElementNotThere(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N]) gr.rules.add(A, B, C) self.assertEqual(gr.rules.size(), 3) self.assertEqual(len(gr.rules), 3) with self.assertRaises(KeyError): gr.rules.remove(B, D)
def test_equalGetTermAndTermMethods(self): gr = Grammar() ins = TempClass() gr.terminals.add(ins) self.assertEqual(gr.terminals.size(), 1) self.assertEqual(len(gr.terminals), 1) self.assertIn(ins, gr.terminals)
def test_addSameTwiceInParameters(self): gr = Grammar() gr.nonterminals.add(A, B, A, C) self.assertEqual(gr.nonterminals.size(), 3) self.assertEqual(len(gr.nonterminals), 3) self.assertIn(A, gr.nonterminals) self.assertIn(B, gr.nonterminals) self.assertIn(C, gr.nonterminals)
def test_addThreeAsArray(self): gr = Grammar() gr.nonterminals.add(*[A, B, C]) self.assertEqual(gr.nonterminals.size(), 3) self.assertEqual(len(gr.nonterminals), 3) self.assertIn(A, gr.nonterminals) self.assertIn(B, gr.nonterminals) self.assertIn(C, gr.nonterminals)
def test_addThreeAsArray(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N]) gr.rules.add(*[A, B, C]) self.assertEqual(gr.rules.size(), 3) self.assertEqual(len(gr.rules), 3) self.assertIn(A, gr.rules) self.assertIn(B, gr.rules) self.assertIn(C, gr.rules)
def test_addSameTwiceInParameters(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N]) gr.rules.add(A, B, A, C) self.assertEqual(gr.rules.size(), 3) self.assertEqual(len(gr.rules), 3) self.assertIn(A, gr.rules) self.assertIn(B, gr.rules) self.assertIn(C, gr.rules)
def test_addInArray(self): gr = Grammar() gr.terminals.add(0, 'asdf', TempClass) self.assertEqual(gr.terminals.size(), 3) self.assertEqual(len(gr.terminals), 3) self.assertIn(0, gr.terminals) self.assertIn('asdf', gr.terminals) self.assertIn(TempClass, gr.terminals)
def remove_rules_with_epsilon(grammar: Grammar, transform_grammar=False) -> Grammar: """ Remove epsilon rules. :param grammar: Grammar where rules remove :param transform_grammar: True if transformation should be performed in place, false otherwise. False by default. :return: Grammar without epsilon rules. """ # Copy if required if transform_grammar is False: grammar = copy(grammar) # Find nonterminals rewritable to epsilon rewritable = find_nonterminals_rewritable_to_epsilon(grammar) # Create list from rules rules = list(grammar.rules()) index = 0 # Iterate thought rules while index < len(rules): rule = rules[index] index += 1 right = rule.right if right == [EPSILON]: if not grammar.start_isSet( ) or rule.fromSymbol != grammar.start_get(): grammar.remove_rule(rule) # Continue IS executed, but due optimalization line is marked as missed. continue #pragma: no cover for rule_index in range(len(right)): symbol = right[rule_index] if symbol in rewritable: new_rule = _create_rule(rule, rule_index, rewritable) rules.append(new_rule) grammar.add_rule(new_rule) return grammar
def remove_nongenerating_nonterminals(grammar: Grammar, transform_grammar=False) -> Grammar: """ Remove nongenerating symbols from the grammar :param grammar: Grammar where to remove nongenerating symbols :param transform_grammar: True if transformation should be performed in place, false otherwise. False by default. :return: Grammar without nongenerating symbols """ # Copy if required if transform_grammar is False: grammar = copy(grammar) # Create working sets generates = set(item.s for item in grammar.terms()) generates.add(EPSILON) rules = set(rule for rule in grammar.rules()) while True: # Create set of next iteration additional = generates.copy() processedRules = [] # Iterate over unprocessed rules for rule in rules: rightPart = rule.right allIn = True # Check if all symbols on the right part of rule are in generates set for symbol in rightPart: if symbol not in generates: allIn = False break # Symbol is missing so rule is not process if not allIn: continue # Rule is process - remove it from processing rules and make symbol as generating additional.add(rule.fromSymbol) processedRules.append(rule) # End of rules iterations # Remove process rules in current iteration for item in processedRules: rules.remove(item) # If current and previous iterations are same, than end iterations if additional == generates: break # Swap sets from previous and current iterations generates = additional # Remove nonterms that are not generating for nonterm in set(grammar.nonterms()).difference(generates): grammar.remove_nonterm(nonterm) return grammar
def test_unionUpdateOperator(self): gr = Grammar(nonterminals=[B, C]) # Because Python is PIECE OF SHIT that can't handle F*****G OPERATOR on property # if there isn't F*****G SETTER. Storing the property in variable na CALLING F*****G # OPERATOR on that variable works and MODIFY F*****G PROPERTY ITSELF. # Burn in F*****G HELL!!!! nonterms = gr.nonterminals with self.assertRaises(NotNonterminalException): nonterms |= {A, B, 0}
def test_correctAddOne(self): gr = Grammar() self.assertEqual(gr.terminals.size(), 0) self.assertEqual(len(gr.terminals), 0) self.assertNotIn(0, gr.terminals) gr.terminals.add(0) self.assertEqual(gr.terminals.size(), 1) self.assertEqual(len(gr.terminals), 1) self.assertIn(0, gr.terminals)
def test_symDifferenceUpdateOperatorWithNotInside2(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N], rules=[B, C]) # Because Python is PIECE OF SHIT that can't handle F*****G OPERATOR on property # if there isn't F*****G SETTER. Storing the property in variable na CALLING F*****G # OPERATOR on that variable works and MODIFY F*****G PROPERTY ITSELF. # Burn in F*****G HELL!!!! rules = gr.rules with self.assertRaises(NonterminalDoesNotExistsException): rules ^= {A, B, NotInsideRule2}
def test_removeElementNotThere(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N]) gr.rules.add(A, B, C) self.assertEqual(gr.rules.size(), 3) self.assertEqual(len(gr.rules), 3) gr.rules.discard(D) self.assertEqual(gr.rules.size(), 3) self.assertEqual(len(gr.rules), 3)
def test_correctAddOne(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N]) self.assertEqual(gr.rules.size(), 0) self.assertEqual(len(gr.rules), 0) self.assertNotIn(A, gr.rules) gr.rules.add(A) self.assertEqual(gr.rules.size(), 1) self.assertEqual(len(gr.rules), 1) self.assertIn(A, gr.rules)
def test_intersectionUpdateOperatorWithInvalidRule(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N], rules=[A, B, C]) # Because Python is PIECE OF SHIT that can't handle F*****G OPERATOR on property # if there isn't F*****G SETTER. Storing the property in variable na CALLING F*****G # OPERATOR on that variable works and MODIFY F*****G PROPERTY ITSELF. # Burn in F*****G HELL!!!! rules = gr.rules rules &= {B, C, 0, InvalidRule} self.assertEqual(rules, {B, C}) self.assertEqual(gr.rules, {B, C})
def test_differenceUpdateOperator(self): gr = Grammar(nonterminals=[A, B, C]) # Because Python is PIECE OF SHIT that can't handle F*****G OPERATOR on property # if there isn't F*****G SETTER. Storing the property in variable na CALLING F*****G # OPERATOR on that variable works and MODIFY F*****G PROPERTY ITSELF. # Burn in F*****G HELL!!!! nonterms = gr.nonterminals nonterms -= {B, C} self.assertEqual(nonterms, {A}) self.assertEqual(gr.nonterminals, {A})
def test_add_remove_add_one(self): gr = Grammar() self.assertEqual(len(gr.terminals), 0) self.assertNotIn(0, gr.terminals) gr.terminals.add(0) self.assertEqual(len(gr.terminals), 1) self.assertIn(0, gr.terminals) gr.terminals.remove(0) self.assertEqual(len(gr.terminals), 0) self.assertNotIn(0, gr.terminals)
def test_unionUpdateOperator(self): gr = Grammar(terminals=[0, 'asdf']) # Because Python is PIECE OF SHIT that can't handle F*****G OPERATOR on property # if there isn't F*****G SETTER. Storing the property in variable na CALLING F*****G # OPERATOR on that variable works and MODIFY F*****G PROPERTY ITSELF. # Burn in F*****G HELL!!!! terms = gr.terminals terms |= {'asdf', TempClass} self.assertEqual(terms, {0, 'asdf', TempClass}) self.assertEqual(gr.terminals, {0, 'asdf', TempClass})
def test_addThreeOneDelete(self): gr = Grammar(nonterminals=(A, B, C)) self.assertIn(A, gr.nonterminals) self.assertIn(B, gr.nonterminals) self.assertIn(C, gr.nonterminals) self.assertNotIn(D, gr.nonterminals) gr.nonterminals.remove(B) self.assertIn(A, gr.nonterminals) self.assertNotIn(B, gr.nonterminals) self.assertIn(C, gr.nonterminals) self.assertNotIn(D, gr.nonterminals)
def test_removeSameElementMoreTimesAsParameters(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N]) gr.rules.add(*[A, B, C]) self.assertEqual(gr.rules.size(), 3) self.assertEqual(len(gr.rules), 3) gr.rules.remove(B, B) self.assertEqual(gr.rules.size(), 2) self.assertEqual(len(gr.rules), 2) self.assertIn(A, gr.rules) self.assertNotIn(B, gr.rules) self.assertIn(C, gr.rules)
def test_removeAll(self): gr = Grammar() gr.nonterminals.add(A, B, C) self.assertEqual(gr.nonterminals.size(), 3) self.assertEqual(len(gr.nonterminals), 3) gr.nonterminals.clear() self.assertEqual(gr.nonterminals.size(), 0) self.assertEqual(len(gr.nonterminals), 0) self.assertNotIn(A, gr.nonterminals) self.assertNotIn(B, gr.nonterminals) self.assertNotIn(C, gr.nonterminals)
def test_removeSameElementMoreTimesInArray(self): gr = Grammar() gr.terminals.add(*[0, 'asdf', TempClass]) self.assertEqual(gr.terminals.size(), 3) self.assertEqual(len(gr.terminals), 3) gr.terminals.remove(*[0, 0]) self.assertEqual(gr.terminals.size(), 2) self.assertEqual(len(gr.terminals), 2) self.assertNotIn(0, gr.terminals) self.assertIn('asdf', gr.terminals) self.assertIn(TempClass, gr.terminals)
def test_removeSameElementMoreTimesInArray(self): gr = Grammar() gr.nonterminals.add(*[A, B, C]) self.assertEqual(gr.nonterminals.size(), 3) self.assertEqual(len(gr.nonterminals), 3) gr.nonterminals.remove(*[B, B]) self.assertEqual(gr.nonterminals.size(), 2) self.assertEqual(len(gr.nonterminals), 2) self.assertIn(A, gr.nonterminals) self.assertNotIn(B, gr.nonterminals) self.assertIn(C, gr.nonterminals)
def test_addThreeOneDelete(self): gr = Grammar(terminals=[0, 1, 2], nonterminals=[N], rules=(A, B, C)) self.assertIn(A, gr.rules) self.assertIn(B, gr.rules) self.assertIn(C, gr.rules) self.assertNotIn(D, gr.rules) gr.rules.remove(B) self.assertIn(A, gr.rules) self.assertNotIn(B, gr.rules) self.assertIn(C, gr.rules) self.assertNotIn(D, gr.rules)