The Artima Developer Community
Sponsored Link

Java Community News
Martin Fowler Takes ANTLR for a Test Drive

2 replies on 1 page. Most recent reply: Mar 8, 2007 12:20 PM by Dipesh Khakhkhar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Martin Fowler Takes ANTLR for a Test Drive Posted: Mar 7, 2007 12:20 PM
Reply to this message Reply
Summary
ANTLR is an open-source Java compiler-compiler. In a recent blog post, Martin Fowler creates a simple domain-specific grammar and uses Antlr to parse code written in that grammar in support of Java code generation.
Advertisement

Automatic code generation from a domain-specific language is one of the ways developers can gain efficiency: instead of hand-coding repetitious code fragments, a developer can define a concise target language and then use software to generate full-fledged application code from code written in the domain-specific language, or DSL.

A key requirement for code generation is the ability to parse DSL code, and there are several high-quality parsers and compiler-compilers to choose from. Martin Fowler recently followed up on an earlier tutorial on SableCC with a test drive of ANTLR in HelloAntlr, noting that:

Like SableCC, ANTLR is a compiler-compiler tool. It's been around for a while, and I've run into a few projects that use it. Unlike SableCC (and the venerable lex/yacc combo) it generates a recursive descent parser using LL grammars. Compiler heads like to argue about whether LL or LALR are better, I'll not step into that debate here...

In the brief article, Fowler defines a simple grammar, and uses a combination of Ant and Java to parse files written in that grammar:

ANTLR works with a grammar file... [that] defines the productions in the grammar and also actions that the parser takes when it encounters productions. The grammar file also defines the lexer (you can split them if you want). In this sense ANTLR is more traditional (and flexible) than SableCC.... ANTLR allows arbitrary actions, or it supports building a tree in the same manner as SableCC... Antlr also uses a grammar file to walk the tree.

About ANTLR's advantages, Fowler writes that:

Antlr has a nice IDE called AntlrWorks that can plug into IntelliJ. The tool will give you syntax highlighting and completion on grammar elements, plot syntax diagrams for your grammar, and allow you to enter test fragments to parse - displaying the resulting parse tree. It's a very helpful tool to see what the parser is doing. However there's no highlighting/completion for the code inside actions, which is an understandable pain.

Another good feature of ANTLR is the fact that there is a decent book on it in the works. The book gives detailed coverage of how the tool works and useful background on language and compiler principles. It does assume you're working on a full blown language and that you'll be generating code...

What are your favorite tools for working with DSLs in Java?


Alex Stojan

Posts: 95
Nickname: alexstojan
Registered: Jun, 2005

Re: Martin Fowler Takes Antlr for a Test Drive Posted: Mar 7, 2007 3:02 PM
Reply to this message Reply
I wrote a few compilers (for _virtual_ machines) using ANTLR generating Java and C++ (I just posted one demo on their Web site at www.antlr.org couple of days ago). I haven't used other such tools (except PCCTS, ANTLR's predecessor, when I was in graduate school), but one thing that I really like about ANTLR is that it's based on LL grammars, since they are much easier to understand than LALR (or other shift-reduce based grammars). Also, the AST matcher is extremely useful for code analysis and generation because you don't have to write your own code to traverse the tree, using something like ... node->left ... node->right ..., etc. For example, if you have an expression like 1 + 2 * 3, you can tell the parser to produce trees like (+ 1 (* 2 3)). Then you can recursively match such expressions with notation expr : #(OPERATOR expr expr) while doing some analysis or generating code by specifying actions on tree nodes. There's lots of other functionality. Really good stuff :)

Dipesh Khakhkhar

Posts: 1
Nickname: dipesh
Registered: Mar, 2007

Re: Martin Fowler Takes Antlr for a Test Drive Posted: Mar 8, 2007 12:20 PM
Reply to this message Reply
Where does JavaCC stand compared to ANTLR and sable?

Flat View: This topic has 2 replies on 1 page
Topic: Relo Project's Vineet Sinha on Understanding Large Code Bases Previous Topic   Next Topic Topic: Brett McLaughlin: What is XML Really Good For?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use