Generic Interpreter 0.9
Private API

gi
Class Grammar

java.lang.Object
  |
  +--gi.Lexicon
        |
        +--gi.Grammar
Direct Known Subclasses:
LL1_Grammar, LR0_Grammar

public class Grammar
extends Lexicon

This class implements a Grammar.

Version:
0.9
Author:
© 1999-2000 Craig A. Rich <carich@acm.org>
See Also:
Source code

Inner Class Summary
protected static class Grammar.ParseTree
          This class implements a ParseTree constructed by interpreting a source stream.
(package private) static class Grammar.Production
          This class implements a Production.
protected static class Grammar.Semantics
          This class implements Semantics embedded in productions and evaluated when interpreting.
 
Inner classes inherited from class gi.Lexicon
Lexicon.Alphabet, Lexicon.Concatenation, Lexicon.Exception, Lexicon.Expression, Lexicon.Match, Lexicon.NonMatch, Lexicon.PosixClass, Lexicon.Range, Lexicon.Repetition, Lexicon.Set, Lexicon.Singleton, Lexicon.UnicodeCategory, Lexicon.Union
 
Field Summary
private  Lexicon.Set first
          The first set computed by first(phrase, start).
private  Map firsts
          The mapping from a nonterminal to its first set in this Grammar.
private  Map follows
          The mapping from a nonterminal to its follow set in this Grammar.
(package private)  Map productions
          The Productions put into this Grammar.
private static int size
          The number of Productions constructed.
(package private)  Object start
          The start symbol of this Grammar.
private  HashSet terminals
          The terminals put into this Grammar.
 
Fields inherited from class gi.Lexicon
accepts, END_OF_SOURCE, END_OF_SOURCE_EXPRESSION, initial, states, transitions, word
 
Constructor Summary
protected Grammar()
          Constructs an empty Grammar.
(package private) Grammar(Grammar grammar)
          Constructs a Grammar that is a shallow copy of grammar.
 
Method Summary
(package private)  Lexicon.Set first(Object nonterminal)
          Returns the first set of a nonterminal.
(package private)  Lexicon.Set first(Object[] phrase, int start)
          Computes the first set of a phrase.
(package private)  Lexicon.Set follow(Object nonterminal)
          Returns the follow set of a nonterminal.
 Object grab(BufferedReader source)
          Grabs terminals from a source character stream using this Grammar.
 Grammar.ParseTree interpret()
          Interprets the standard input stream using this Grammar.
 Grammar.ParseTree interpret(File source)
          Interprets a source file using this Grammar.
 Grammar.ParseTree interpret(InputStream source)
          Interprets a source byte stream using this Grammar.
(package private)  Grammar.ParseTree interpret(LineNumberReader source)
          Interprets a source character stream using an LR(1) parser around this Grammar.
 Grammar.ParseTree interpret(PipedWriter source)
          Interprets a source pipe using this Grammar.
 Grammar.ParseTree interpret(Reader source)
          Interprets a source character stream using this Grammar.
 Grammar.ParseTree interpret(String source)
          Interprets a source string using this Grammar.
 Grammar.ParseTree interpret(String[] argv)
          Interprets by command-line arguments using this Grammar.
(package private)  boolean nonterminal(Object symbol)
          Indicates whether a symbol is a nonterminal in this Grammar.
protected  void put(Object nonterminal, Object[] phrase)
          Puts a production into this Grammar.
protected  void put(Object nonterminal, Object[][] phrases)
          Puts productions into this Grammar.
(package private)  boolean terminal(Object symbol)
          Indicates whether a symbol is a terminal in this Grammar.
 
Methods inherited from class gi.Lexicon
closure, expression, initial, put, put, recognize, state, transition, word
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

first

private final Lexicon.Set first

The first set computed by first(phrase, start).


firsts

private final Map firsts

The mapping from a nonterminal to its first set in this Grammar. When empty, there is a need to compute current first sets. It is computed only on demand created by first(nonterminal).


follows

private final Map follows

The mapping from a nonterminal to its follow set in this Grammar. When empty, there is a need to compute current follow sets. It is computed only on demand created by follow(nonterminal).


productions

final Map productions

The Productions put into this Grammar. It is a mapping from a nonterminal to its Productions.


size

private static int size

The number of Productions constructed.


start

Object start

The start symbol of this Grammar. It is the nonterminal on the left-hand side of the Production first put into this Grammar.


terminals

private final HashSet terminals

The terminals put into this Grammar. When empty, there is a need to discover terminals. It is computed only on demand created by grab(source).

Constructor Detail

Grammar

protected Grammar()

Constructs an empty Grammar.


Grammar

Grammar(Grammar grammar)

Constructs a Grammar that is a shallow copy of grammar. The fields of the new Grammar refer to the same objects as those in grammar.

Parameters:
grammar - the Grammar to be copied.
Method Detail

first

Lexicon.Set first(Object nonterminal)

Returns the first set of a nonterminal.

Parameters:
nonterminal - the nonterminal whose first set is requested.
Returns:
firsts.get(nonterminal), computing firsts if there is a need to compute current first sets.

first

Lexicon.Set first(Object[] phrase,
                  int start)

Computes the first set of a phrase.

Parameters:
phrase - the phrase whose first set is computed.
start - the index at which to start computing the first set.
Returns:
the first set of phrase.

follow

Lexicon.Set follow(Object nonterminal)

Returns the follow set of a nonterminal.

Parameters:
nonterminal - the nonterminal whose follow set is requested.
Returns:
follows.get(nonterminal), computing follows if there is a need to compute current follow sets.

grab

public Object grab(BufferedReader source)
            throws Lexicon.Exception

Grabs terminals from a source character stream using this Grammar. Invokes Lexicon.grab(source) until it returns a terminal occurring in a phrase of this Grammar or end of source. Blocks until a character is available, an I/O error occurs, or the end of the source stream is reached.

Overrides:
grab in class Lexicon
Parameters:
source - the source character stream.
Returns:
the first terminal occurring in a phrase of this Grammar.
Throws:
Lexicon.Exception - if an I/O or lexical error occurs.

interpret

public Grammar.ParseTree interpret()
                            throws Lexicon.Exception

Interprets the standard input stream using this Grammar.

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 Grammar.ParseTree interpret(File source)
                            throws FileNotFoundException,
                                   Lexicon.Exception

Interprets a source file using this Grammar.

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 Grammar.ParseTree interpret(InputStream source)
                            throws Lexicon.Exception

Interprets a source byte stream using this Grammar.

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

Grammar.ParseTree interpret(LineNumberReader source)
                      throws Lexicon.Exception

Interprets a source character stream using an LR(1) parser around this Grammar. This method is overridden by all parsers, so it is only invoked when this Grammar has not been extended by a parser.

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 Grammar.ParseTree interpret(PipedWriter source)
                            throws IOException,
                                   Lexicon.Exception

Interprets a source pipe using this Grammar.

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 Grammar.ParseTree interpret(Reader source)
                            throws Lexicon.Exception

Interprets a source character stream using this Grammar.

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 Grammar.ParseTree interpret(String source)
                            throws Lexicon.Exception

Interprets a source string using this Grammar.

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 Grammar.ParseTree interpret(String[] argv)

Interprets by command-line arguments using this Grammar. When interpreting, the parser used will be (in order):

  • The parser most recently constructed by preceding command-line arguments.
  • The parser originally around this Grammar.
  • An LR(1) parser.
The first I/O, lexical, syntax or semantic error that occurs when interpreting is printed to the standard error stream. If requested, each ParseTree is printed to the standard error stream after interpreting.

Parameters:
argv - 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:
-tree
Print each ParseTree subsequently constructed by interpreting a source stream.
-ll1
Construct an LL(1) parser around this Grammar.
-lr0
Construct an LR(0) parser around this Grammar.
-slr1
Construct an SLR(1) parser around this Grammar.
-lr1
Construct an LR(1) parser around this Grammar.
-
Interpret the standard input stream using this Grammar.
filename
Interpret source file filename using this Grammar.
If no filename arguments are given, the standard input stream is interpreted.
Returns:
the last ParseTree constructed by interpreting a source stream.

nonterminal

boolean nonterminal(Object symbol)

Indicates whether a symbol is a nonterminal in this Grammar.

Parameters:
symbol - the symbol whose status is requested.
Returns:
true if symbol is a nonterminal in this Grammar; false otherwise.

put

protected void put(Object nonterminal,
                   Object[] phrase)

Puts a production into this Grammar. The start symbol is the first nonterminal put in this Grammar.

Parameters:
nonterminal - the nonterminal to be added to this Grammar.
phrase - the phrase produced by nonterminal. phrase may contain nonterminals, terminals, and Semantics.

put

protected void put(Object nonterminal,
                   Object[][] phrases)

Puts productions into this Grammar. The productions are successively added using put(nonterminal, phrase).

Parameters:
nonterminal - the nonterminal on the left-hand side of the production.
phrases - the phrases produced by nonterminal. Each phrase in phrases may contain nonterminals, terminals, and Semantics.

terminal

boolean terminal(Object symbol)

Indicates whether a symbol is a terminal in this Grammar.

Overrides:
terminal in class Lexicon
Parameters:
symbol - the symbol whose status is requested.
Returns:
true if symbol is a terminal in this Grammar; false otherwise.

 

© 1999-2000 Craig A. Rich <carich@acm.org>