flex-bison-in-action/main.py

43 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import os
import subprocess
# ЭТИ ПУТИ НАДО ЗАДАТЬ ВРУЧНУЮ
# АНАЛИЗАТОРЫ .l, .y ДОЛЖНЫ ИМЕТЬ ОДИНАКОВЫЙ basename
# И ЛЕЖАТЬ В ПАПАКЕ С ИМЕНЕМ basename
ANALYZERS_DIR = r'C:\Users\user\Desktop\УЧЕБА\6_СЕМ\КОМПИЛЯТОРЫ\flex_bison_test\analyzers\calc'
FLEX_EXE_PATH = r"C:\tools\win_flex_bison\win_flex.exe"
BISON_EXE_PATH = r"C:\tools\win_flex_bison\win_bison.exe"
#
def main():
# Подготовка путей
analyzer_name = os.path.basename(ANALYZERS_DIR)
lexical_analyzer_path = fr"{ANALYZERS_DIR}\{analyzer_name}.l"
syntaxic_analyzer_path = fr"{ANALYZERS_DIR}\{analyzer_name}.y"
#
# Подготовка списка команд
cmds = [f'{FLEX_EXE_PATH} {lexical_analyzer_path}',
f'{BISON_EXE_PATH} -d {syntaxic_analyzer_path}',
f'gcc lex.yy.c {analyzer_name}.tab.c -o {analyzer_name}.exe']
#
# Исполнение команд
for cmd in cmds:
try:
subprocess.run(cmd, capture_output=True, check=True)
print(f'\033[92mSuccess execute {cmd}!\033[0m')
except subprocess.CalledProcessError as e:
print(f"\033[91mError during export: {e}\033[0m")
#
# Очистка промежуточных файлов
for path in ['lex.yy.c',
f'{analyzer_name}.tab.c',
f'{analyzer_name}.tab.h']:
os.remove(path)
#
if __name__ == "__main__":
main()