Generic Interpreter 1.3
Protected API

gi
Class Lexicon

java.lang.Object
  extended by gi.Lexicon
Direct Known Subclasses:
Grammar

public class Lexicon
extends Object

This class implements a Lexicon.

Version:
1.3
Author:
© 1999-2009 Craig A. Rich <carich@csupomona.edu>

Nested Class Summary
protected static class Lexicon.Alphabet
          This class implements an Alphabet of character symbols.
protected static class Lexicon.Concatenation
          This class implements an Expression expressing the concatenation of two regular languages.
protected  class Lexicon.Exception
          This class implements an Exception.
protected static class Lexicon.Expression
          This class implements an Expression expressing a regular language.
protected static class Lexicon.Match
          This class implements an Alphabet containing some characters.
protected static class Lexicon.NonMatch
          This class implements an Alphabet containing all except some characters.
protected static class Lexicon.PosixClass
          This class implements an Alphabet containing the characters in a POSIX character class.
protected static class Lexicon.Range
          This class implements an Alphabet containing the characters in a range.
protected static class Lexicon.Repetition
          This class implements an Expression expressing the repetition of a regular language.
protected static class Lexicon.Singleton
          This class implements an Expression expressing a singleton language.
protected static class Lexicon.UnicodeCategory
          This class implements an Alphabet containing the characters in a Unicode general category.
protected static class Lexicon.Union
          This class implements an Expression expressing the union of two regular languages.
 
Field Summary
protected static Object $
          The terminal matched by the character at the end of a source stream.
protected  int debug
          The debug switches, initially zero.
protected static int LEXICAL
          debug switch constant enabling printing terminals and associated words grabbed during lexical analysis.
protected static int TERMINALS
          debug switch constant enabling printing the set of terminals before lexical analysis.
protected static int VERBOSE
          debug switch constant enabling all debugging.
 
Constructor Summary
protected Lexicon()
          Constructs an empty Lexicon.
 
Method Summary
protected static Lexicon.Expression expression(String ere)
          Creates an Expression by interpreting a POSIX extended regular expression (ERE), as used in egrep.
protected  Object grab(LineNumberReader source)
          Grabs a terminal from a source character stream using this Lexicon.
 Object interpret()
          Interprets the standard input stream using this Lexicon.
 Object interpret(File source)
          Interprets a source file using this Lexicon.
 Object interpret(InputStream source)
          Interprets a source byte stream using this Lexicon.
 Object interpret(PipedWriter source)
          Interprets a source pipe using this Lexicon.
 Object interpret(Reader source)
          Interprets a source character stream using this Lexicon.
 Object interpret(String source)
          Interprets a source string using this Lexicon.
 void interpret(String[] arguments)
          Lexical analysis by command-line arguments using this Lexicon.
protected  void put(Object a, Lexicon.Expression e)
          Puts a terminal and associated Expression into this Lexicon.
protected  String word()
          Returns the word most recently grabbed using this Lexicon.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

$

protected static final Object $

The terminal matched by the character at the end of a source stream.

Since:
1.1, renames END_OF_SOURCE in version 1.0.

debug

protected int debug

The debug switches, initially zero. The following bits enable debugging to standard output:

0x01 = TERMINALS
Print the set of terminals before lexical analysis
0x02 = LEXICAL
Print terminals and associated words grabbed during lexical analysis
0x04 = FIRST_FOLLOW
Print first and follow sets precomputed during syntax analysis
0x08 = SYNTAX
Print parsing decisions made during syntax analysis
0x10 = CONFLICT
Print parsing conflicts encountered during syntax analysis
0x20 = PARSE_TREE
Print each ParseTree produced by syntax analysis

Since:
1.1

TERMINALS

protected static final int TERMINALS

debug switch constant enabling printing the set of terminals before lexical analysis.

Since:
1.1
See Also:
Constant Field Values

LEXICAL

protected static final int LEXICAL

debug switch constant enabling printing terminals and associated words grabbed during lexical analysis.

Since:
1.1
See Also:
Constant Field Values

VERBOSE

protected static final int VERBOSE

debug switch constant enabling all debugging.

Since:
1.1
See Also:
Constant Field Values
Constructor Detail

Lexicon

protected Lexicon()

Constructs an empty Lexicon.

Method Detail

expression

protected static Lexicon.Expression expression(String ere)
                                        throws Lexicon.Exception

Creates an Expression by interpreting a POSIX extended regular expression (ERE), as used in egrep. The syntax and semantics for EREs is formally specified by the ERE Grammar. Provides a convenient method for constructing an Expression, at the cost of an LR(1) parse. Implementations seeking maximum speed should avoid this method and use explicit Expression subclass constructors; for example,

new Union(new NonMatch("0"), new Singleton("foo"))
instead of
expression("[^0]|foo")

Parameters:
ere - the POSIX extended regular expression (ERE) to interpret.
Returns:
the Expression constructed by interpreting ere.
Throws:
Lexicon.Exception - if an ERE syntax error occurs.

put

protected void put(Object a,
                   Lexicon.Expression e)

Puts a terminal and associated Expression into this Lexicon. The Expression supersedes any previously associated with the terminal.

Parameters:
a - the terminal to add to this Lexicon.
e - the Expression associated with terminal a. When grabbing, the language expressed by e matches a.

grab

protected Object grab(LineNumberReader source)
               throws Lexicon.Exception

Grabs a terminal from a source character stream using this Lexicon. The variable returned by word() is set to the longest nonempty prefix of the remaining source characters matching an Expression in this Lexicon. If no nonempty prefix matches an Expression, a Lexicon.Exception is thrown. If the longest matching prefix matches more than one Expression, the terminal associated with the Expression most recently constructed is returned. Blocks until a character is available, an I/O error occurs, or the end of the source stream is reached.

Parameters:
source - the source character stream.
Returns:
the terminal grabbed from source.
Throws:
Lexicon.Exception - if an I/O or lexical error occurs.

word

protected String word()

Returns the word most recently grabbed using this Lexicon.

Returns:
the word most recently grabbed by grab(source).

interpret

public Object interpret(Reader source)
                 throws Lexicon.Exception

Interprets a source character stream using this Lexicon.

Parameters:
source - the source character stream.
Returns:
the ParseTree constructed by interpreting source.
Throws:
Lexicon.Exception - if an I/O, lexical, syntax or semantic error occurs.

interpret

public Object interpret(String source)
                 throws Lexicon.Exception

Interprets a source string using this Lexicon.

Parameters:
source - the source string.
Returns:
the ParseTree constructed by interpreting source.
Throws:
Lexicon.Exception - if an I/O, lexical, syntax or semantic error occurs.

interpret

public Object interpret(InputStream source)
                 throws Lexicon.Exception

Interprets a source byte stream using this Lexicon.

Parameters:
source - the source byte stream.
Returns:
the ParseTree constructed by interpreting source.
Throws:
Lexicon.Exception - if an I/O, lexical, syntax or semantic error occurs.

interpret

public Object interpret()
                 throws Lexicon.Exception

Interprets the standard input stream using this Lexicon.

Returns:
the ParseTree constructed by interpreting the standard input stream.
Throws:
Lexicon.Exception - if an I/O, lexical, syntax or semantic error occurs.

interpret

public Object interpret(File source)
                 throws FileNotFoundException,
                        Lexicon.Exception

Interprets a source file using this Lexicon.

Parameters:
source - the source file.
Returns:
the ParseTree constructed by interpreting source.
Throws:
FileNotFoundException - if the source file cannot be found.
Lexicon.Exception - if an I/O, lexical, syntax or semantic error occurs.

interpret

public Object interpret(PipedWriter source)
                 throws IOException,
                        Lexicon.Exception

Interprets a source pipe using this Lexicon.

Parameters:
source - the source pipe.
Returns:
the ParseTree constructed by interpreting source.
Throws:
IOException - if the source pipe cannot be connected.
Lexicon.Exception - if an I/O, lexical, syntax or semantic error occurs.

interpret

public void interpret(String[] arguments)

Lexical analysis by command-line arguments using this Lexicon. The first I/O or lexical error that occurs during lexical analysis is printed to the standard error stream.

Parameters:
arguments - the command-line arguments controlling the interpreter.
The following arguments may appear zero or more times, are processed in order, and have the following effects:
-t, --terminals
Print the set of terminals in this Lexicon before subsequent lexical analyses.
-l, --lexical
Print terminals in this Lexicon grabbed during subsequent lexical analyses.
-v, --verbose
Print maximum debugging. Equivalent to -tl.
-
Lexically analyze the standard input stream using this Lexicon.
filename
Lexically analyze source file filename using this Lexicon.
If no filename arguments are given, the standard input stream is lexically analyzed.

 

© 1999-2009 Craig A. Rich <carich@csupomona.edu>