Neuro-Symbolic Query Compiler (2025)

Abstract: Precise recognition of search intent in Retrieval-Augmented Generation (RAG) systems remains a challenging goal, especially under resource constraints and for complex queries with nested structures and dependencies. This paper presents **QCompiler**, a neuro-symbolic framework inspired by linguistic grammar rules and compiler design, to bridge this gap. It theoretically presents a minimal yet sufficient Backus-Naur Form (BNF) grammar G[q] to formalize complex queries. Unlike previous methods, this grammar maintains completeness while minimizing redundancy. Based on this, QCompiler includes a query expression translator, a Lexical syntax parser, and a Recursive Descent Processor to compile queries into Abstract Syntax Trees (ASTs) for execution. The atomicity of the sub-queries in the leaf nodes ensures more precise document retrieval and response generation, significantly improving the RAG system’s ability to address complex queries.

## introduction in the field of cognitive science, the human brain demonstrates a sophisticated synergy between two cognitive systems @xcite : on the one hand, through neural-networks-based computation, humans can rapidly process information from complex sensory inputs. on the other hand, with symbolic-system-based logical reasoning, humans can analyze abstract rules such as language, mathematics, and causality, performing calculations and inferences through symbols and rules. these two systems complement each other, enabling humans to flexibly handle a range of complex tasks from perception to reasoning, exhibiting a powerful generalization capability that cannot be achieved by a single mechanism alone. in the field of artificial intelligence, artificial neural networks (anns) have shown strong fitting capabilities but struggle to extrapolate and * corresponding author. generalize in a world of constantly updated knowledge @xcite . recently, retrieval-augmented generation (rag) technique addresses this limitation to some extent by introducing a retrieval process that allows anns to access external knowledge beyond their training data @xcite . this enhancement improves the ability of neural networks to process information in open domains and real-world scenarios. however, this improvement has a limited upper bound: as user queries become more complex or require reasoning, the chance of retrieving all relevant documents at once decreases significantly, leading to poor performance of rag systems, as shown in figure 1(a) . the handling of complex user queries for anns presents a significant challenge: these queries often contain implicit intents, nested logical structures, and intricate dependencies, making it difficult to arrive at answers in a single step. for example, the query "i want to find an introduction and reviews of j.k. rowling's most popular book and check if the local library has it" may require the system to use anns to extract key information, while relying on symbolic rules for task decompo-sition and reasoning-enabling multi-turn interaction with both databases and users. the system cannot execute other queries without first identifying which is the most popular book. this naturally raises the first question: how can we effectively process complex queries to recognize search intent precisely? a straightforward idea is to leverage the powerful anns' capabilities to achieve this in an endto-end manner @xcite @xcite . unlike the human brain, however, anns often struggle with tasks that require explicit symbolic reasoning. this is in part because they may not encounter sufficient data related to specific symbolic reasoning, leading to implicit reasoning modeling, making it difficult to handle complex queries that require explicit analysis and decomposition. this raises a second question: how can we replicate the synergy of two cognitive systems in human brain, neural computation and symbolic reasoning, to effectively address complex queries in real-world scenarios? many studies have investigated approaches for rewriting, disambiguating, and decomposing complex queries using symbolic and heuristic rules @xcite @xcite . we can regard complex queries as a domain specific language (dsl) generated by a certain complete grammar g @xcite , and such approaches can be viewed as selecting implicitly or explicitly a subset g ′ of grammar g (denoted as g ′ ⊆ g) to generate queries. there has also been research on the use of llms to handle complex queries, with iterative and agentic rag systems showing strong problem planning & solving capabilities @xcite . however, these methods face two main issues: (1) although the processing of complex queries may cover all implied intentions, from the perspective of grammar-generated languages, this coverage may not be minimal @xcite , resulting in unnecessary complexity and resource waste. to address these questions, we propose qcom-piler, inspired by grammars in linguistics and compiler theory. we first summarize four query types and design a backus-naur form (bnf) grammar g[q] ⊂ g ′ ⊆ g that is minimally sufficient to formalize these queries. then we train a small language model to translate natural-language queries into these bnf-based expressions, which can be parsed into an abstract syntax tree (ast). the grammar g[q] constrains the search space during language model generation, while the parsed abstract syntax tree (ast) enables symbolic reasoning by recursively applying grammar rules at each node. this process efficiently handles nested structures and dependencies of complex queries, resembling a compiler that can translate high-level code into machine-executable instructions. this framework can be seamlessly customized into the existing rag system for better query understanding because: (1) the parsed ast can capture the implicit search intents, nested structure, and dependencies of the original complex query. our contributions in this paper are as follows: ## methodology in this work, we focus on the understanding and representation of complex queries. we give an overview of qcompiler and how it works in figure 2 , and details of grammar, components used to implement each step, training, and inference in the following sections. ## experiment in this section, we validate the effectiveness of our method through a series of experiments. ## analysis in this section, we focus on analyzing how qcompiler can improve the performance of the rag system. ## conclusion in this paper, we present qcompiler, a neurosymbolic framework inspired by linguistic grammar rules and compiler design. we first give a minimal yet sufficient grammar g[q] to formalize and generate complex queries. then qcompiler can efficiently compile each complex query into an abstract syntax tree that contains these types and captures its nested structures and complex dependencies, demonstrating a strong ability to understand and analyze complex queries. the atomicity of the sub-queries in the leaf nodes ensures precise document retrieval and response generation. this lightweight framework enables seamless integration with existing rag systems, highlighting its broad applicability and flexibility in practical deployment scenarios.