10.6 Alternation Matching


 
Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  10.   Matching Mechanics

    Content

10.6 Alternation Matching

An alternation is a collection of parsers, any of which may match against an assembly. As with the Sequence class, the primary service of the Alternation class is matching, as Figure 10.5 shows.

Figure 10.5. The Alternation class. Alternation implements match() so that matching succeeds against any of the parsers in the collection of parsers it inherits from CollectionParser .

graphics/10fig05.gif

An Alternation object matches each of its subparsers against the collection of assemblies that match() receives. The Alternation object accumulates the results of each match into an output final state. Here it is in pseudocode:

 out = new state;  for each subparser p {     out = out + p.match(in); } return out; 

The actual code for alternation matching follows this design, but it walks the results of intermediate results more manually. Here is the code for Alternation.match() :

 public Vector match(Vector in) {      Vector out = new Vector();     Enumeration e = subparsers.elements();     while (e.hasMoreElements()) {         Parser p = (Parser) e.nextElement();         add(out, p.matchAndAssemble(in));     }     return out; } 

   
Top


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