10.02.2013 Views

Foundations of Software - LAMP - EPFL

Foundations of Software - LAMP - EPFL

Foundations of Software - LAMP - EPFL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A JSON parser<br />

package examples.parsing<br />

// import the standard parser class<br />

import scala.util.parsing.combinator.syntactical.StandardTokenParsers<br />

object JSON extends StandardTokenParsers {<br />

// fix some delimiter symbols ...<br />

lexical.delimiters += (”{”, ”}”, ”[”, ”]”, ”:”)<br />

// ... and some reserved words<br />

lexical.reserved += (”null”, ”true”, ”false”)<br />

// here are the four productions making up the JSON grammar<br />

def obj : Parser[Any] = ”{” ˜ repsep(member, ”,”) ˜ ”}”<br />

def arr : Parser[Any] = ”[” ˜ repsep(value, ”,”) ˜ ”]”<br />

def member : Parser[Any] = stringLit ˜ ”:” ˜ value<br />

def value : Parser[Any] = stringLit | numericLit | obj | arr |<br />

”null” | ”true” | ”false”<br />

46

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!