Domain-specific languages

A language developed for expressing solutions to problems that are specific to a domain such as finance, payroll, electronic circuit design, parser generators, input validations, and so on is called a domain-specific language (DSL). Two common examples that are familiar to most programmers are Structured Query Language (SQL) and regular expression (RE). If we were to write imperative code for retrieving data from a database, it would have been a difficult task and error prone. SQL gives you a declarative language to achieve the same objective, and it has solid mathematical foundations. While searching strings, RE helps us to give complicated patterns to match against a string. It helps to avoid writing tedious logic for searching complicated string matches.

As a concrete example of a DSL, which is quite popular in the .NET and Java world, we are pasting here a specification given to the ANTLR tool to write a very simple arithmetic evaluator. The tool generates a lexical analyzer and parser automatically from this specification. Please consult the ANTLR documentation to understand the semantics of the following script:

    grammar Evaluator; 
 
    options { 
      language=CSharp3; 
    } 
 
    @lexer::namespace{AntlrExample} 
    @parser::namespace{AntlrExample} 
   
    public addop 
    : mulop (( '+' | '-' ) mulop)*; 
  
    mulop 
    : INTEGER (( '*' | '/' ) INTEGER)*; 
    /* 
      * Lexical Analysis Rules 
    */ 
 
    INTEGER : '0'..'9'+; 
    WS :  (' '|'	'|'
'|'
')+ {Skip();} ; 

The ANTLR tool will generate a lexical analyzer, a parser module, and even a tree walker to process the expression. We can write parsers for C#, Java, and even C/C++ using this tool. In the Unix and the Windows native programming world, the tool Lex (GNU Flex) and Yacc (GNU Bison) is used for the same purpose.

As another case in point, authors worked in a DSL project, which evaluated a spreadsheet for consistency. A code snippet from the turing complete DSL generated for the project is given here:

    Boolean range_flag; 
    NUMERIC sum_value; 
 
    BEGIN  
 
    //--- See whether B3 cell in the CONTROL worksheet 
    //--- present in the Relational database 
    VALIDATE  LOOKUP($(CONTROL.B3), 
    @(EI_SEGMENT_MAPPING.EI_SEGMENT_CODE))==TRUE; 
 
    //---- The Sum of Range should be 100 or Zero 
    range_flag = SUM_RANGE($(HOURLY_DISTRIBUTION.C2), 
    $(HOURLY_DISTRIBUTION.C25))  == 100.0; 
    range_flag =  range_flag || 
    SUM_RANGE($(HOURLY_DISTRIBUTION.C2), 
    $(HOURLY_DISTRIBUTION.C25)) == 0.0; 
 
    //--- If false throw exception 
    VALIDATE range_flag; 
 
    EXCEPTION 
    //----- on Exception execute this 
    ErrorString = "FAILED WHILE VALIDATING CONTROL Sheet Or" 
    ErrorString = ErroString +  
    " WorkSheet Error in the range C2-C25"; 
    WRITELOG  ErrorString; 
    bResult = false; 
 
    END 

The preceding code snippet was parsed using a hand-coded recursive descent parser, and an abstract syntax tree (AST) was generated. The tree was traversed in a depth-first manner to generate a .NET IL code to generate a .NET assembly (DLL). The resulting assembly was consumed as if the logic were written in C#!

The Extensible Stylesheet Language (XSL) is another domain-specific language, which is very much familiar to most programmers. A small XSL snippet is given here for the sake of completeness, and readers are expected to master this language for generating sites with variant layouts:

    <?xml version="1.0"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
      <xsl:output method="xml"/> 
      <xsl:template match="*"> 
        <xsl:element name="{name()}"> 
          <xsl:for-each select="@*"> 
            <xsl:element name="{name()}"> 
              <xsl:value-of select="."/> 
            </xsl:element> 
          </xsl:for-each> 
          <xsl:apply-templates select="*|text()"/> 
        </xsl:element> 
        </xsl:template>  
    </xsl:stylesheet> 

The other popular DSLs are VHDL and Verilog (Digital Hardware Description Language), CSS, and template languages available with different technology stacks, JBoss Drools, and so on.

Writing and designing one's own DSL is a very vast topic, which cannot be covered in a short chapter. Some things that one needs to take care of are as follows:

  • Designing an object model, which mimics domain concerns
  • Designing a linguistic abstraction and associated key words for arriving at language elements and rules for composing elements to form compound elements
  • Mapping language elements to the object model created
  • Deciding whether the DSL should be internal or external?
  • Deciding whether the DSL should be turing complete or not

The topic is well covered by Debasish Ghosh and Martin Fowler/Rebecca Parsons in their wonderful books. Please consult the following books for a thorough understanding of the same:

  • DSLs in Action by Debashish Ghosh ( Manning)
  • Domain-Specific Languages by Martin Fowler and Rebecca Parsons (Addison Wesley Martin Fowler signature series)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.221