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.

2 Likes

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 GitHub - SoftMoore/CPRL-Kt: CPRL (for Compiler PRoject Language) is a relatively small programming language that was designed for teaching the basics of compiler design. CPRL includes variables, statements, expressions, arrays, and subprograms..

1 Like

There is a new edition to this book and a new GitHub repository to accompany it. For the book, see Compiler Design Using Kotlin™: An Object-Oriented Approach. The corresponding GitHub repository is CPRL-Kt-2nd.

1 Like