This project involves building a compiler using Lex and Yacc. The instructions below will guide you on how to set up, run, and use a graphical user interface (GUI) with the compiler.
- Download and install Flex and Bison from WinFlexBison or you can download the .exe file from
Program/Program.zip
file.
- Open the Start menu and search for "Environment Variables."
- Click on Environment Variables.
- Edit the
Path
variable by adding:- The path to
flex
. - The path to
gcc
.
- The path to
- Create a new folder.
- Add the following files to the folder:
LexFile.l
YaccFile.y
Install the following extensions in Visual Studio Code:
Run the following command in the terminal:
flex LexFile.l; gcc lex.yy.c -o Lex; ./Lex.exe
Run the following commands in the terminal:
flex LexFile.l; yacc -d YaccFile.y; gcc lex.yy.c -o lexer; gcc YaccFile.tab.c lex.yy.c -o program
- Open the terminal in VS Code.
- Execute:
flex LexFile.l; yacc -d YaccFile.y; gcc lex.yy.c -o lexer; gcc YaccFile.tab.c lex.yy.c -o program
- Run the compiled program:
./program.exe
This section explains how to create and use a GUI with the compiler, leveraging the customtkinter
Python library.
Download and install Python from python.org.
Install customtkinter
(or tkinter
) by running:
pip install customtkinter
- Create a new file named
GUI.py
. - Import the required libraries:
import tkinter as tk import customtkinter as ctk import subprocess import os
Define a function use_GUI()
to run the program and display the output:
def use_GUI():
user_input = text_input.get("1.0", tk.END).strip()
process = subprocess.Popen(["program.exe"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate(input=user_input.encode())
result_output.delete("1.0", tk.END)
result_output.insert(tk.END, stdout.decode())
- Ensure the path to
program.exe
is correct. - This function captures user input, executes the program, and displays the output in the GUI.
Design the GUI:
root = ctk.CTk()
root.title("Compiler")
root.geometry("1400x800")
root.grid_rowconfigure(0, weight=1, minsize=5)
root.grid_rowconfigure(1, weight=0, minsize=5)
root.grid_columnconfigure(0, weight=1, minsize=5)
root.grid_columnconfigure(1, weight=0, minsize=5)
root.grid_columnconfigure(2, weight=1, minsize=5)
text_input = ctk.CTkTextbox(root, height=10, width=30, font=("Consolas", 19, "bold"), wrap=tk.WORD, corner_radius=8)
text_input.grid(row=0, column=0, padx=20, pady=10, sticky="nsew")
result_output = ctk.CTkTextbox(root, height=10, width=30, font=("Consolas", 19, "bold"), wrap=tk.WORD, corner_radius=8)
result_output.grid(row=0, column=1, columnspan=3, padx=10, pady=10, sticky="nsew")
run_button = ctk.CTkButton(root, text="Run Code", command=use_GUI, width=200, height=40, font=("Consolas", 15, "bold"))
run_button.grid(row=2, column=0, padx=20, pady=10)
root.mainloop()
Run GUI.py
to start the graphical interface.
- Ensure
program.exe
is in the same folder asGUI.py
. - After updating the Lex or Yacc files, rebuild the program using:
flex LexFile.l; yacc -d YaccFile.y; gcc lex.yy.c -o lexer; gcc YaccFile.tab.c lex.yy.c -o program
This README file provides a comprehensive guide to setting up, running, and enhancing your compiler project with a GUI. Make sure to follow each step carefully for a successful implementation.