r/Compilers • u/kaushalbx • Aug 17 '26
Can Sanskrit work as a natural programming language?
I’ve been experimenting with this idea by building a Sanskrit compiler based on Pāṇinian grammar.
Instead of treating Sanskrit only as text to interpret, the compiler parses grammatically structured Sanskrit and turns the instructions into executable operations.
The interesting part for me is whether Pāṇini’s formal grammatical system can provide enough structure to bridge natural language and programming languages deterministically.
I now have a working implementation and would be interested in hearing what others think about this approach.
Website: https://panini.cc/
GitHub: https://github.com/kaushalbx/paninivm
Article: https://medium.com/@kaushalbx/p%C4%81%E1%B9%87inivm-building-a-natural-programming-language-with-sanskrit-grammar-aa82b855074c
Article: https://medium.com/@kaushalbx/building-p%C4%81%E1%B9%87inivm-compiling-2-500-year-old-paninian-grammar-into-an-executable-kotlin-engine-5a6bb8de20fb
7
u/ktimespi Aug 17 '26
regular expressions were meant to capture spoken language initially, and failed. So I think it is possible to a certain degree.
2
u/particlemanwavegirl Aug 18 '26
You can do this but if you then use the language to implement a nam-shub, that's where things start to get messy.
22
u/dandy_kulomin Aug 17 '26
I have no idea as to your question, but I love this. I also don't know sanskrit so I can't try it.
2
u/Arakela Aug 17 '26 edited Aug 17 '26
Grammatical text can only be constructed in relation to an observer. The source language can itself construct the space in which grammatical text becomes observable, using words of that same language. In this sense, grammar is not merely describing a structure; it is generating the space and time through which that structure is observed. For example, in CoffeeScript:
1
u/Imaginary_Concern400 Aug 17 '26
Interesting... i assume you would define a mapping from Sanskrit characters to regular ASCII ones? But even then, how can we define the regex? I am interested to know, how did you implement lexing and parsing for this? (I have read the comment of using ANTLR, but I am interested to know how you went about defining the regex for it)
Very interesting project though :)
1
u/Sad_Discipline8288 Aug 18 '26
I tried experimenting a similar intent compiler on top of LaTeX did not work there,far too many structural problems and ambiguity with these languages...
1
u/VectorspaceDreams Aug 20 '26 edited Aug 22 '26
What you can take from, not sure if you've heard of it but you might've, is Kip, an experimental Haskell implemented programming language that uses Turkish, which is also highly inflected and the grammatical function parts aren't just dressing overtop but are required to build a well-typed program.
I've heard Panini's work is proto-generative grammar, but some other scholars of Sanskrit said that the Ashtadhyayi is not truly generative in the sense that it could theoretically generate all possible Sanskrit sentences, so I'm not sure if it's enough to build an entire formal programming framework but I don't know.
On this note, Kip uses an FST for morphological analysis; do you use regex for it? If so, I recommend you look into finite-state morphology and xfst. I've made an Akkadian noun analyzer with regex before but modern regexes are often not regular (so I guess they're just "expressions") and (to my knowledge) have far greater expressive power than even most natural languages and I'm not sure if you need a whole regex module for Sanskrit morphological analysis, and even if you did, rolling out regexes for grammar is a massive headache (partly why I've left the project in the dust for now)
Upvoted and will star on GitHub. Will keep up with this 100%
0
u/kaushalbx Aug 17 '26 edited Aug 17 '26
Website: https://panini.cc/
GitHub: https://github.com/kaushalbx/paninivm
Article: https://medium.com/@kaushalbx/building-p%C4%81%E1%B9%87inivm-compiling-2-500-year-old-paninian-grammar-into-an-executable-kotlin-engine-5a6bb8de20fb
0
u/kaushalbx Aug 17 '26
Here is an example showing number guessing game.
1
u/[deleted] Aug 17 '26
[removed] — view removed comment