Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to the Scala Parser and Combinators

DZone's Guide to

Introduction to the Scala Parser and Combinators

Curious as to how to get started in making parsers that can be used in your own programs? With Scala's parser combinators, it's easy to set up.

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Scala parser combinators are a powerful way to build parsers that can be used in everyday programs. But it’s hard to understand the plumbing pieces and how to get started. After you get the first couple of samples to compile and work, the plumbing starts to make sense. This is a very easy way to design your own programming language — using its parser library.

So let's get started! The first thing we need to do is extend the regex parser.

To use Scala parser combinators, you need to add the following dependencies in your build.sbt:

libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.5"


sbt version is 2.11.3

EBNF grammar for this language would look something like this:

def symbol: Parser[Any] = "+" | "-" | "*"

def number: Parser[Int] = """(0|[1-9]\d*)""".r ^^ { _.toInt }


symbol is our parser of type any that states that it will contain the +,-, or * symbols using the | operator.

number is our parser of type int. Looking at it, you might get confused. It states that it will evaluate a regular expression of type int. Then you might think, "What is the ^^ symbol doing?" It denotes the operation that needs to be performed when the left-hand side expression is evaluated. So here, it is converting this expression value to integer.

def expression = {
  number ~ symbol ~ number ^^ { case firstOperand ~ operator ~ secondOperand =>
    validateAndExtractFirstValue(firstOperand) + validateAndExtractSecondValue(secondOperand)
  }


Now what is this method doing?

The ~ symbol in the Scala parser is used to separate out the token in Scala. It will parse the expression if the expression contains a number followed by a symbol followed by another number. Then it simply add those two numbers.

To be more concise, here is the full code:

import scala.util.Try
import scala.util.parsing.combinator.RegexParsers

class ScalaParser extends RegexParsers {

  def expression = {
    number ~ symbol ~ number ^^ { case firstOperand ~ operator ~ secondOperand =>
      validateAndExtractFirstValue(firstOperand) + validateAndExtractSecondValue(secondOperand)
    }
  }

  def symbol: Parser[Any] = "+" | "-" | "*"

  def number: Parser[Int] = """(0|[1-9]\d*)""".r ^^ { _.toInt }

  def validateAndExtractFirstValue(firstOperand: Any): Int = {
    val firstValue: Try[Int] = Try(firstOperand.toString.toInt)
    firstValue match {
      case util.Success(value) => value
      case util.Failure(exception) => throw new Exception("can not convert values to integer")
    }
  }

  def validateAndExtractSecondValue(secondOperand: Any): Int = {
    val secondValue = Try(secondOperand.toString.toInt)
    secondValue match {
      case util.Success(value) => value
      case util.Failure(exception) => throw new Exception("can not convert values to integer")
    }
  }
}

object TestSimpleParser extends ScalaParser {
  def main(args: Array[String]) = {
    parse(expression, "5 + 4") match {
      case Success(result, _) => println(result)
      case Failure(msg, _) => println("FAILURE: " + msg)
      case Error(msg, _) => println("ERROR: " + msg)
    }

  }
}


To be clear, to call the parser, you have to call the parse method. It takes two arguments: one is the type of parser, and the second one is the statement to be parsed.

I think this blog will help you get started with Scala parser and combinators.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
scala ,parser ,combinators ,java ,tutorial

Published at DZone with permission of Anubhav Tarar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}