concept grammar in category dsl

appears as: grammars, grammar, The grammar, grammars, grammar, The grammar
DSLs in Action

This is an excerpt from Manning's book DSLs in Action.

DSL implementation is language design, and language design is a complex task that doesn’t scale. Instead of starting anew with the complexities of the lexers and grammars of your language, most DSLs are implemented as an embedding within a higher-level language. Still, it’s complex enough and is definitely not an exercise to be undertaken by nonexpert programmers. Later chapters cover language features and their suitability for implementing embedded DSLs.

We’ll start with the grammar as we did earlier. If you went through the last DSL that we designed using parser combinators in Scala in section 8.4, the grammar in listing 8.7 will be perfectly clear to you.

8.3.1. Step 1: Executing the grammar

If you look at the grammar that we designed in listing 8.2, you’ll see it defines the complete syntax of our order-processing DSL. It successfully parses the DSL snippet that I included immediately after the listing. The program in the following listing processes our DSL script and generates the output for a successful parse.

Listing 8.4. Running the DSL processor

We invoked the parser order , which is the topmost abstraction for our DSL grammar (see the grammar in listing 8.2). If the DSL script that we provide produces a successful parse, we print the output ; otherwise, we print whatever error message the parser generates. Printing only the default output of the parser looks too trivial and not meaningful for processing in our application. In the next section, I’ll show you how you can generate a semantic model of your choice from the process of parsing the language. But for now, can you guess what’ll be the output of the print statement ?