Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I need to develop a simple declarative language to drive an application. I have various computational modules, some of them depending on other modules which also need setup. The problem is that I don't know how to manipulate the keywords. I will explain myself with an example

Task optimize
Units meters
System {
   // input data
}

Optimizer {
     type Simplex
     convergenceCriteria 0.001
}

PointEvaluator {
     type MyEvaluatorTechnique
     convergenceCriteria = 0.1
}

this is a solution, which has header very generic entities which describe the meaning of each section, but I could also have sections that explicity concern specific techniques

Task {
     type optimize
     optimizer Simplex
}
Units meters
System {
   // input data
}

Simplex {
     convergenceCriteria 0.001
     PointEvaluator MyEvaluatorTechnique
}

MyEvaluatorTechnique {
     convergenceCriteria = 0.1
}

I would like to hear your opinion on which method may sound better in terms of design correctness, and pros and cons of both solutions. One thing I don't like in the first solution is, for example, the fact that depending on the type, I may have options that do not make sense for that specific type. In the second solution, however, I am setting up not the generic task (which then uses specific types of subsystem). Instead, I specify the specific subsystems performing the task.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Designing DSL's is hard.

So hard, that I suggest that you avoid it until you are compelled to create the DSL.

My suggestion is this.

  1. Create a proper class hierarchy.

  2. Create pleasant, easy-to-use initializers and constants.

  3. Get things to work as simple object construction.

Later, after things work, and after you see what the DSL must express, consider designing a DSL.

Some class Definitions.

class  Task( object ):
    pass

class Optimizer( Task ):
    def __init__( self, optimizer ):
        pass

class Simplex( object ):
    def __init__( self, evaluator ):

class Evaluator( object ):
    def __init__( self, convergence ):
         pass

class MyEvaluatorTechnique( Evaluator ):
    pass

A Configuration

config = Optimizer( 
    Simplex(), 
    MyEvaluatorTechnique( convergence=0.001 ) 
)

This avoids a lot of complexity of writing a parser and handling keywords. Instead, you use the parser for another language (i.e. Python or Java or Lua or something)

share|improve this answer
    
I read your blog post on this same topic. I did realize that declarative style is hard, and we are in fact probably planning to switch to an imperative style. For the moment, however, I must implement a declarative style. –  Stefano Borini Feb 16 '11 at 17:08
    
+1 for Lua. This use case is more-or-less exactly what Lua was designed for from the ground up. That it is a pretty excellent and fast little language is a real bonus. –  Adam Crossland Feb 16 '11 at 17:14
    
+1 for Lua idem. Even older versions, like Lua 4 could handle these type of requirements easy. –  Machado Feb 16 '11 at 17:20
    
@Stefano Borini: This is declarative. –  S.Lott Feb 16 '11 at 17:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.