potentialpaper

Context-free Recognition with Transformers

Selim Jerad, Anej Svete, Sophie Hao, Ryan Cotterell, William Merrill

ICML 2026

Edit this page

Or clone it with git

Run in a terminal:

Needs Git LFS. To push, use an API key from your profile as the password.

Or let your agent set it up

It checks and sets up everything: signing in, git, the clone, credentials and an SSH key.

Abstract. Transformers excel on tasks that process well-formed inputs according to some grammar, such as natural language and code. However, it remains unclear how they can process grammatical syntax internally. In fact, under standard complexity conjectures, standard transformers cannot recognize context-free languages (CFLs), a canonical formalism to describe syntax, or even regular languages, a subclass of CFLs. Merrill & Sabharwal (2025) show that O(log(n))\mathcal{O}(log(n)) looping layers (w.r.t. input length nn) allows transformers to recognize regular languages, but the question of context-free recognition remained open. In this work, we show that looped transformers with O(log(n))\mathcal{O}(log(n)) looping layers and O(n6)\mathcal{O}(n^6) padding tokens can recognize all CFLs. However, training and inference with O(n6)\mathcal{O}(n^6) padding tokens is potentially impractical. Fortunately, we show that, for natural subclasses such as unambiguous CFLs, the recognition problem on transformers becomes more tractable, requiring O(n3)\mathcal{O}(n^3) padding. We empirically validate our results and show that looping helps on languages that require logarithmic depth. Overall, our results shed light on the intricacy of CFL recognition by transformers: while general recognition may require an intractable amount of padding, natural constraints such as unambiguity yield efficient recognition algorithms.

Are you an author? Claim this paper to see who's reading it, with views and other statistics, and to write about it here.

Claim this paper
How does this paper connect to your work? Write how you built on it, compared with it or reproduced it, or add a summary, a critique or errata.
Or make it interactive:
Start writing

edit · history

FromAToBDateVersionSubmitted toPDFEvidence
19 Sep 2025ICLR 2026submittedICLR 2026Rejected—[1]
24 Jan 2026ICML 2026submittedICML 2026Accepted—[1]
Their titles, abstracts and, where both have a PDF, the text.Missing a version?

We don't know of a submission of this paper to a venue with open reviews. Missing a submission?

Loading the discussion…