Thousands of years ago, a man living in what is now India wrote down all the grammatical guidelines that govern Sanskrit; one of the earliest documented languages in the ancient world.

His name was Pāṇini, and his 4,000-some grammatical sūtras, or rules, are supposed to work like an algorithm that can generate grammatically correct words from a base and suffix.

For centuries, linguists have been trying to rebuild this 'language machine' using the thousands of steps described by Pāṇini in his legendary text, the Aṣṭādhyāyī. And yet it never really has run as it should.

A PhD student at the University of Cambridge, Rishi Rajpopat, thinks he's finally cracked the ancient puzzle, and his solution is impressively simple. All that it does is change the interpretation of one 'metarule' outlined by Pāṇini, and voila, the machine runs by itself with almost no exceptions.

The inspiration came to Rajpopat after nine months of tangling himself in the Pāṇini problem for his master's thesis at Cambridge.

"I was almost ready to quit, I was getting nowhere," he recalls.

But then Rajpopat's supervisor, a professor of Asian and Middle Eastern studies at the University of Cambridge named Vincenzo Vergiani, reminded him of an important principle of problem-solving: "If the solution is complicated, you are probably wrong."

"So I closed the books for a month and just enjoyed the summer, swimming, cycling, cooking, praying, and meditating," says Rajpopat.

"Then, begrudgingly, I went back to work, and, within minutes, as I turned the pages, these patterns started emerging, and it all started to make sense."

After two and a half years of careful work, Rajpopat has clearly laid out why Pāṇini's engine had previously stalled at a common linguistic crossroads.

Oftentimes, when using Pāṇini's grammar guidelines, two rules will come into conflict, and it's never really been clear which rule should ultimately win out.

Rajpopat has essentially solved which way the switch should flip.

In the past, other scholars have argued that if two rules of equal strength come head to head, the one listed later in Pāṇini's text wins out.

But Rajpopat thinks this is an incorrect interpretation of Pāṇini's metarule.

Instead, the linguist argues if two rules are at odds, the rule that applies to the right side of a word (the suffix) should trump the rules that apply to the left side of a word (the stem).

When Rajpopat tested Sanskrit words in this version of the engine, it worked – producing grammatically correct words with almost no exceptions.

The word 'guru' is a good example. In the sentence 'jñānaṁ dīyate guruṇā' ('Knowledge is given by the guru'), there is a Pāṇini rule that applies to the left part of guru and another that applies to the right part.

Under Rajpopat's interpretation, the right part ultimately wins out, which is why the suffix of the word changes to 'guruṇā', while the stem of the word remains the same.

"Pāṇini's style is not entirely self-evident, and one faces challenges at multiple levels when attempting to unravel the enigma that is the Aṣṭādhyāyī," writes Rajpopat in his thesis.

"… it is not easy to determine the exact meanings of Pāṇini's rules because the sūtra style in which they are composed is very concise and compact. Much information is often packed into a few words, thereby making it considerably difficult to comprehend their exact purport."

Words in some rules, for instance, can sometimes slip over into the following rule.

Some scholars take issue with the idea of calling Pāṇini's rules a machine, but Rajpopat argues in his thesis that "Pāṇini did intend for the Aṣṭādhyāyī to be interpreted linearly and as a closed grammatical machine."

His work shows the machine is self-sufficient and can run on its own metarules without the need for scholars to add padding for exceptions.

"Pāṇini had an extraordinary mind, and he built a machine unrivaled in human history," says Rajpopat, whose thesis is titled In Pāṇini We Trust: Discovering the Algorithm for Rule Conflict Resolution in the Astādhyāyī'.

"He didn't expect us to add new ideas to his rules. The more we fiddle with Pāṇini's grammar, the more it eludes us."

For years now, scientists have been trying to create a computer program using Pāṇini's rules, but with little success. Rajpopat's discovery could be the key to make these attempts finally run.

Sanskrit is the sacred language of Hinduism and shares a common relative with Classical Latin. Understanding how it is structured can help experts not only read important historical documents with more accuracy, it could also reveal crucial insights into the foundations of human language itself.

"My student Rishi has cracked it – he has found an extraordinarily elegant solution to a problem which has perplexed scholars for centuries," says Vergiani.

"This discovery will revolutionize the study of Sanskrit at a time when interest in the language is on the rise."

Rajpapot's thesis is available online at the University of Cambridge.