#!/usr/bin/env python3 import sys import os from pathlib import Path # Add the current directory to Python path so we can import our modules sys.path.insert(0, '/home/janniclas/Projekte/Construction-of-Compilers/Project-02-03-04') import triplayacc as yacc import cfg_build import syntax from cfg.CFG import CFG from cfg.CFG_Node import (CFG_START, CFG_END) def make_cfg(ast): start = CFG_START() end = CFG_END() last = ast.cfa(start, end) if last is not None: last.add_child(end) return CFG(start, end) def generate_dot_files(): base = Path("triplaprograms") programs = sorted([f for f in base.glob("*.tripla")]) output_dir = Path("mistraltests/before") output_dir.mkdir(exist_ok=True, parents=True) for path in programs: try: print(f"Processing {path.name}...") # Reset the global FUNCTIONS registry cfg_build.FUNCTIONS.clear() source = path.read_text() ast = yacc.parser.parse(source) # Create CFG cfg = make_cfg(ast) # Generate dot file dot_str = cfg.to_dot() # Save to file output_path = output_dir / f"{path.stem}_cfg_before.dot" with open(output_path, "w") as f: f.write(dot_str) print(f"Saved: {output_path}") except Exception as e: print(f"Error processing {path.name}: {e}") continue if __name__ == "__main__": generate_dot_files() print("Done generating 'before' dot files.")