if self.accept_test(): raise Accept() raise Reject("Unexpected character") def on_reject(self, exc): super().on_reject(exc) self.error_msg = exc.args[0] if __name__ == '__main__': from io import StringIO from pycog.utility.diagram import diagram with open("ps_and_qs.gv", "w") as gv_file: diagram(PsAndQs(StringIO("pppqqq")), gv_file) def report(fsm): accepted = fsm.run() if accepted: print('"{inp}" accepted.'.format(inp = fsm.stream.getvalue())) else: ptr = ' '*fsm.pos + '^' inp = fsm.stream.getvalue() msg = 'Rejected: {msg}\n{inp}\n{ptr}'.format(msg=fsm.error_msg, inp=inp, ptr=ptr) print(msg) print('-'*60) print("NFA to accept the language (p^i)(q^j)") print('='*60)
def on_no_transition(self, s_name): if self.accept_test(): raise Accept() self.error_msg = "No transition available--unknown cause." if not self.stack_empty: self.unmatched_open() super().on_no_transition(s_name) if __name__ == '__main__': from io import StringIO from pycog.utility.diagram import diagram with open("check_parens.gv", "w") as gv_file: diagram(ParenChecker(StringIO("()")), gv_file) def report(fsm): try: accepted = fsm.run() if accepted: print('"{inp}" accepted.'.format(inp = fsm.stream.getvalue())) else: inp = fsm.stream.getvalue() ptr = "" if fsm.open_symbol_pos != None: ptr = ' '*fsm.open_symbol_pos + '^' if fsm.close_symbol_pos != None: ptr += ' '*(fsm.close_symbol_pos - fsm.open_symbol_pos - 1) + '^' elif fsm.close_symbol_pos != None:
sys.stdout.write('\n') def on_backtrack(self, occ): """Undo the placing of a queen in response to backtracking.""" super().on_backtrack(occ) # Uncomment these two lines to display the state of the board each time # backtracking occurs. # print() # self.draw() if occ.state not in ['init', 'final']: self.queens.remove(self.current_state) def on_exhausted(self): """ Handle the case where no solution can be found. This should never happen. """ raise Exception("No solution found!") if __name__ == "__main__": solver = EightQueens() solver.run() solver.draw() with open("eight_queens.gv", "w") as gv_file: diagram(solver, gv_file)
raise Accept() raise Reject("Unexpected character") def on_reject(self, exc): super().on_reject(exc) self.error_msg = exc.args[0] if __name__ == '__main__': from io import StringIO from pycog.utility.diagram import diagram with open("ps_and_qs.gv", "w") as gv_file: diagram(PsAndQs(StringIO("pppqqq")), gv_file) def report(fsm): accepted = fsm.run() if accepted: print('"{inp}" accepted.'.format(inp=fsm.stream.getvalue())) else: ptr = ' ' * fsm.pos + '^' inp = fsm.stream.getvalue() msg = 'Rejected: {msg}\n{inp}\n{ptr}'.format(msg=fsm.error_msg, inp=inp, ptr=ptr) print(msg) print('-' * 60) print("NFA to accept the language (p^i)(q^j)")