Writing a compiler in Kotlin

I once wrote a simple compiler using C# and the tooling I used were GPLEX and GPPG for lexer and parser of my language.

I find myself wanting to write another language processor, this time in Kotlin. I have seen that if I intended to do it in Java I would be using JFlex and BYacc/J or Jay. These tools would generate a Lexical Scanner and a Grammar Parser for me but the output of these would be a .java source file.

Which tools exist (or how can I use existing tools) to produce such Scanner and Parser as Kotlin sources? Maybe transform such .java outputs to .kt ones? Or maybe a way to transpile those .java files into Kotlin?

I would personally use them as they come. You can mix kotlin and java source files within the same problem so the fact that your generated files are java shouldn’t be a problem.


It is possible to write a simple compiler (or even a complex compiler) without external tools. You might want to take a look at my recent book entitled Introduction to Compiler Design: An Object-Oriented Approach Using Kotlin. No special compiler-related tools are required or used within this book. Source code to supplement the book can be found on GitHub at https://github.com/SoftMoore/CPRL-Kt.

1 Like