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.

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; ...

Get Building Parsers with Java™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.