def grammar(self): # 展示语法分析中可以计算的集合和表 syntax = Syntax() syntax.syntax_init('./help/syntax.json') syntax.get_follow() self.grammar_window = QDialog() ui = syntax_grammar.Ui_dialog() init_win(self.grammar_window, ui) set_grammar_tbl(ui, syntax)
def callback(res): lexical = Lexical() lexical.get_dfa('./help/dfa.json') # 读取DFA转换表 lexical_res = lexical.lexical_run(str(res).replace( '\r\n', '\n')) # 得到词法分析的token序列 tokens, nums_attr = [], [] if not lexical_res[0] and not lexical_res[1]: QMessageBox.warning(self, '输入无效', '请输入有效程序文本') return for idx in range(len( lexical_res[0])): # item[1]为种别码,item[3]为行号,item[2]为属性值 item = lexical_res[0][idx] if 'comment' not in item[1]: tokens.append(item[1]) nums_attr.append((item[3], item[2])) syntax = Syntax() syntax.syntax_init('./help/syntax.json') syntax_lst, syntax_err = syntax.syntax_run(tokens, nums_attr) self.syntax_window = QDialog() ui = syntax_res.Ui_Dialog() init_win(self.syntax_window, ui) set_syntax_win(ui, syntax, syntax_lst, lexical_res[1], syntax_err)
class MainWindow(QMainWindow): # noinspection PyArgumentList def __init__(self, editor): super().__init__() self.editor = editor self.setFixedSize(width, height) self.setWindowTitle('编译器@zjr') self.setWindowIcon(QIcon('help/system.ico')) self.setCentralWidget(editor) self.file_path = None # 打开的文件的目录 self.menu_bar = QMenuBar(self) # 菜单栏 self.file_menu = QMenu('文件', self.menu_bar) self.open_file_action = QAction('打开', shortcut=QKeySequence.Open, triggered=self.open_file) self.save_file_action = QAction('保存', shortcut=QKeySequence.Save, triggered=self.save_file) self.save_as_action = QAction('另存为', shortcut='ctrl+shift+s', triggered=self.save_as_file) self.lexical_menu = QMenu('词法分析', self.menu_bar) self.lexical_run_action = QAction('运行', shortcut='ctrl+f1', triggered=self.lexical_run) self.dfa_action = QAction('DFA转换表', triggered=self.dfa) self.nfa_action = QAction('NFA转换表', triggered=self.nfa) self.syntax_menu = QMenu('语法分析', self.menu_bar) self.syntax_run_action = QAction('运行', shortcut='ctrl+f2', triggered=self.syntax_run) self.grammar_action = QAction('语法信息', triggered=self.grammar) self.semantic_menu = QMenu('语义分析', self.menu_bar) self.semantic_run_action = QAction('运行', shortcut='ctrl+f3', triggered=self.semantic_run) self.semantic_info_action = QAction('语义信息', triggered=self.semantic_info) self.about_menu = QMenu('关于', self.menu_bar) self.more_action = QAction('待实现', triggered=self.more) self.init_menu_bar() self.lexical_window = None self.dfa_window = None self.nfa_window = None self.syntax_window = None self.grammar_window = None self.semantic_win = None # 语义分析结果窗口 self.semantic_info_win = None # 语义信息窗口 self.syntax = None def init_menu_bar(self): self.menu_bar.setGeometry(0, 0, width, bar_height) for menu_bar in [ self.file_menu, self.lexical_menu, self.syntax_menu, self.semantic_menu, self.about_menu ]: self.menu_bar.addMenu(menu_bar) self.file_menu.addAction(self.open_file_action) self.file_menu.addAction(self.save_file_action) self.file_menu.addAction(self.save_as_action) self.lexical_menu.addAction(self.lexical_run_action) self.lexical_menu.addAction(self.dfa_action) self.lexical_menu.addAction(self.nfa_action) self.syntax_menu.addAction(self.syntax_run_action) self.syntax_menu.addAction(self.grammar_action) self.semantic_menu.addAction(self.semantic_run_action) self.semantic_menu.addAction(self.semantic_info_action) self.about_menu.addAction(self.more_action) def open_file(self): # 打开文件 self.file_path = QFileDialog.getOpenFileName(self, '', os.getcwd() + '/input', 'C(*.c);;Txt(*.txt)')[0] if self.file_path: with open(self.file_path, 'r', encoding='utf-8') as f: self.editor.set_text(f.read()) def save_file(self): # 保存文件 def callback(res): if self.file_path: with open(self.file_path, 'w', encoding='utf-8', newline='') as f: f.write(res) else: self.save_as_file() self.editor.get_text(callback) def save_as_file(self): # 文件另存为 def callback(res): if self.file_path: with open(self.file_path, 'w', encoding='utf-8', newline='') as f: f.write(res) self.file_path = QFileDialog.getSaveFileName(self, '', os.getcwd() + '/input', 'C(*.c);;Txt(*.txt)')[0] self.editor.get_text(callback) def lexical_run(self): # 运行词法分析 def callback(res): lexical = Lexical() lexical.get_dfa('./help/dfa.json') res = lexical.lexical_run(str(res).replace( '\r\n', '\n')) # 词法分析的token序列,要将window换行符'\r\n'转换 self.lexical_window = LexicalWindow(res) self.lexical_window.show() self.editor.get_text(callback) def dfa(self): # dfa转换表 lexical = Lexical() lexical.get_dfa('./help/dfa.json') self.dfa_window = DFAWindow(lexical.get_dfa_table()) self.dfa_window.show() def nfa(self): # nfa转换表 path = QFileDialog.getOpenFileName(self, '', os.getcwd() + '/help/nfa.json', 'Json(*.json)')[0] if path: lexical = Lexical() lexical.get_nfa(path) self.nfa_window = NFAWindow(lexical.nfa, lexical.nfa2dfa()) self.nfa_window.show() def syntax_run(self): # 运行语法分析 def callback(res): lexical = Lexical() lexical.get_dfa('./help/dfa.json') # 读取DFA转换表 lexical_res = lexical.lexical_run(str(res).replace( '\r\n', '\n')) # 得到词法分析的token序列 tokens, nums_attr = [], [] if not lexical_res[0] and not lexical_res[1]: QMessageBox.warning(self, '输入无效', '请输入有效程序文本') return for idx in range(len( lexical_res[0])): # item[1]为种别码,item[3]为行号,item[2]为属性值 item = lexical_res[0][idx] if 'comment' not in item[1]: tokens.append(item[1]) nums_attr.append((item[3], item[2])) syntax = Syntax() syntax.syntax_init('./help/syntax.json') syntax_lst, syntax_err = syntax.syntax_run(tokens, nums_attr) self.syntax_window = QDialog() ui = syntax_res.Ui_Dialog() init_win(self.syntax_window, ui) set_syntax_win(ui, syntax, syntax_lst, lexical_res[1], syntax_err) self.editor.get_text(callback) def grammar(self): # 展示语法分析中可以计算的集合和表 syntax = Syntax() syntax.syntax_init('./help/syntax.json') syntax.get_follow() self.grammar_window = QDialog() ui = syntax_grammar.Ui_dialog() init_win(self.grammar_window, ui) set_grammar_tbl(ui, syntax) def semantic_run(self): # 语义分析 def callback(res): lexical = Lexical() lexical.get_dfa('./help/dfa.json') # 读取DFA转换表 lexical_res = lexical.lexical_run(str(res).replace( '\r\n', '\n')) # 得到词法分析的token序列 tokens, nums_attr = [], [] if not lexical_res[0] and not lexical_res[1]: QMessageBox.warning(self, '输入无效', '请输入有效程序文本') return for idx in range(len( lexical_res[0])): # item[1]为种别码,item[3]为行号,item[2]为属性值 item = lexical_res[0][idx] if 'comment' not in item[1]: tokens.append(item[1]) nums_attr.append((item[3], item[2])) if not self.syntax: self.syntax = Syntax() self.syntax.syntax_init('./help/semantic.json') semantic = Semantic() res_lst, res_err = semantic.semantic_run(tokens, nums_attr, self.syntax) self.semantic_win = QDialog() ui = semantic_res.Ui_Dialog() init_win(self.semantic_win, ui) set_semantic_win(ui, semantic) self.editor.get_text(callback) def semantic_info(self): # 语义相关信息 if not self.syntax: self.syntax = Syntax() self.syntax.syntax_init('./help/semantic.json') self.semantic_info_win = QDialog() ui = semantic_info.Ui_Dialog() init_win(self.semantic_info_win, ui) _set_grammar_tbl(ui.grammar_tbl, self.syntax) def more(self): # 待实现 pass