71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
import tkinter as tk
|
|
from tkinter import scrolledtext, messagebox
|
|
from lexical.lexical import Lexical
|
|
from syntax.syntax import Syntax
|
|
from semantic.rule import symbol_table_pool
|
|
from syntax.syntax import LL1Generator
|
|
|
|
def process_code(input_code):
|
|
lexical = Lexical()
|
|
lexical.load_source(input_code)
|
|
lexical_success = lexical.execute()
|
|
output = []
|
|
if lexical_success:
|
|
lexical_result = lexical.get_result()
|
|
output.append('词法分析是否成功:\t' + str(lexical_success))
|
|
output.append('\n词法分析结果:')
|
|
for i in lexical_result:
|
|
output.append(f"{i.type} {i.str} {i.line}")
|
|
output.append('')
|
|
syntax = Syntax()
|
|
syntax.put_source(lexical_result)
|
|
syntax_success = syntax.execute()
|
|
output.append('语法分析和语义分析是否成功\t' + str(syntax_success))
|
|
if syntax_success:
|
|
output.append("\n符号表信息:\t")
|
|
output.append(symbol_table_pool.debug())
|
|
output.append('\n语义分析结果:\t')
|
|
output.append('四元式代码:\t')
|
|
i = -1
|
|
for code in syntax.get_result().root.code:
|
|
i += 1
|
|
output.append(f"{i} \t{code}")
|
|
return '\n'.join(output), ''
|
|
else:
|
|
return '\n'.join(output), '错误原因:\t' + syntax.get_error().info
|
|
else:
|
|
return '', '错误原因:\t' + lexical.get_error().info
|
|
|
|
def on_submit():
|
|
input_code = input_text.get("1.0", tk.END)
|
|
output_text.delete("1.0", tk.END)
|
|
error_label.config(text="")
|
|
try:
|
|
result, error = process_code(input_code)
|
|
if result:
|
|
output_text.insert(tk.END, result)
|
|
if error:
|
|
error_label.config(text=error)
|
|
except Exception as e:
|
|
messagebox.showerror("程序异常", str(e))
|
|
|
|
root = tk.Tk()
|
|
root.title("Hydrogen语言编译器前端演示")
|
|
|
|
# 输入框
|
|
input_text = scrolledtext.ScrolledText(root, width=80, height=20)
|
|
input_text.pack(padx=10, pady=5)
|
|
|
|
# 提交按钮
|
|
submit_btn = tk.Button(root, text="提交", command=on_submit)
|
|
submit_btn.pack(pady=5)
|
|
|
|
# 输出框
|
|
output_text = scrolledtext.ScrolledText(root, width=80, height=20, bg="#ffffff")
|
|
output_text.pack(padx=10, pady=5)
|
|
|
|
# 错误信息框
|
|
error_label = tk.Label(root, text="", fg="red")
|
|
error_label.pack(pady=5)
|
|
|
|
root.mainloop() |