def test_load(self): buf = io.StringIO(""" E -> T E' E' -> + T E' | & T -> F T' T' -> * F T' | & F -> ( E ) | id """) cfg = CFG.load(buf) self.assertEqual('E', cfg.initial_symbol) self.assertDictEqual({ 'E': {"T E'"}, "E'": {"+ T E'", '&'}, 'T': {"F T'"}, "T'": {"* F T'", '&'}, 'F': {'( E )', 'id'} }, cfg.productions) buf = io.StringIO('') with self.assertRaises(ValueError): CFG.load(buf) buf = io.StringIO(""" S -> """) with self.assertRaises(ValueError): CFG.load(buf)
def update_grammar(self): '''Updates grammar with given input and then updates UI. If it fails to generate the grammar, nothing happens.''' try: self.grammar = CFG.load(self.editor.toPlainText().splitlines()) except Exception as e: traceback.print_tb(e.__traceback__) self.window.statusBar().showMessage( 'Failed to generate grammar. Check your syntax.') #self.make_proper_item.setEnabled(False) #self.parse_table_item.setEnabled(False) return self.window.statusBar().showMessage('Done.') self.make_proper_item.setEnabled(True) self.parse_table_item.setEnabled(True) self.run_grammar_btn.setEnabled(False) try: self.update_tables() except RecursionError as e: self.window.statusBar().showMessage( 'Failed to generate grammar tables: Recursion depth overflow.') #self.make_proper_item.setEnabled(False) #self.parse_table_item.setEnabled(False) return