DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Introduction to the Scala Parser and Combinators

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.

Anubhav Tarar user avatar by
Anubhav Tarar
·
Mar. 09, 17 · Tutorial
Like (6)
Save
Tweet
Share
20.95K Views

Join the DZone community and get the full member experience.

Join For 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.

Parser (programming language) Scala (programming language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Real Democratization of AI, and Why It Has to Be Closely Monitored
  • Silver Bullet or False Panacea? 3 Questions for Data Contracts
  • Connecting Your Devs' Work to the Business
  • Data Engineering Trends for 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: