Context-free Recognition with Transformers
Abstract
Lay Summary
Transformers, the networks behind today’s language and coding models, excel at texts that follow grammatical rules, like nested clauses or matched brackets in code. This structure is captured by what computer scientists call context-free grammars. Yet, there has been no clear theory of how a transformer could actually verify that an input obeys a grammar’s rules—and theory even suggests a standard fixed-size transformer cannot. This paper closes this gap via two modest changes to the standard transformer architecture. The first is the transformer’s depth: the number of layers grows with the input length. The second is the transformer’s memory space: we append scratch-pad slots to the input that serve as memory. Together, we show these changes enable transformers to robustly understand grammatical syntax. Precisely, we show a transformer can process an arbitrary grammar efficiently, at the cost of a large scratch-pad. Conveniently, the grammars we typically use (such as the ones that describe code) are what we call unambiguous—where a unique set of grammatical rules leads to text. We show that for unambiguous grammars, transformers can process sentences even more efficiently. This aligns with what we see in practice: modern language models sometimes struggle to understand ambiguous sentences in natural language, but seem exceedingly proficient on coding tasks. Our experiments vet our theory: transformers with more depth and space seem to better process grammatical syntax. Overall, our paper provides exact ways in which a transformer can process the syntax of some language, whether it be English or Python.