Structural Code Search using Natural Language Queries

Ben Limpanukorn, Yanjun Wang, Zach Patterson, Pranav Garg, Murali Krishna Ramanathan, Xiaofei Ma, Anoop Deoras, Miryung KimarXiv 2025 · DOI

Introduction

Developers spend a significant portion of their time searching through code. While keyword and regular expression searches are ubiquitous, they are often "blind" to the actual logic and hierarchy of code. Structural code search allows for much more sophisticated queries—for example, "find all calls to foo() that take a String argument and are located inside a while loop."

However, structural search tools (like Semgrep or CodeQL) require users to write queries in complex, specialized domain-specific languages (DSLs). This creates a high barrier to entry. On the other end of the spectrum, "semantic search" (using vector embeddings) is easy to use but often fails to capture the precise syntactic constraints required for structural tasks. Pure LLM-based search is accurate but prohibitively slow and expensive for large-scale production codebases. This paper proposes a "middle way": combining the ease of natural language with the precision and efficiency of specialized structural search engines.

Methodology

The researchers developed a general framework to translate Natural Language (NL) queries into DSL queries. This approach leverages the reasoning capabilities of Large Language Models (LLMs) to perform translation while relying on the deterministic precision of tools like Semgrep for the actual retrieval.

System architecture for translating natural language queries to structural DSL queries via RAG and LLMs.

The process involves three main pillars:

  1. Synthetic Data Generation: To overcome the lack of existing "English-to-DSL" datasets, the authors built an algorithm to generate them. They identify specific code constructs (like an if statement) in a real code corpus, generate a matching DSL query, and then use an LLM to "back-translate" that DSL query into a natural language description.
  2. RAG-Based Translation: When a user enters a query, the system uses Retrieval-Augmented Generation (RAG) to find similar (NL, DSL) pairs from its synthetic dataset. These pairs serve as few-shot examples for an LLM (GPT-4o), which then translates the user's request into the target DSL.
  3. Query Refinement with Error Feedback: LLMs sometimes misidentify the "target" of a query (e.g., matching a surrounding loop instead of the specific function call inside it). The system uses static analysis to check if the generated DSL query actually targets the construct type the user requested.
The query refinement loop using static analysis to validate the target of the generated DSL query.

Evaluation and Results

The researchers constructed a new benchmark for structural code search consisting of 400 total structural queries (200 each for GQL and Semgrep) mapped to 10 diverse Java projects. Unlike existing benchmarks like CodeSearchNet, this dataset focuses specifically on syntactic and structural patterns.

Key results include:

  • High Accuracy: The NL-to-DSL approach achieved F1 scores between 55% and 70% across the benchmarks.
  • Superior Performance: The proposed method outperformed semantic vector search by up to 57% and pure LLM retrieval by 14% in F1 scores.
  • Efficiency: By translating to a DSL, the system only needs to call the LLM once per query, making it 22x more token-efficient than pure LLM-based retrieval. This makes it a viable solution for large enterprise repositories.

Discussion and Future Work

The paper demonstrates that synthetic data can effectively "bootstrap" translation systems for niche DSLs. However, some limitations remain. The current approach focuses primarily on conjunctions (e.g., "X AND Y"), and future work is needed to support complex negations and disjunctions. Additionally, while the framework is DSL-agnostic, expanding support beyond Java would require instantiating the generation logic for other languages' specific AST constructs. Finally, a future "router" could be implemented to automatically determine which DSL is best suited for a specific user query.

Summary updated

BibTeX
@misc{limpanukorn2025structural,
  author = {Ben Limpanukorn and Yanjun Wang and Zach Patterson and Pranav Garg and Murali Krishna Ramanathan and Xiaofei Ma and Anoop Deoras and Miryung Kim},
  title = {{Structural Code Search using Natural Language Queries}},
  year = {2025},
  eprint = {2507.02107},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2507.02107},
}
Figure