NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Language Models as Compilers: Simulating Pseudocode Execution Improves Algorithmic Reasoning in Language Models

Hyungjoo Chae, Yeonghyeon Kim, Seungone Kim, Kai Tzu-iunn Ong, Beong-woo Kwak, Moohyeon Kim, Sunghwan Kim, Taeyoon Kwon, Jiwan Chung, Youngjae Yu, Jinyoung Yeo

Abstract

Large Language Models (LLMs) often struggle with complex algorithmic reasoning because they attempt to solve problems "on-the-fly" without a reusable plan. We introduce THINK-AND-EXECUTE, a framework where an LLM first discovers the high-level logic of a task and expresses it as pseudocode, then "simulates" the execution of that code to solve specific problems. This approach bridges the "planning gap" by providing a structured yet flexible medium for reasoning. Evaluated across various algorithmic benchmarks and real-world robotics tasks, THINK-AND-EXECUTE significantly outperforms standard Chain-of-Thought and Program-of-Thought baselines.

Introduction: The Planning Gap in LLM Reasoning

While LLMs have shown remarkable success in many areas, "algorithmic reasoning"—tasks requiring strict logical steps like tracking shuffled objects or completing nested parentheses—remains a major hurdle. Current solutions generally fall into two camps:

  1. Chain-of-Thought (CoT): The model explains its reasoning in natural language. However, natural language is often ambiguous and informal, making it easy for the model to lose track of complex states.
  2. Program-of-Thought (PoT): The model writes Python code, which is then run by an external interpreter. This requires the model to write a new script for every single instance, which is error-prone and fails to capture the underlying logic shared across similar problems.

The authors identify a "planning gap": LLMs lack a dedicated phase to analyze a task's core logic before diving into specific instances.

Figure 1: Comparison of THINK-and-EXECUTE with CoT and PoT on a "Web of Lies" instance. While CoT and PoT fail on complex logic, THINK-and-EXECUTE correctly simulates the pseudocode logic to reach the answer.

Methodology: The THINK-AND-EXECUTE Framework

The authors propose a two-stage framework that treats the LLM like a compiler, separating task-level logic discovery from instance-level execution.

1. The THINK Phase (Task-level Instruction)

An "Instructor LM" (like GPT-3.5) is given a few example questions from a task. It generates a high-level analysis of the required logic and translates it into a task-level pseudocode prompt. This pseudocode is generic—it uses abstract functions (e.g., extract_information()) and loops to describe how to solve any problem of that type.

2. The EXECUTE Phase (Instance-level Reasoning)

A "Reasoner LM" takes this pseudocode and a specific question. Instead of running the code on a computer, the LLM simulates the execution. It follows the loops and logic gates, using print() statements defined in the pseudocode to output its intermediate reasoning steps (rationales) before arriving at the final answer.

Figure 4: The overall architecture of THINK-and-EXECUTE, contrasting task-level pseudocode generation (THINK) with instance-level simulation (EXECUTE) against the standard Program-of-Thoughts approach.

Experimental Evaluation

The framework was evaluated on 7 algorithmic reasoning tasks from the Big-Bench Hard (BBH) benchmark, including Dyck Languages, Geometric Shapes, Navigate, Reasoning about Colored Objects, Temporal Sequences, Tracking Shuffled Objectives, and Web of Lies. They also tested the framework on SayCan, a real-world robotics planning task.

Key Results

  • Superior Performance: THINK-AND-EXECUTE consistently beat Zero-shot CoT and PoT. On GPT-3.5-Turbo, it achieved an average accuracy of 60.4%, compared to 48.0% for CoT and 32.4% for PoT.
  • Boosting Small Models: For CodeLlama-7B, the framework boosted average performance from ~19% to 30.8%, suggesting that logic can be distilled and shared across different architectures.
  • Robotics: In the SayCan robotics task, the framework achieved 25.7% accuracy, nearly doubling the performance of the best baseline (Chain-of-Code at 14.6%).

Discussion and Insights

  • Pseudocode as the "Goldilocks" Medium: The authors found that pseudocode is better for reasoning than natural language (too vague) or Python (too rigid). It provides a structural backbone that helps the LLM maintain "state" (like a mental stack or dictionary).
  • Task-Level vs. Instance-Level: Discovering the logic once for a whole task is much more efficient than generating a new solution for every question. It allows the model to act more like a compiler and less like a distracted writer.
  • The Power of print(): Including print() statements in the pseudocode forces the model to externalize its internal state, acting as a structured form of Chain-of-Thought that keeps the reasoning on track.

Conclusion and Future Work

THINK-AND-EXECUTE makes a compelling case that if we want LLMs to reason like computers, we should start treating them like compilers. While the approach excels in algorithmic tasks, future work is needed to explore its applicability to more subjective domains and to automate the "meta-prompting" process currently required for the THINK phase.

Summary updated

BibTeX
@inproceedings{chae2024language,
  author = {Hyungjoo Chae and Yeonghyeon Kim and Seungone Kim and Kai Tzu-iunn Ong and Beong-woo Kwak and Moohyeon Kim and Sunghwan Kim and Taeyoon Kwon and Jiwan Chung and Youngjae Yu and Jinyoung Yeo},
  title = {{Language Models as Compilers: Simulating Pseudocode Execution Improves Algorithmic Reasoning in Language Models}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.1253},
}
Figure