This tutorial will teach you how to use the Scala parser combinator library to perform non-trivial text processing tasks. It is a very useful skill to have, since there is a high chance that you are going to encounter a problem of this type in your professional career (I had to implement a custom parser on two occasions so far). In addition, it is a really fun challenge to create your own mini programming language.
We are going to focus on the practical aspects of implementing a parser for handling formula expressions, so this is by no means a formal theoretical introduction to compiler theory, language design, or anything like that. We will start by defining a use case and build up a solution step-by-step by writing code and making design decisions as we develop it. Of course, you need to have some experience with Scala to follow along, but there are no particular prerequisites, so we will focus on clarity as much as possible. For all of the above reasons we are not going to dive into parser optimization and deeper topics, since there are whole books written on these matters.
Table of Contents
- Introduction - General introduction to the Scala parser tutorial.
- Use Case Definition - Here we define the example problem that we are going to solve.
- Project Set-Up - How to configure our Scala project and add some necessary functions for loading data, etc.
- Lexer - How to implement a lexer for the tokens that we allow in the formulas.
- Parser - How to define the valid formula syntax by composing grammar rules.
- Formula Evaluation - How to reuse the formulas as functions with different input.
- Tying Everything Together - Complete a fully function version of the program.
- Getting Fancy - Adding more languge features in the formulas.