Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a simple Domain Specific Language

I am curious to learn about creating a domain specific language. For now the domain is quite basic, just have some variables and run some loops, if statements.

Edit :The language will be Non-English based with a very simple syntax .

I am thinking of targeting the Java Virtual Machine, ie compile to Java byte code.

Currently I know how to write some simple grammars using ANTLR.

I know that ANTLR creates a lexer and parser but how do I go forward from here?

  • about semantic analysis: does it have to be manually written or are there some tools to create it?
  • how can the output from the lexer and parser be converted to Java byte code?
  • I know that there are libraries like ASM or BCEL but what is the exact procedure?
  • are there any frameworks for doing this? And if there is, what is the simplest one?
like image 459
Gautam Avatar asked Aug 24 '11 02:08

Gautam


2 Answers

You should try Xtext, an Eclipse-based DSL toolkit. Version 2 is quite powerful and stable. From its home page you have plenty of resources to get you started, including some video tutorials. Because the Eclipse ecosystem runs around Java, it seems the best choice for you.

You can also try MPS, but this is a projectional editor, and beginners may find it more difficult. It is nevertheless not less powerful than Xtext.

like image 122
Rui Curado Avatar answered Sep 19 '22 10:09

Rui Curado


If your goal is to learn as much as possible about compilers, then indeed you have to go the hard way - write an ad hoc parser (no antlr and alike), write your own semantic passes and your own code generation.

Otherwise, you'd better extend an existing extensible language with your DSL, reusing its parser, its semantics and its code generation functionality. For example, you can easily implement an almost arbitrary complex DSL on top of Clojure macros (and Clojure itself is then translated into JVM, you'll get it for free).

like image 40
SK-logic Avatar answered Sep 20 '22 10:09

SK-logic