def __init__(self): QWidget.__init__(self) self.abaGramatica = Ui_abaGramatica() self.abaGramatica.setupUi(self) self.conectarEventos() self.firsts = None self.follows = None self.parser = None
class AbaGramatica(QWidget): def __init__(self): QWidget.__init__(self) self.abaGramatica = Ui_abaGramatica() self.abaGramatica.setupUi(self) self.conectarEventos() self.firsts = None self.follows = None self.parser = None def conectarEventos(self): self.abaGramatica.buttonGerarParser.clicked.connect(self.gerarParser) self.abaGramatica.buttonAnalisar.clicked.connect(self.analisarSentenca) self.abaGramatica.textEditGramatica.textChanged.connect(self.desabilitarAnalise) def gerarParser(self): gramatica = self.abaGramatica.textEditGramatica.toPlainText() resposta = verificarFormatoGramatica(gramatica) if resposta.correta: prods = resposta.prods prodsNumeradas = resposta.prodsNumeradas numProducoes = resposta.numProducoes Vn = resposta.Vn Vt = resposta.Vt inicial = resposta.inicial gramatica = GLC(Vn=Vn, Vt=Vt, inicial=inicial, prods=prods, prodsNumeradas=prodsNumeradas, numProducoes=numProducoes) resposta = verificarGramaticaLL1(gramatica) if resposta.LL1 is True: firsts = resposta.firsts follows = resposta.follows self.firsts = firsts self.follows = follows tabela = construirTabelaLL1(gramatica, firsts, follows) self.parser = ParserLL1(gramatica=gramatica, tabela=tabela, firsts=firsts, follows=follows) self.abaGramatica.editSentenca.setEnabled(True) self.abaGramatica.buttonAnalisar.setEnabled(True) self.abaGramatica.buttonGerarParser.setText("Parser OK") else: QMessageBox.warning(self, resposta.tipoErro, resposta.mensagemErro) else: QMessageBox.warning(self, resposta.tipoErro, resposta.mensagemErro) def analisarSentenca(self): sentenca = self.abaGramatica.editSentenca.text() sentenca = str(sentenca).strip() sentenca = re.sub(' +', ' ', sentenca) resultado = self.parser.parse(sentenca) if resultado.valida: titulo = "Sentença válida" msg = "<p>" msg += resultado.mensagem msg += '<br />Parse: ' + ", ".join([str(x) for x in resultado.parse]) msg += "</p>" QMessageBox.information(self, titulo, msg) else: titulo = "Sentença inválida" QMessageBox.warning(self, titulo, resultado.mensagem) def desabilitarAnalise(self): self.abaGramatica.editSentenca.setEnabled(False) self.abaGramatica.buttonAnalisar.setEnabled(False) self.abaGramatica.buttonGerarParser.setText("Gerar Parser") self.parser = None