other bookmarks

The busy Java developer’s guide to Scala: Building a calculator, Part 2

Domain-specific languages (DSLs) have become a hot topic; much of the buzz around functional languages is their ability to build such languages. In this latest installment, Ted Neward tackles the problem of transforming textual input into the AST for interpretation by continuing with a simple calculator DSL that demonstrates the power of functional languages for building “external” DSLs. To parse textual input and transform it into the tree structure used by the interpreter in the last article, Ted introduces parser combinators, a standard Scala library designed solely for the task. (In the previous article, we built a calculator interpreter and AST.)

Scala Parser Combinators

Aug 4, 2009 Author: admin | Filed under: scala

Jim McBeath: Scala Parser Combinators

Scala Parser Combinators
One of the reasons I chose to do my StringArt applet as my first Scala applet was because Scala includes a nifty capability called parser combinators.

A parser is a function that converts a stream of input tokens into a data structure that can be more easily digested by the application. A combinator is a way of combing two functions to produce a composed function. A parser combinator is a way of combining two parsers to produce another parser.

The Parser classes in Scala include methods to combine Parsers in various ways and to add functions to be executed when a parse succeeds, to build the resulting datastructure.

Parser combinators simplify the creation of some parsers, including the simple expression parsing I wanted. When using Scala, the specification of the syntax to be parsed, which might normally be done in a separate file which is read by a separate parser (as when using a parser generator such as yacc), can be done directly in a Scala source file.

Scala’s syntax has two features that make it possible to write a syntax specification directly in Scala that still looks a lot like a BNF description.

1. (Almost) any operator string can be used as a method name.
2. A method that takes a single argument can be written without parentheses.

The parser combinator class is designed to be able to generate an abstract syntax tree (AST) representing the parse, which can then be easily manipulated by the application. In the following example, we create a parser that builds an AST which we then evaluate to produce a value.

The Magic Behind Parser Combinators

Aug 4, 2009 Author: admin | Filed under: scala

The Magic Behind Parser Combinators - Code Commit

The Magic Behind Parser Combinators

If you’re like me, one of the first things that attracted you to Scala was its parser combinators. Well, maybe that wasn’t the first thing for me, but it was pretty far up there. Parser combinators make it almost too easy to create a parser for a complex language without ever leaving the comfortable play-pen afforded by Scala. Incidentally, if you aren’t familiar with the fundamentals of text parsing, context-free grammars and/or parser generators, then you might want to do some reading before you continue with this article.
Intro to Parser Combinators

In most languages, the process of creating a text parser is usually an arduous and clumsy affair involving a parser generator (such as ANTLR, JavaCC, Beaver or ScalaBison) and (usually) a lexer generator such as JFlex. These tools do a very good job of generating sources for efficient and powerful parsers, but they aren’t exactly the easiest tools to use. They generally have a very steep learning curve, and due to their unique status as compiler-compilers, an unintuitive architecture. Additionally, these tools can be somewhat rigid, making it very difficult to implement unique or experimental features. For this reason alone, many real world compilers and interpreters (such as javac, ruby, jruby and scalac) actually use hand-written parsers. These are usually easier to tweak, but they can be very difficult to develop and test. Additionally, hand-written parsers have a tendency toward poor performance (think: the Scala compiler).

When creating a compiler in Scala, it is perfectly acceptable to make use of these conventional Java-generating tools like ANTLR or Beaver, but we do have other options. Parser combinators are a domain-specific language baked into the standard library. Using this internal DSL, we can create an instance of a parser for a given grammar using Scala methods and fields. What’s more, we can do this in a very declarative fashion. Thanks to the magic of DSLs, our sources will actually look like a plain-Jane context-free grammar for our language. This means that we get most of the benefits of a hand-written parser without losing the maintainability afforded by parser generators like bison. For example, here is a very simple grammar for a simplified Scala-like language, expressed in terms of parser combinators:

Ruminations of a Programmer: External DSLs made easy with Scala Parser Combinators

External DSLs are hard since implementing them involves reinventing most of the mechanisms found in a general purpose language. Designing internal DSLs are equally hard, more so in a statically typed language. Dynamically typed languages like Ruby offer strong meta-programming facilities, which help in implementing internal DSLs. But metaprogramming in Ruby is still considered elitist by many, and is not an art mastered by programmers at large.

Parser combinators offer a unique value here. They allow programmers to write executable grammars, in the sense that designing and implementing a DSL is almost equivalent to writing the EBNF notations in the syntax of the native language. So what really are parser combinators and what kind of language support do we need to implement parser combinator libraries ? Here is how Gilad Bracha describes them ..

The basic idea is to view the operators of BNF (or regular expressions for that matter) as methods that operate on objects representing productions of a grammar. Each such object is a parser that accepts the language specified by a particular production. The results of the method invocations are also such parsers. The operations are called combinators for rather esoteric technical reasons (and to intimidate unwashed illiterates wherever they might lurk).

Combinators have their theoretical underpinnings in functional programming. A parser combinator is a higher order function that accepts a parser and applies transformation functions to generate more complex parsers. Hence parser combinators are easily implemented in languages that have strong support for functional programming. In Scala, parsers are implemented as monads - hence defining combinators for parsers are just monadic transformations implementing sequencing, alternation or any other composition operations.

This post is not an introductory material on parser combinators or their implementations in Scala. Here I would like to narrate my experience in designing an external DSL for a financial application using the parser combinator library of Scala. In one of my earlier posts, I had talked about monads as abstractions for containers and computations. Parser combinator implementation in Scala is a great example of the power of monads in evolving a DSL.

In developing a financial application involving securities trade and settlement processing, we’ve been using XML as the DSL for getting buy/sell orders from client, trade/execution information from the exchange and settlement information from clearing agencies. Needless to say, XML processing is one of the key functions that pervade our codebase. In one of my very early posts, I had ranted about executable XMLs (aka Lisp) and had considered using Scheme as the DSL for securities trade processing operations. SISC offers a fully compliant Scheme implementation on top of the JVM, still all ideas fell through the cracks of enterprise decision making deliberations. After a fairly long time, I have found out another alternative - DSLs designed using Scala parser combinators ..

ScalaModules

Aug 4, 2009 Author: admin | Filed under: scala

Home - scalamodules - GitHub

Home

ScalaModules aims at Scala-based OSGi development. The mission of ScalaModules is to employ the power of the Scala programming language to ease OSGi development.

Of course using Scala for OSGi will itself be beneficial, because of the great simplifications Scala brings compared to Java. But ScalaModules will also make use of the additional possibilities offered by Scala, mainly the chance to create a Domain Specific Language.

Therefore with ScalaModules your code will be more intuitive and concise as well as less verbose and less involved compared to Java-based OSGi development. Check it out!

blog - Heiko Seeberger - osgi expert & scala entusiast

Aug 4, 2009 Author: admin | Filed under: scala

Heiko Seeberger

Heiko Seeberger

Welcome to my blog!

SBT: A Scala-based Simple Build Tool

Aug 3, 2009 Author: admin | Filed under: scala

Brizzled: SBT: A Scala-based Simple Build Tool

Awhile ago, I embarked on an effort to build yet another build tool, this one in Scala. I shelved that effort, and I’ve switched to SBT, instead.

I’ve had it with Ant, for all the reasons outlined in my previous build tool discussion, and then some.

I looked at Maven and Ivy, but, really, I am so over XML configuration files. (Again, see previous rant.)

At one suggestion, I began to look at Gradle, a Groovy build tool with Maven, Ivy and Ant integration. Now this was looking promising: A simple code-oriented configuration (no XML! Yay!), with complete access to Ant tasks and full external dependency management.

Before diving feet-first into Gradle, though, I decided to take a closer look at SBT, a tool Daniel Spiewak recommended. SBT bills itself as “a simple build tool for Scala projects that aims to do the basics well.” SBT uses Scala, much the same way that Gradle uses Groovy; this is appealing to me, since I’m doing most of my “elective programming” in Scala these days. After digging a bit, I found that SBT has complete support for external dependency management.

java centura neagra :)

Jul 29, 2009 Author: admin | Filed under: Uncategorized

JavaBlackBelt - Training and Skills Management in Java Hibernate Spring

buildr - ant maven replacement

Jul 24, 2009 Author: admin | Filed under: Uncategorized

buildr — Welcome

Apache Buildr is a build system for Java-based applications, including support for Scala, Groovy and a growing number of JVM languages and tools. We wanted something that’s simple and intuitive to use, so we only need to tell it what to do, and it takes care of the rest. But also something we can easily extend for those one-off tasks, with a language that’s a joy to use. And of course, we wanted it to be fast, reliable and have outstanding dependency management.

Here’s what we got:

* A simple way to specify projects, and build large projects out of smaller sub-projects.
* Pre-canned tasks that require the least amount of configuration, keeping the build script DRY and simple.
* Compiling, copying and filtering resources, JUnit/TestNG test cases, APT source code generation, Javadoc and more.
* A dependency mechanism that only builds what has changed since the last release.
* A drop-in replacement for Maven 2.0, Buildr uses the same file layout, artifact specifications, local and remote repositories.
* All your Ant tasks are belong to us! Anything you can do with Ant, you can do with Buildr.
* No overhead for building “plugins” or configuration. Just write new tasks or functions.
* Buildr is Ruby all the way down. No one-off task is too demanding when you write code using variables, functions and objects.
* Simple way to upgrade to new versions.
* Did we mention fast?

724 clone :))

Jul 17, 2009 Author: admin | Filed under: Uncategorized

Dailymix

Cine suntem?

O mica echipa de dezvoltatori, cu multe palarii pe cap: programatori, matematicieni, statisticieni, testeri, marketeri si nu in ultimul rand antreprenori.
Lucram de un an de zile la siteul acesta si la softul din spatele lui (practic mai mult la soft decat la site), si credem ca am reusit sa obtinem un produs care raspunde destul de bine unei probleme des intalnite.
Ce facem?
Cautam solutia la o problema: cum gasesc ce este relevant pentru mine in presa romaneasca?

Am stat, ne-am gandit, am studiat, am scris cod, ne-am certat, am re-scris cod, am imbunatatit, am sters, am refacut. Rezultatul unui an de procese continue este acest site, si softul din spatele lui, care lucreaza zilnic pentru a da un raspuns cat se poate de corect la aceasta intrebare fundamentala.

Pentru a va putea furniza aceste date serviciul cerceteaza si proceseaza, pe cat de corect posibil, un numar mare de surse prezente online. Cat de curand vom mai adauga si alte surse, imbogatind astfel aria de expertiza a motorului de cautare.

Serviciul de monitorizare al presei este oferit de newsmix.ro

Advertising


Most Popular

  • None found

Recent Comments

  • None found