From ddb6eee6099f2f4d8542e1bfbc86b1c36a170f41 Mon Sep 17 00:00:00 2001 From: Jan-Niclas Loosen Date: Sat, 22 Nov 2025 18:32:29 +0100 Subject: [PATCH] Extend main for export --- Project-02/main.py | 18 +++++++++++++++--- Project-02/triplalex.py | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Project-02/main.py b/Project-02/main.py index 66c30d2..ab17e50 100644 --- a/Project-02/main.py +++ b/Project-02/main.py @@ -100,8 +100,20 @@ if __name__ == "__main__": print("") pretty_print(ast) - # Show AST graph + # Export DOT + dot_str = ast.to_dot() + if prompt_yesno("Export AST as .dot file?"): + default = f"{path.stem}.dot" + out = input(f"Filename [{default}]: ").strip() + if not out: + out = default + try: + Path(out).write_text(dot_str, encoding="utf-8") + print(f"Saved DOT file as: {out}") + except Exception as e: + print(f"Could not save DOT file: {e}") + + # Display AST diagram if prompt_yesno("Display AST diagram?"): - dot_str = ast.to_dot() render_ast_from_string(dot_str) - print("Rendered AST diagram.") \ No newline at end of file + print("Rendered AST diagram.") diff --git a/Project-02/triplalex.py b/Project-02/triplalex.py index b7c463d..7903e03 100644 --- a/Project-02/triplalex.py +++ b/Project-02/triplalex.py @@ -16,7 +16,7 @@ reserved = { 'false': 'FALSE' } -# List of token names. This is always requiredy +# List of token names. tokens = [ 'ID', 'CONST',