15.10 Jaql Assemblers


 
Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  15.   Parsing a Query Language

    Content

When you parse a user 's query, you want to create a Query object as a result. To achieve this, you set the initial assembly's target to be a QueryBuilder object. This builder collects information as you parse the user's query. You need assemblers to capture select terms, class names , and comparisons. To handle expressions, you need assemblers for arithmetic operations and variables . Finally, you need an assembler that exchanges a token on an assembly's stack with an atom that has the token's value as its functor. Altogether, the Jaql parser uses six assemblers, as Figure 15.15 shows.

Figure 15.15. Jaql assemblers. The Jaql parser uses six assemblers that help it build an engine query from a user's textual query. Jaql offloads much of its work to a separate comparison parser.

graphics/15fig15.gif

The comparison parser must use an assembler when it sees an arithmetic expression, a word, a comparison, or a variable. Most of these assemblers process elements on the stack. For example, the arithmetic assembler pops two arithmetic operands, builds an ArithmeticOperator object from them, and pushes this object. The ComparisonParser class in sjm.examples.query reuses the AtomAssembler and ArithmeticAssembler classes from sjm.examples.logic .

The assembler for variables pops a token from the stack, extracts its string, and pushes a Variable object of that name. This assembler also looks up the variable name in a ChipSpeller object (which the builder carries) and throws a runtime exception if the variable name is unknown.

The assemblers for comparisons, select terms, and class names update a query builder. They expect that the target of the assembly the parser is working on will be a QueryBuilder object. For example, the class name assembler informs the query builder that the given name is a class name to select from:

 package sjm.examples.query;  import sjm.parse.*; import sjm.parse.tokens.*; import sjm.engine.*; /**  * Pops a class name, and informs a QueryBuilder that this  * is a class to select from.  */ public class ClassNameAssembler extends Assembler { public void workOn(Assembly a) {     QueryBuilder b = (QueryBuilder) a.getTarget();     Token t = (Token) a.pop();     b.addClassName(t.sval()); } } 

The code for all the assemblers and the Jaql and Comparison parsers is on the CD in package sjm.examples.query .


   
Top


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net