4.3 Special Tokenizers and Targets

Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  4.   Testing a Parser


If your parser needs a special tokenizer or target, you can create a new subclass of ParserTester and override the assembly() and freshTarget() methods . For example, the SlingParser class in Chapter 16, "Parsing an Imperative Language," uses a special tokenizer that recognizes reserved words. This parser class also requires an assembly to have as its target a SlingTarget object. To provide for these special target and tokenization needs, the class SlingTester in sjm.examples.sling overrides the freshTarget() and assembly() methods of its superclasses. Here is SlingTester.java :

 package sjm.examples.sling;  import sjm.parse.*; import sjm.parse.tokens.*; import sjm.examples.sling.*; /**  * This class tests that class <code>SlingParser</code> can  * parse random language elements.  */ public class SlingTester extends TokenTester { /**  * Create a tester for the primary Sling parser.  */ public SlingTester() {     super(new SlingParser().statement()); } /*  * Return an assembly for a given string, using the Sling  * tokenizer.  */ protected Assembly assembly(String s) {     Tokenizer t = new SlingParser().tokenizer();     t.setString(s);     return new TokenAssembly(t); } /*  * The Sling parser expects a SlingTarget object as an  * assembly's target. Normally, this target expects two  * sliders to be available, but we send nulls here because  * there is no GUI in use during random testing.  */ protected sjm.utensil.PubliclyCloneable freshTarget() {     return new SlingTarget(null, null); } /**  * Run a test.  */ public static void main(String[] args) {     new SlingTester().test(); } } 

A sample run of this class prints the following (with some whitespace changes):

 Testing depth 2...      Testing string for (vrnjex, (pi), -t) {gnu = (t);}     Testing string for (zllciub, s2, (s2)) {uuezrpu = (pi);}     Testing string plot (3.3);     Testing string plot ceil (-hrnpb);     Testing string plot ceil (-random);     Testing string for (umq, -s2, ((-t))) {         for (iojf, schq, random) {hrbr = - t;}}     Testing string plot ((s2));     ... 

Although an explanation of the Sling language lies ahead, you can probably imagine that these are not normal-looking Sling programs. One strength of random testing is that it produces valid language elements that you might not create in your feature testing. These elements include strings that perhaps no one would ever try but also include types of strings that your users might think of that you would not. Random testing is an important complement to feature testing of a parser.


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

Similar book on Amazon

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