When Prompt Engineering Meets Software Engineering: CNL-P as Natural and Robust "APIs'' for Human-AI Interaction

Zhenchang Xing, Yang Liu, Zhuo Cheng, Qing Huang, Dehai Zhao, Daniel Sun, Chenhua Liu

TL;DR

The paper introduces CNL-P (Controlled Natural Language for Prompt), a structured framework that treats prompts as robust "APIs" by merging prompt engineering best practices with rigorous software engineering principles. By introducing a formal grammar and the first-ever static analysis "linting" tool for natural language prompts, the authors provide a way to make human-AI interaction more predictable, modular, and easier to debug without losing the accessibility of natural language.

What Problem Does This Paper Address?

As Large Language Models (LLMs) become the backbone of modern applications, the "prompt" has become the primary interface—essentially the API—between humans and AI. However, this interface is currently plagued by two extremes:

  1. Natural Language (NL) Prompts: These are easy to write but suffer from ambiguity, lack of structure, and "hallucinations" in how the model interprets instructions. They are notoriously difficult to maintain or debug at scale.
  2. Programming Language (PL) Frameworks: Tools like LangChain or DSPy offer control but require significant coding expertise. They often lead to "tight coupling," where the prompt logic is buried inside complex code, making it inaccessible to domain experts who aren't software engineers.

The authors identify a "macro-level" gap: we need the rigor of software engineering (modularity, encapsulation, and error checking) applied directly to the natural language we use to talk to AI.

Methods

The authors propose CNL-P, which stands on three main pillars:

  • Formal Syntax (BNF): Unlike loose templates, CNL-P is defined by a Backus-Naur Form (BNF) grammar. It organizes prompts into specific components: PERSONA, CONSTRAINTS, DATA_TYPES, VARIABLES, and a WORKER (which defines the execution flow).
  • SE Principles in Prompts: It enforces Modularity (independent components), Abstraction (hiding implementation details), and Separation of Concerns (distinguishing what the agent is from how it performs tasks).
  • The CNL-P Linting Tool: This is perhaps the most novel technical contribution. Drawing inspiration from traditional compilers, the authors built a tool that performs static analysis on prompts. It checks for syntactic errors and semantic inconsistencies (like using a variable before it’s defined or passing the wrong data type to an API call) before the prompt is ever sent to the LLM.
Architecture of the CNL-P Linting Tool
  • NL2CNL-P Transformer: To lower the barrier to entry, they developed an LLM-based agent that automatically converts "messy" free-form natural language into the structured CNL-P format.

Datasets

The researchers evaluated CNL-P using two primary sources:

  • Awesome ChatGPT Prompts: They selected 93 high-quality, complex prompt instances from this popular GitHub repository (113K+ stars) to test the quality of conversion from NL to CNL-P.
  • AllenAI Classification Tasks: They used six diverse classification tasks (e.g., math reasoning, document suitability) to test whether LLMs could actually execute instructions written in CNL-P syntax without prior training.

Key Insights and Ideas

  • "Linting" Natural Language: The most surprising insight is that you can apply static analysis to NL. By forcing NL into a "controlled" structure, the authors created a middle ground where a program can catch logic errors that even a powerful model like GPT-4o might overlook during execution.
  • Zero-Shot Understanding: LLMs are surprisingly good at following a new, structured syntax (CNL-P) without any "few-shot" examples or fine-tuning. The models seem to treat the structured keywords as strong anchors for attention.
  • The Technical vs. Non-Technical Divide: Human evaluation revealed that while technical users loved the modularity and rigor of CNL-P, non-technical users initially found it less "readable." This highlights a classic trade-off: structure provides power but increases the initial cognitive load.

Key Results

  • Superior Structure: In head-to-head comparisons with other prompt frameworks like RISEN and RODES, CNL-P scored significantly higher in Modularity, Extensibility, and Process Rigor across both human and LLM evaluations.
Evaluation comparisons across LLM, Technical, and Non-Technical groups
  • Performance Parity: Despite its rigid structure, LLMs (GPT-4o, Llama-3, Claude-3, etc.) achieved accuracy on complex tasks that was equal to or better than traditional NL prompts, proving that the structure doesn't "confuse" the model.
  • Linter Accuracy: The CNL-P linting tool achieved 100% accuracy in identifying 47 different types of coding/logic errors in prompts. In contrast, asking GPT-4o to find those same errors manually resulted in only 76.6% accuracy and a 19% "redundancy" rate (making up errors that weren't there).

Limitations and Future Work

The authors acknowledge that CNL-P is in its early stages. Key limitations include:

  • Readability: For non-programmers, the "code-like" appearance can be intimidating. The authors suggest that better IDEs (like their proposed UGAiForge) and better NL-to-CNL-P transformers are needed to bridge this gap.
  • State Management: While CNL-P improves on current methods, managing complex "state" across long, multi-turn interactions remains a challenge for any prompt-based system.
  • Ecosystem Development: A language is only as good as its tools. The authors envision a future "SE4AI" infrastructure, including full-stack compilers that can translate CNL-P requirements directly into optimized machine code or API orchestrations.

Summary updated

BibTeX
@inproceedings{xing2025when,
  author = {Zhenchang Xing and Yang Liu and Zhuo Cheng and Qing Huang and Dehai Zhao and Daniel SUN and Chenhua Liu},
  title = {{When Prompt Engineering Meets Software Engineering: CNL-P as Natural and Robust "APIs'' for Human-AI Interaction}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=SqoL14HDm0},
}
Figure