Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scala Parser Combinators - Scalapeno Lightning Talk

139 views

Published on

Slides from my Scalapeno 2018 lightning talk

Published in: Software
  • Be the first to comment

  • Be the first to like this

Scala Parser Combinators - Scalapeno Lightning Talk

  1. 1. Why? How? - DSLs Everywhere - Also parsing in general - Internal vs. External Lots of options for parsing - String.split - RegEx - Hand-Written - Parser Generators From simple and fragile to robust and complex Goal: Robust, but easier to create (and in Scala…)
  2. 2. Parser Combinators The “functional way” of writing a parser A parsing function: Character Stream → Result[Character Stream, T] Combine functions into more complicated patterns - Sequence - Choice - ... Not only in Scala; but we’ll focus on the standard Scala Parser Combinators.
  3. 3. Scala Parser Combinators - Simple Example Input: A line consisting of a word (string) and its count (number), e.g. “johnny 12” case class WordFreq(word: String, count: Int) { override def toString = "Word <" + word + "> " + "occurs with frequency " + count } And we’d like to use it in a program: object TestSimpleParser extends SimpleParser { def main(args: Array[String]) = { parse(freq, "johnny 12") match { case Success(matched,_) => println(matched) case Failure(msg,_) => println("FAILURE: " + msg) case Error(msg,_) => println("ERROR: " + msg) } } }
  4. 4. Scala Parser Combinators - Simple Example Then a possible parser is: class SimpleParser extends RegexParsers { def word: Parser[String] = """[a-z]+""".r ^^ { _.toString } def number: Parser[Int] = """(0|[1-9]d*)""".r ^^ { _.toInt } def freq: Parser[WordFreq] = word ~ number ^^ { case wd ~ fr => WordFreq(wd,fr) } } The basic pattern: - Defining functions for parsing simple strings - Map matched strings into more meaningful object model - Combine results into more complex structures
  5. 5. Actimize Profiling Language Context: - A data profiling engine - Aggregations, functions, metadata - Highly customizable by clients, professional services, etc. - Existing interface: XML configuration files
  6. 6. Defining a Profile
  7. 7. Model How a profile object model should “ideally” look like. POSOs
  8. 8. Parser Defines the whole syntax in one class ~200 lines
  9. 9. Summary Where? Parsing – DSLs and Beyond Why? Building a robust parser is complicated How? Scala Parser Combinators

×