A parser is an object that can recognize text and decompose its structure according to a set of
rules into a form suitable for further processing. For example, you can write a parser that will
create a machine command interpreter object that corresponds to a textual program, such as:
while StarPress1401 hasMoreMaterial
carry from StarPress1401 to UnloadBuffer1501;
When you write a parser, the set of strings it recognizes is a language . When you create
a new little computer language, you will most often create a context-free language , a pattern
of text that follows a grammar —a collection of rules that determine how you can compose
an element of the language. A parser decomposes a string according to the rules of
In a somewhat abstract sense, a language need not be a set of strings. Design Patterns uses
the word language to mean the set of possibilities that can occur by applying the set of rules
that an interpreter hierarchy models. For example, the machine command interpreter hierarchy
establishes a language: the set of all possible objects that you might compose from
the hierarchy. An IfCommand object, for example, is an element, or sentence, of this
Using this terminology, you might state the intent of I NTERPRETER as: Define a class
hierarchy that represents a grammar—a set of composition rules—and define an operation
throughout this hierarchy to interpret, or bring meaning to, instances of the set of possible
compositions. This explanation is similar to the Design Patterns statement of the intent of
I NTERPRETER : "Given a language, define a representation for its grammar along with
an interpreter that uses the representation to interpret sentences in the language" (p. 243).
If you think of the possible compositions of the machine command hierarchy as a language,
the job of a parser is to translate a textual language into an equivalent interpreter language.
Many tools perform this translation. You can search the Web for "parser generator" tools to
find the latest freeware parsers. You can also use the code that comes with Building Parsers
with Java™ (Metsker 2001). That topic's intent is to make it easier for you to create new little
You don't have to develop your own parsers to understand I NTERPRETER . You should,
however, understand why an interpreter often needs a parser.
Close this topic and write a sentence that describes the association of interpreters,
languages, and parsers.
Interpreters let you compose new commands, using the classes in a hierarchy that you create.
Each class in the hierarchy defines a way of composing commands—a grammar rule. This