# Writing a compiler in Kotlin

**URL:** <https://discuss.kotlinlang.org/t/writing-a-compiler-in-kotlin/19411>\
**Category:** Support\
**Created:** [October 7, 2020, 10:54am UTC](https://discuss.kotlinlang.org/t/writing-a-compiler-in-kotlin/19411 "2020-10-07T10:54:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanospicer](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@nanospicer](https://discuss.kotlinlang.org/u/nanospicer)\
**Post date:** [October 7, 2020, 10:54am UTC](https://discuss.kotlinlang.org/t/writing-a-compiler-in-kotlin/19411/1 "2020-10-07T10:54:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [October 7, 2020, 11:47am UTC](https://discuss.kotlinlang.org/t/writing-a-compiler-in-kotlin/19411/2 "2020-10-07T11:47:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![softmoore](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@softmoore](https://discuss.kotlinlang.org/u/softmoore)\
**Post date:** [October 13, 2020, 1:56pm UTC](https://discuss.kotlinlang.org/t/writing-a-compiler-in-kotlin/19411/3 "2020-10-13T13:56:56Z")

</div>

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](https://www.amazon.com/Introduction-Compiler-Design-Object-Oriented-Approach/dp/1734139153)_. 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.](https://github.com/SoftMoore/CPRL-Kt).

---

<div class="post-metadata">

**Author:** ![softmoore](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@softmoore](https://discuss.kotlinlang.org/u/softmoore)\
**Post date:** [January 12, 2023, 12:30pm UTC](https://discuss.kotlinlang.org/t/writing-a-compiler-in-kotlin/19411/4 "2023-01-12T12:30:07Z")

</div>

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](https://www.amazon.com/Compiler-Design-Using-Kotlin-Object-Oriented/dp/1734139161/)_. The corresponding GitHub repository is [CPRL-Kt-2nd](https://github.com/SoftMoore/CPRL-Kt-2nd).
