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()
def validate(self): with open(self.upload_folder + self.filename, "r") as file: code = file.read() cr = Lexical(code) result = cr.run() return jsonify({ 'code': code, 'lexical_table': result, 'syntatic_result': [] })
def validate(): assert request.path == '/validate' assert request.method == 'POST' cr = Lexical(request.form['code']) lexical_result = cr.run() s = Syntatic(request.form['code'], cr.getTokens()) syntatic_result = s.run() return jsonify({ 'code': request.form['code'], 'lexical_table': lexical_result, 'syntatic_result': syntatic_result })
def __init__(self): QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.editor = QCodeEditor(self.ui.centralwidget) self.ui.editor.setGeometry(QtCore.QRect(10, 20, 661, 751)) text = '''open a file \nor type something...''' self.ui.editor.setPlainText(text) self.ui.editor.setObjectName("editor") self.ui.open_btn.triggered.connect(self.open_file) self.ui.actionread_dfa.triggered.connect(self.read_dfa) self.ui.actionclear.triggered.connect(self.clear_dfa) self.lexical_window = Lexical() self.dfa_window = None self.grammer_show = None self.grammar_analyzer = GrammarAnalyze() self.semantic_analyzer_page = Semantic() self.semantic_analyzer = None self.SDTShow = None self.setWindowTitle("Analyzer") self.table = None
def dfa(self): # dfa转换表 lexical = Lexical() lexical.get_dfa('./help/dfa.json') self.dfa_window = DFAWindow(lexical.get_dfa_table()) self.dfa_window.show()
from lexical import Lexical from semantic import Pro from syntax import Parser from vm import VM import os if __name__ == '__main__': # 新建词法分析器 lexical = Lexical() # 载入源代码 lexical.load_source(open('fib.txt', encoding="utf-8").read()) # 执行词法分析 lexical_success = lexical.execute() # 将结果保存在tokens文件中 with open('lexical_result.txt', 'w+') as f: lexical_result = lexical.get_result() for i in lexical_result: f.write(i.type + '\t' + i.str + '\t' + str(i.line) + '\n') # # 打印结果 print('词法分析是否成功:\t', lexical_success) if lexical_success: # lexical_result = lexical.get_result() print() print('词法分析结果:') for i in lexical_result: print(i.type, i.str, i.line) print() else: