Search:
|
Access:
» Using ANTLR Studio to develop ANTLR grammarsRelated categories: C/C++ | Programming Tools Prashant DevaViewed: 3979 | Article date: 2006-07-18 14:45:40 ANTLR is a tool for generating parsers, lexers and tree parsers. Of course you can write these on your own in your favorite programming language but that would take an extremely long time and not to mention the number of mistakes you will make coupled with the fact that it would be extremely hard to understand the grammar of your parser/lexer.
ANTLR is a tool for generating parsers, lexers and tree parsers. Of course you can write these on your own in your favorite programming language but that would take an extremely long time and not to mention the number of mistakes you will make coupled with the fact that it would be extremely hard to understand the grammar of your parser/lexer.
About the authorPrashant Deva is the creator of ANTLR Studio and the founder of Placid Systems. Contact author at: pdeva@placidsystems.com Thus it is far more convenient to write a grammar for the language you want to parse and let a parser generator tool generate the relevant code for you. There are also other tools like lex, yacc, etc which can generate parsers/lexers for you, but ANTLR stands out in that it generates code which is extremely easy to read and has an advanced IDE in ANTLR Studio which makes writing grammars extremely easy. Writing ANTLR grammars is generally considered to be a slow, tedious and difficult process, done only by 'real' programmers. And why shouldn't it be? Look at all the stuff you gotta 'manually' do. First of all there used to be no proper ide for this kind of stuff, so you bring out your trusty old notepad, if you are using windows or emacs, if you are using linux and type away your grammar. Of course there is no syntax highlighting, auto complete, content outline or any of the frills present in your regular java/c++ ides. Add to this the fact that you have to manually build the grammar file each time you make a mistake and there is absolutely no proper way to debug the grammar and you might as well code the parser/lexer in your favorite programming language (e.g., java/c/c++), after all you get all the benefits of your IDE, even though you may have have to write a few thousand extra lines of code! But all this is about to change with ANTLR Studio, an IDE for ANTLR which completely integrate with the Eclipse development environment. ANTLR Studio was built with the problem of grammar development being disconnected from the main development environment in mind, so it goes to great lengths to provide a highly integrated experience. And to prove this we are going to build a simple app using ANTLR in ANTLR Studio. The CalculatorThe windows calculator although looks very friendly at a first glance, has many things wrong about it. Firstly, if you find a mathematical expression written somewhere, you can't just copy/paste it into the calculator. You need to type out the entire expression by hand. Also if it is a long expression, sometimes you might just forget which part you are on, cause the calculator doesn't show you the whole expression, it just shows you the latest result of the expression you have typed until now. So if you by mistake entered a wrong digit while typing out that long expression, you can never find out and your result will be wrong without you even knowing about it.
Figure 1. Windows Calculator, although its interface looks simple at first, it can get your way when you are actually working with it So we are going to create a new calculator in Eclipse using ANTLR Studio, as shown in Fig 2. You can just type in or copy/paste an entire mathematical expression in the text box and press the calculate button to see the answer. This calculator doesn't have any of the problems mentioned above. To keep things simple for this tutorial, we will just allow the multiply and add operators inside expressions.
Figure 2. Our calculator, simple interface which allows you to see the whole expression at once When the calculate button is clicked, the calculator parses the expression in the text box and makes a tree out of it. It then walks the tree to evaluate the expression. The java portion of this app is trivial and irrelevant to our tutorial, so we will just focus on the ANTLR grammar, which contains the meat of the code. The Parser
Figure 3. ANTLR Studio automatically puts the name of the package when you create a new grammar file In the Package Explorer, right-click on a package where you want to place the grammar and select 'New->ANTLR Grammar'. Type the name of the grammar file in the box that pops up and press 'Ok'. A new grammar file is created for you with the header section containing package declaration automatically put it. Type the following parser grammar into the editor. Notice that the TypeOnce feature of the editor automatically shows completions without the need to press [Ctrl+Space] making typing the grammar extremely fast.
|
|
Copyright C 2006 by Software Developer's Journal. All rights reserved.









SDJ Users:
Shopping Cart









