|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--sjm.parse.Parser
A Parser
is an object that recognizes the
elements of a language.
Each Parser
object is either a
Terminal
or a composition of other parsers.
The Terminal
class is a subclass of
Parser
, and is itself a hierarchy of
parsers that recognize specific patterns of text. For
example, a Word
recognizes any word, and a
Literal
matches a specific string.
In addition to Terminal
, other subclasses of
Parser
provide composite parsers,
describing sequences, alternations, and repetitions of
other parsers. For example, the following
Parser
objects culminate in a good
parser that recognizes a description of good
coffee.
This prints out:Alternation adjective = new Alternation(); adjective.add(new Literal("steaming")); adjective.add(new Literal("hot")); Sequence good = new Sequence(); good.add(new Repetition(adjective)); good.add(new Literal("coffee")); String s = "hot hot steaming hot coffee"; Assembly a = new TokenAssembly(s); System.out.println(good.bestMatch(a));
The parser does not match directly against a string, it matches against an[hot, hot, steaming, hot, coffee] hot/hot/steaming/hot/coffee^
Assembly
. The
resulting assembly shows its stack, with four words on it,
along with its sequence of tokens, and the index at the
end of these. In practice, parsers will do some work
on an assembly, based on the text they recognize.
Field Summary | |
protected Assembler |
assembler
|
protected java.lang.String |
name
|
Constructor Summary | |
Parser()
Constructs a nameless parser. |
|
Parser(java.lang.String name)
Constructs a parser with the given name. |
Method Summary | |
void |
accept(ParserVisitor pv)
Accepts a "visitor" which will perform some operation on a parser structure. |
abstract void |
accept(ParserVisitor pv,
java.util.Vector visited)
Accepts a "visitor" along with a collection of previously visited parsers. |
static void |
add(java.util.Vector v1,
java.util.Vector v2)
Adds the elements of one vector to another. |
Assembly |
best(java.util.Vector v)
Returns the most-matched assembly in a collection. |
Assembly |
bestMatch(Assembly a)
Returns an assembly with the greatest possible number of elements consumed by matches of this parser. |
Assembly |
completeMatch(Assembly a)
Returns either null, or a completely matched version of the supplied assembly. |
static java.util.Vector |
elementClone(java.util.Vector v)
Create a copy of a vector, cloning each element of the vector. |
java.lang.String |
getName()
Returns the name of this parser. |
abstract java.util.Vector |
match(java.util.Vector in)
Given a set (well, a Vector , really) of
assemblies, this method matches this parser against
all of them, and returns a new set (also really a
Vector ) of the assemblies that result from
the matches.
|
java.util.Vector |
matchAndAssemble(java.util.Vector in)
Match this parser against an input state, and then apply this parser's assembler against the resulting state. |
protected abstract java.util.Vector |
randomExpansion(int maxDepth,
int depth)
|
java.lang.String |
randomInput(int maxDepth,
java.lang.String separator)
Return a random element of this parser's language. |
Parser |
setAssembler(Assembler assembler)
Sets the object that will work on an assembly whenever this parser successfully matches against the assembly. |
java.lang.String |
toString()
Returns a textual description of this parser. |
protected java.lang.String |
toString(java.util.Vector visited)
Returns a textual description of this parser. |
protected abstract java.lang.String |
unvisitedString(java.util.Vector visited)
|
Methods inherited from class java.lang.Object |
|
Field Detail |
protected java.lang.String name
protected Assembler assembler
Constructor Detail |
public Parser()
public Parser(java.lang.String name)
String
- A name to be known by. For parsers
that are deep composites, a simple name
identifying its purpose is useful.Method Detail |
public void accept(ParserVisitor pv)
ParserVisitor
- the visitor to acceptpublic abstract void accept(ParserVisitor pv, java.util.Vector visited)
ParserVisitor
- the visitor to acceptVector
- a collection of previously visited
parsers.public static void add(java.util.Vector v1, java.util.Vector v2)
v1
- the vector to add tov2
- the vector with elements to addpublic Assembly best(java.util.Vector v)
Vector
- the collection to look throughpublic Assembly bestMatch(Assembly a)
Assembly
- an assembly to match againstpublic Assembly completeMatch(Assembly a)
Assembly
- an assembly to match againstpublic static java.util.Vector elementClone(java.util.Vector v)
in
- the vector to copypublic java.lang.String getName()
public abstract java.util.Vector match(java.util.Vector in)
Vector
, really) of
assemblies, this method matches this parser against
all of them, and returns a new set (also really a
Vector
) of the assemblies that result from
the matches.
For example, consider matching the regular expression
a*
against the string "aaab"
.
The initial set of states is {^aaab}
, where
the ^ indicates how far along the assembly is. When
a*
matches against this initial state, it
creates a new set {^aaab, a^aab, aa^ab,
aaa^b}
.
Vector
- a vector of assemblies to match againstpublic java.util.Vector matchAndAssemble(java.util.Vector in)
Vector
- a vector of assemblies to match againstprotected abstract java.util.Vector randomExpansion(int maxDepth, int depth)
public java.lang.String randomInput(int maxDepth, java.lang.String separator)
public Parser setAssembler(Assembler assembler)
Assembler
- the assembler to applypublic java.lang.String toString()
toString
in class java.lang.Object
protected java.lang.String toString(java.util.Vector visited)
unvisitedString
which
subclasses must implement.Vector
- a list of objects already printedprotected abstract java.lang.String unvisitedString(java.util.Vector visited)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |