Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Create Domain-Specific Languages with ANTLR : Page 6

The latest version of ANTLR provides the tools you need to build a parser for special-purpose languages.


advertisement
Putting it All Together

You now have all the basic components for SPARQL prettification in place. Specifically, you have seen how to specify a grammar to build a lexer, parser, and tree walker. You've also seen how to use StringTemplate to emit structured output. The next step is to wire these components together; the Java code for combining them fairly straightforward. First, define the expected behavior by writing an integration test:

src/test/java/com/devx/sparql/integration/SparqlPrettifierTest.java public class SparqlPrettifierTest { @Test public void shouldPrettifyOneTripleSparql() throws Exception { SparqlPrettifier prettifier = new SparqlPrettifier(); String expected = readExpectation( "select-one-triple" ); String actual = prettifier.prettify( "SELECT ?s WHERE { ?s ?p ?o}" ); assertEquals( expected, actual ); } ... }

The preceding test reads an HTML file (select-one-triple-output.html) from disk and compares that to the results of invoking the prettify() method on a SparqlPrettifier object. The SparqlPrettifier sets up all the transformation components to work together as shown below.

// src/main/java/com/devx/sparql/SparqlPrettifier.java public class SparqlPrettifier { public String prettify( String sparql ) throws Exception { ByteArrayInputStream sparqlStream = new ByteArrayInputStream( sparql.getBytes() ); ANTLRInputStream source = new ANTLRInputStream( sparqlStream ); SparqlLexer lexer = new SparqlLexer( source ); CommonTokenStream tokens = new CommonTokenStream( lexer ); SparqlParser parser = new SparqlParser( tokens ); Tree ast = (Tree) parser.query().getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream( ast ); Reader templatesIn = new InputStreamReader( getClass().getResourceAsStream( "/templates/sparql.stg" ) ); StringTemplateGroup templates = new StringTemplateGroup( templatesIn, DefaultTemplateLexer.class ); SparqlWalker walker = new SparqlWalker( nodes ); walker.setTemplateLib( templates ); return walker.query().toString(); } }

These few lines wire together each of the generated components and return the result of the prettification transformation back to the caller. Even though this simple task requires a fair amount of work, you'll find that the process is the same for larger applications such as in the construction of a DSL, and it scales well.

ANTLR is a sophisticated tool for building language recognizers. You've seen how to use ANTLR to help you build programs that can perform complex tasks such as "pretty printing." You covered a lot of ground in this article, but you'll find you can get a lot of mileage out of ANTLR just by knowing the basics. ANTLR is quite powerful; the effort you invest in learning its capabilities will pay significant dividends. We hope you'll consider ANTLR when confronted with the challenge of creating DSLs for your software project.

Related Resources


Rod Coffin is an agile technologist at Semantra, helping to develop an innovative natural language ad hoc reporting platform. He has many years of experience mentoring teams on enterprise Java development and agile practices and has written several articles on a range of topics from Aspect-Oriented Programming to EJB 3.0. Rod is a frequent speaker at user groups and technology conferences and can be contacted via his home page.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap