Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding a parser for a domain specific language in Java

Tags:

java

parsing

dsl

We want to design a simple domain specific language for writing test scripts to automatically test a XML-based interface of one of our applications. A sample test would be:

  • Get an input XML file from network shared folder or subversion repository
  • Import the XML file using the interface
  • Check if the import result message was successfull
  • Export the XML corresponding to the object that was just imported using the interface and check if it correct.

If the domain specific language can be declarative and its statements look as close as my sentences in the sample above as possible, it will be awesome because people won't necessarily have to be programmers to understand/write/maintain the tests. Something like:

newObject = GET FILE "http://svn/repos/template1.xml"
reponseMessage = IMPORT newObject
newObjectID = GET PROPERTY '/object/id/' FROM responseMessage
(..)

But then I'm not sure how to implement a simple parser for that languange in Java. Back in school, 10 years ago, I coded a language parser using Lex and Yacc for the C language. Maybe an approach would be to use some equivalent for Java?

Or, I could give up the idea of having a declarative language and choose an XML-based language instead, which would possibly be easier to create a parser for? What approach would you recommend?

like image 566
b.roth Avatar asked Mar 08 '10 11:03

b.roth


People also ask

How do you write parser in Java?

The first step in writing a parser is to tokenize the input string. This means to separate the input string into short bits that represent the basic entities in the expression. We could do this by hand, reading one character at a time and assembling the tokens character by character.

What is domain specific language in Java?

A Domain Specific Language is a programming language with a higher level of abstraction optimized for a specific class of problems. A DSL uses the concepts and rules from the field or domain.

What is domain specific language examples?

Domain-specific languages have been talked about, and used for almost as long as computing has been done. DSLs are very common in computing: examples include CSS, regular expressions, make, ant, SQL, many bits of Rails, expectations in JMock, graphviz's dot language, strut's configuration file....


2 Answers

You could try JavaCC or Antlr for creating a parser for your domain specific language. If the editors of that file are not programmers, I would prefer this approach over XML.

like image 81
Péter Török Avatar answered Sep 20 '22 15:09

Péter Török


Take a look at Xtext - it will take a grammar definition and generate a parser as well as a fully-featured eclipse editor pluging with syntax highlighting and -checking.

like image 29
Michael Borgwardt Avatar answered Sep 18 '22 15:09

Michael Borgwardt