Abstract
Large Language Models (LLMs) often struggle with semantic preservation when explaining code. We present a "self-reflection" framework that enables LLMs to generate highly accurate Equivalent Representations (ERs)—such as pseudocode, flowcharts, or structured data. By employing a dual-model loop where one LLM generates a representation and another reconstructs the original code, the system iteratively refines its output. This approach significantly reduces hallucinations and ensures near-perfect semantic alignment without requiring labeled training data.
Introduction
While LLMs excel at code generation, providing accurate, human-readable explanations remains a challenge. Developers rely on Equivalent Representations (ERs) like comments and diagrams for maintenance and communication. However, manual creation is labor-intensive, and automated tools often fail to capture 100% of the underlying logic. The core challenge is ensuring that a natural language description or flowchart is semantically identical to the source code.
Methodology: The Self-Reflection Framework
The proposed approach treats ER generation as a translation task with an integrated verification step, utilizing a dual-model architecture.
The Dual Framework
- ER Generation ($M_{CR}$): This model transforms the input source code into an ER, such as a JSON dictionary or pseudocode.
- Code Reconstruction ($M_{RC}$): This model acts as a validator, attempting to reconstruct the original source code based solely on the ER produced by $M_{CR}$.
The Reflection Loop
If the reconstructed code deviates from the original, the system calculates a Semantic-Equivalent Score based on text similarity and abstract syntax tree (AST) overlap. This score is converted into natural language feedback and fed back to $M_{CR}$. A Memory Module allows $M_{CR}$ to store previous attempts and feedback, enabling it to learn from errors and iteratively refine the ER over $N$ iterations until a high accuracy threshold is met.
The framework supports both an Open Setting, where the LLM determines the optimal format, and a Constrained Setting, where specific formats like flowcharts are required.
Experimental Setup
The researchers evaluated the framework using the CoNaLa dataset, comprising 500 Python snippets from Stack Overflow. These snippets cover a broad spectrum of real-world tasks, including complex API interactions, mathematical operations, and data manipulation.
Analysis of LLM Representations
The "Open Setting" experiments revealed how LLMs conceptualize code structure:
- Preference for Structure: LLMs chose structured formats (JSON, XML, tables) 55% of the time over plain natural language, suggesting they perceive code as a hierarchical structure.
- Mathematical Abstraction: For calculation-heavy code, models naturally used symbolic notation (e.g., $\sum$), using mathematics as a bridge for data flow.
- API Reasoning: Models could infer the purpose of APIs (e.g., mapping
os.getpid()to "process ID") through context, effectively "de-abbreviating" logic without external documentation. - Adaptive Logic: The representation style adapted to the code's features, such as using SQL-style logic for filtering or flowcharts for complex branching.
Key Results
The self-reflection loop proved highly effective at reducing hallucinations. While initial ERs often contained errors, the iterative process consistently pushed outputs toward near-perfect semantic matches. In constrained settings, the system produced fluent, technically precise documentation and clear flowcharts. Because the method relies on reconstruction rather than ground-truth labels, it is highly versatile and applicable to low-resource programming languages.
Conclusion and Future Work
This self-reflection approach offers a robust path toward automated, high-fidelity code documentation. Future research will address the computational cost of iterative calls and explore using these ERs as intermediate "bridges" for more accurate code generation (Requirements $\rightarrow$ ER $\rightarrow$ Code). Additionally, the authors propose investigating "LLM-friendly" requirement descriptions based on the structured formats preferred by the models in this study.
Summary updated