Semantic Parsing — Natural Language to Formal Representation

Machine Learning
Introduced by
Zelle & Mooney (1996) — foundational supervised approach
Year
1996 (modern neural revival c. 2018)
Type
NLP structured-prediction task

What does it do?

Semantic parsing is a natural-language-processing task, and one subtask within the wider field of natural-language understanding, that converts free-text utterances into executable formal representations such as SQL queries, logical forms, or Abstract Meaning Representations (AMR).

Zelle and Mooney (1996) introduced a supervised approach, based on inductive logic programming, for parsing English database queries. The Spider benchmark (Yu et al., 2018) extended the task to cross-domain settings, where the parser must handle databases that differ from those seen in training.

The intuition behind the method

When a user types a question in plain English — 'How many students passed the exam last year?' — a database or reasoning engine cannot understand it directly. Semantic parsing acts as a translator: it reads the natural-language question and produces a precise formal instruction (such as a SQL SELECT statement or a logic expression) that a computer can execute. The parser learns the mapping between language patterns and formal structures from annotated examples, so it can generalise to new questions it has never seen.

History

Zelle and Mooney (1996) introduced a supervised approach, using inductive logic programming, to parse English database queries. Early systems relied on hand-crafted grammars and feature-engineered models. Later work moved toward neural encoder-decoder models. The Spider benchmark (Yu et al., 2018) made cross-domain evaluation a central test for text-to-SQL systems.

When to use it

Semantic parsing fits when a natural-language question or command must be turned into an executable formal language. Use SQL when the target is a relational database and the result can be checked by execution. Use logical forms for question answering over a knowledge base. Use AMR when the aim is a meaning representation that does not depend on any particular database schema, for example for summarisation or knowledge extraction. The target representation must be defined before annotation begins, because the parser can only output structures in that language. Choose semantic parsing when the output will be executed or checked; it is less suitable when the goal is open-ended text generation or when no formal target exists. Compositional questions with nested clauses or multi-step reasoning are a known difficulty and call for more capable models and careful evaluation. Data needs depend on the target language, domain and model. Evaluation requires a held-out test set large enough to give stable accuracy estimates, and small test sets give high-variance results that should be reported with their uncertainty. Training usually requires far more annotated pairs than evaluation. Any cross-domain claim requires a test set whose databases do not appear in training, as in Spider.

Limitations and considerations

  • The target formal language and its schema must be fixed before annotation or training; a change to the database schema or to the representation generally requires new or revised data.
  • Compositional and multi-step questions remain difficult, particularly on cross-domain databases the parser has not seen.
  • Creating aligned pairs of utterances and formal representations is costly and needs annotators who understand the target language.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Outputs are executable: a generated SQL query or logical form can be run and its result compared with a gold answer, which is not possible with free-text generation.
  • Lets users query a database or knowledge base in plain language without writing the formal query themselves.
  • Neural sequence-to-sequence and transformer-based decoders can be trained on cross-domain benchmarks such as Spider, so generalisation to unseen databases can be measured directly.

Applications

  • Natural-language interfaces to relational databases — allowing analysts to query data warehouses in plain English.
  • Question-answering systems that retrieve answers by executing logical queries over knowledge graphs or structured knowledge bases.
  • Voice assistants and conversational agents that translate spoken commands into API calls or device instructions.
  • Scientific and clinical data exploration — enabling researchers without SQL expertise to interrogate structured datasets.

How it works

How it works

1

A paired corpus of natural-language utterances and their target formal representations is assembled. Each pair links a sentence to its correct SQL query, logical form, or AMR graph. The quality and coverage of this corpus directly determines what the parser can learn: narrow domain corpora tend to produce specialised parsers, while cross-domain datasets such as Spider are built to test generalisation to databases not seen during training.

2

The input utterance is encoded into a numerical form the parser can process. Many current systems use contextual embeddings from pretrained neural language models, which represent a word's meaning in its sentence context. The target formal language is also represented structurally, as a sequence of tokens (for SQL), a tree, or a graph, so the decoder knows which output shapes are allowed.

3

The parser maps the encoded utterance to a target formal structure. Sequence-to-sequence models commonly generate SQL or logical-form tokens one at a time, and graph-based decoders produce AMR graphs. Constrained decoding is one approach in which the target language's grammar restricts each generated token so that the output is syntactically valid. For text-to-SQL, this step produces a query that can be run directly against a relational database.

4

The generated formal output is checked against gold-standard annotations. Exact-match accuracy compares the predicted query with the gold query after light normalisation, such as whitespace and letter case. It is strict and can count semantically equivalent queries as errors. Execution accuracy runs both queries against the database and compares the returned results. It is closer to what the user needs, but its value depends on the database content. The Spider benchmark reports both measures (Yu et al., 2018), and using them together is standard practice for text-to-SQL. For AMR graphs, the Smatch score compares predicted and gold graphs; it is specific to AMR and is not a general measure for other formal languages. Precision and recall are also used in some logical-form evaluations. Error analysis shows whether the parser confuses schema elements, mishandles compositional structures, or fails on out-of-domain vocabulary.

Common pitfalls

  • Applying a parser trained on one database schema to another without checking schema linking (matching question words to tables and columns) produces plausible but incorrect queries.
  • Evaluating only with exact-match accuracy can be misleading: two SQL queries may be syntactically different but return the same correct answer; use execution accuracy alongside exact match.
  • Treating parsing as a simple string mapping ignores nested clauses and scope, so parsers fail on questions with several conditions or embedded sub-questions.
  • Skipping grammar-constrained decoding allows the model to output syntactically invalid SQL or malformed logical forms that cannot be executed.
  • Claiming cross-domain generalisation from a random split of questions over one database; this does not test performance on databases absent from training.

Frequently asked questions

What is the difference between semantic parsing and machine translation?

Machine translation maps natural language to another natural language, preserving meaning in a human-readable form. Semantic parsing maps natural language to a formal, executable representation such as SQL or a logical form; the output must conform to a strict grammar and can be run by a computer to produce an answer.

How much annotated data do I need?

Evaluation needs a held-out test set large enough to give stable accuracy estimates; very small test sets produce high-variance results that should be reported with their uncertainty. Training usually needs far more annotated natural-language/formal-representation pairs, and the exact amount depends on the target language, the domain and the model. Cross-domain benchmarks such as Spider are a practical starting point when domain-specific annotation is scarce.

Can semantic parsing work for languages other than English?

Yes, but annotated corpora and pretrained models for many other languages are less abundant. A case report on Romanian-language text found that a commercial cognitive service not trained for that language added complexity to entity identification and cognitive querying. Multilingual pretrained encoders may help, but results for languages with little annotation can lag behind English.

What is AMR and when should I use it instead of SQL?

Abstract Meaning Representation (AMR) is a graph-based formalism that captures the meaning of a sentence independent of any database schema. Use AMR when the goal is broad semantic understanding, summarisation, or knowledge extraction rather than database querying. Use SQL when the target is a relational database and executable query accuracy is the primary measure of success.

Related methods

Sources

  1. Zelle, J.M. & Mooney, R.J. (1996). Learning to Parse Database Queries Using Inductive Logic Programming. AAAI.

    Open publication reference
  2. Yu, T. et al. (2018). Spider: A Large-Scale Human-Labeled Dataset for Complex and Cross-Domain Semantic Parsing. EMNLP.

    Open publication reference
  3. 02_df481c09536e6a8e887e995949c2c99f01b00ccc, p. 6