|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
General Java Java Developer's Journal: A Blueprint For Developing Language Tools
A proven approach to making them modular, extensible, and maintainable
By: Paul Mukherjee
Oct. 16, 2005 11:30 AM
Language tools such as compilers, interpreters, and code generators are a critical part of the software development landscape. Any software project will include several procured tools and very likely several in-house tools. Experience shows that the only guarantee with such tools is change: the underlying language may change due to improvements or extensions and the functionality provided by the tool expands, driven by user-requested features and the need to stay in front of the competition. The specific changes that will be made are rarely known at the outset, but change is coming.
This article describes a proven approach to developing language-based tools in a way that is modular, extensible, and maintainable. The approach is based on two principles: establishing the core modules at the outset and using the visitor pattern to interact with language sentences. To illustrate the approach a simple calculator example is given. This calculator supports the addition, subtraction, multiplication, and division of integers. The language supported by this calculator is informally described below:
expression = constant | This language is very simple, but it's sufficient to illustrate the main concepts related to the development of language tools. The example is developed in Java based on the JavaCC parser generator. However the concepts presented are language-independent and apply equally to C++ and C#. All of the code shown in this article is available for download. A key objective when developing a language tool is to ensure that the tool isn't dependent on the actual representation of the language. This allows simple support for multiple language formats, imports from other tools, and so on. To meet this objective, a distinction is made between concrete and abstract syntax. This is described below. After this the notion of context is introduced followed by a description of the actual mechanism for parsing. Then the use of the generated parse tree is explained.
Concrete Syntax Note that normally semantic actions would be included in such a JavaCC description. These are presented later in the article. In general there can be several concrete syntaxes for a language (plain text, XML, RTF, etc.). The tool design should be sufficiently flexible to support multiple concrete syntaxes (as well as the ability to add further concrete syntaxes) without having a major impact on the rest of the tool.
Abstract Syntax
Notice that the package arrangement in Figure 1 follows the convention used in Eclipse that dictates that classes in a package named intern are not to be exposed to other tools.
Context Information The solution to this problem is to associate each abstract syntax node with an object defining the context of that node. This might, for example, be the start and end line and column for the node; the information required here may vary according to the nature of the language, the tool, and the concrete format in question. As with the abstract syntax, client tools should be shielded from the implementation details of context information, so an interface is used and classes implementing this interface are internal. It's possible that there may be multiple context information classes according to the concrete representation. This is suggested in Figure 2 where a plain text context information class is used.
Parsing This example is based on JavaCC, but the principle applies to other parser generators: the semantic actions in the matching rules in the parser definition are used to create and instantiate abstract syntax objects, resulting in the creation of an abstract syntax tree corresponding to the input text. This abstract syntax tree will be the input to other components in the tool that require the input text. Reader Feedback: Page 1 of 1
Your Feedback
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||