import re # Initialize variables A-Z vars = {chr(i): 0 for i in range(ord('A'), ord('Z')+1)} # --- Read program manually --- print("Enter your program line by line. End input with an empty line:") program_lines = [] while True: try: line = input() if line.strip() == "": # empty line signals end of program break program_lines.append(line) except EOFError: break # Combine lines and remove comments code = "\n".join(program_lines) code = re.sub(r"#.*", "", code).strip() # remove comments # Tokenize commands and parentheses tokens = re.findall(r'\b\w+\b|[()]', code) def execute(tokens, start=0, end=None): i = start output = [] while i < (len(tokens) if end is None else end): cmd = tokens[i] if cmd == 'CLEAR': var = tokens[i+1] vars[var] = 0 i += 2 elif cmd == 'INCREASE': var = tokens[i+1] vars[var] += 1 i += 2 elif cmd == 'PRINT': var = tokens[i+1] output.append(str(vars[var])) i += 2 elif cmd == 'REPEAT': var = tokens[i+1] loop_count = vars[var] # store fixed loop count if tokens[i+2] != 'TIMES' or tokens[i+3] != '(': raise ValueError("Invalid REPEAT syntax") # Find closing parenthesis for the loop depth = 1 j = i + 4 while j < len(tokens) and depth > 0: if tokens[j] == '(': depth += 1 elif tokens[j] == ')': depth -= 1 j += 1 if depth != 0: raise ValueError("Mismatched parentheses in REPEAT") # Execute loop body exactly loop_count times for _ in range(loop_count): output.extend(execute(tokens, i+4, j-1)) i = j # move past the loop else: raise ValueError(f"Unknown command: {cmd}") return output # --- Run the program --- result = execute(tokens) print(" ".join(result))