Generic Interpreter 1.3
Protected API

gi
Class Grammar

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

public class Grammar
extends Lexicon

This class implements a Grammar.

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

Nested Class Summary
protected static class Grammar.ParseTree
          This class implements a ParseTree constructed by interpreting a source stream.
protected static class Grammar.Semantics
          This class implements Semantics embedded in productions and evaluated when interpreting.
 
Nested classes/interfaces inherited from class gi.Lexicon
Lexicon.Alphabet, Lexicon.Concatenation, Lexicon.Exception, Lexicon.Expression, Lexicon.Match, Lexicon.NonMatch, Lexicon.PosixClass, Lexicon.Range, Lexicon.Repetition, Lexicon.Singleton, Lexicon.UnicodeCategory, Lexicon.Union
 
Field Summary
protected static int CONFLICT
          debug switch constant enables printing parsing conflicts encountered during syntax analysis.
protected static int FIRST_FOLLOW
          debug switch constant enables printing first and follow sets precomputed during syntax analysis.
protected static int PARSE_TREE
          debug switch constant enables printing each ParseTree produced by syntax analysis.
protected static int SYNTAX
          debug switch constant enables printing parsing decisions made during syntax analysis.
 
Fields inherited from class gi.Lexicon
$, debug, LEXICAL, TERMINALS, VERBOSE
 
Constructor Summary
protected Grammar()
          Constructs an empty Grammar.
 
Method Summary
protected  Object grab(LineNumberReader source)
          Grabs a terminal from a source character stream using this Grammar.
 void interpret(String[] arguments)
          Interprets by command-line arguments using 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.
 
Methods inherited from class gi.Lexicon
expression, interpret, interpret, interpret, interpret, interpret, interpret, put, word
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FIRST_FOLLOW

protected static final int FIRST_FOLLOW

debug switch constant enables printing first and follow sets precomputed during syntax analysis.

Since:
1.1
See Also:
Constant Field Values

SYNTAX

protected static final int SYNTAX

debug switch constant enables printing parsing decisions made during syntax analysis.

Since:
1.1
See Also:
Constant Field Values

CONFLICT

protected static final int CONFLICT

debug switch constant enables printing parsing conflicts encountered during syntax analysis.

Since:
1.1
See Also:
Constant Field Values

PARSE_TREE

protected static final int PARSE_TREE

debug switch constant enables printing each ParseTree produced by syntax analysis.

Since:
1.1
See Also:
Constant Field Values
Constructor Detail

Grammar

protected Grammar()

Constructs an empty Grammar.

Method Detail

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 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 side of the productions.
phrases - the phrases produced by nonterminal. Each phrase in phrases may contain nonterminals, terminals, and Semantics.

grab

protected Object grab(LineNumberReader source)
               throws Lexicon.Exception

Grabs a terminal 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 void interpret(String[] arguments)

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.

Overrides:
interpret in class Lexicon
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 Grammar before subsequent syntax analyses.
-l, --lexical
Print terminals in this Grammar grabbed during subsequent syntax analyses.
-f, --firstfollow
Print first and follow sets precomputed during subsequent syntax analyses.
-s, --syntax
Print parsing decisions made during subsequent syntax analyses.
-c, --conflict
Print parsing conflicts encountered during subsequent syntax analyses.
-p, --parsetree, --tree
Print each ParseTree produced by subsequent syntax analyses.
-v, --verbose
Print maximum debugging. Equivalent to -tlfscp.
--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.

 

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