marf.nlp.Parsing.GrammarCompiler
Class Grammar

java.lang.Object
  extended by marf.nlp.Parsing.GrammarCompiler.Grammar
All Implemented Interfaces:
java.io.Serializable

public class Grammar
extends java.lang.Object
implements java.io.Serializable

Represents the language Grammar data structure. Encapsulates collections of terminals, non-terminals, and rules of the current grammar that can be serialized and reloaded on demand.

$Id: Grammar.java,v 1.22 2007/12/18 21:37:56 mokhov Exp $

Since:
0.3.0.2
Version:
$Revision: 1.22 $
Author:
Serguei Mokhov
See Also:
Serialized Form

Field Summary
static java.lang.String DEFAULT_FIRST_SETS_FILE
          Default filename to output first sets.
static java.lang.String DEFAULT_FOLLOW_SETS_FILE
          Default filename to output follow sets.
static java.lang.String DEFAULT_RULES_FILE
          Default filename to output rule productions.
protected  Terminal oEOFTerminal
          A local EOF terminal reference.
protected  Terminal oEpsilonTerminal
          The epsilon token.
protected  java.util.Vector oNonTerminalList
          List of non-terminals.
protected  java.util.Vector oRules
          A vector of rules.
protected  NonTerminal oStartNonTerminal
          A reference to the starting state.
protected  java.util.Vector oTerminalList
          List of terminals.
 
Constructor Summary
Grammar()
          Default constructor.
 
Method Summary
 void addNonTeminal(NonTerminal poNonTerminal)
          Adds non-terminal to the set of non-terminals.
 void addRule(Rule poRule)
          Adds a grammar rule to the collection of rules.
 void addTeminal(Terminal poTerminal)
          Adds a terminal to the set of terminals.
 void computeFirstSets()
          Computes first sets for all non-terminals from the non-terminals list.
 void computeFollowSets()
          Computes follow sets for all non-terminals from the non-terminals list.
 int contains(GrammarElement poGrammarElement)
          Similarly to contains(String) looks up the index of the specified grammar element.
 int contains(java.lang.String pstrName)
          Checks whether a grammar element with a given name (lexeme) exists in the corresponding list or not.
 int containsNonTerminal(java.lang.String pstrName)
          Checks whether the named non-terminal is contained in this grammar and gets its index.
 int containsTerminal(java.lang.String pstrName)
          Checks whether the named terminal is contained in this grammar and gets its index.
 Terminal getEOFTerminal()
          Allows querying for the current EOF terminal.
 Terminal getEpsilonTerminal()
          Allows querying for the current epsilon terminal.
static java.lang.String getMARFSourceCodeRevision()
          Retrieves class' revision.
 java.util.Vector getNonTerminalList()
          Allows querying for the collection of non-terminals of this grammar.
 Rule getRule(int piA, int piB, int piC)
          Retrieves a grammar rule given the indices of the LHS non-terminal and two grammar elements on the RHS.
 Rule getRule(java.lang.String pstrTerminal, int piNonTerminalIndex)
          Allows querying for a grammar rule given the name of the terminal and the index of the non-terminal grammar elements.
 java.util.Vector getRules()
          Allows querying for the collection of rules of this grammar.
 NonTerminal getStartNonTerminal()
          Allows querying for the current starting non-terminal.
 java.util.Vector getTerminalList()
          Allows querying for the collection of terminals of this grammar.
 boolean serialize(int piOperation)
          Performs serialization of this grammar data structure.
 void setEOFTerminal(Terminal poTerminal)
          Allows setting a new EOF terminal.
 void setEpsilonTerminal(Terminal poEpsilonTerminal)
          Allows setting a new epsilon terminal.
 void setStartNonTerminal(NonTerminal poStartNonTerminal)
          Allows setting a new starting non-terminal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_FIRST_SETS_FILE

public static final java.lang.String DEFAULT_FIRST_SETS_FILE
Default filename to output first sets.

See Also:
Constant Field Values

DEFAULT_FOLLOW_SETS_FILE

public static final java.lang.String DEFAULT_FOLLOW_SETS_FILE
Default filename to output follow sets.

See Also:
Constant Field Values

DEFAULT_RULES_FILE

public static final java.lang.String DEFAULT_RULES_FILE
Default filename to output rule productions.

See Also:
Constant Field Values

oTerminalList

protected java.util.Vector oTerminalList
List of terminals.


oNonTerminalList

protected java.util.Vector oNonTerminalList
List of non-terminals.


oRules

protected java.util.Vector oRules
A vector of rules.


oEOFTerminal

protected Terminal oEOFTerminal
A local EOF terminal reference.


oEpsilonTerminal

protected Terminal oEpsilonTerminal
The epsilon token.


oStartNonTerminal

protected NonTerminal oStartNonTerminal
A reference to the starting state.

Constructor Detail

Grammar

public Grammar()
Default constructor.

Method Detail

computeFirstSets

public void computeFirstSets()
Computes first sets for all non-terminals from the non-terminals list.


computeFollowSets

public void computeFollowSets()
                       throws SyntaxError
Computes follow sets for all non-terminals from the non-terminals list.

Throws:
SyntaxError - if there is no starting non-terminal

containsNonTerminal

public int containsNonTerminal(java.lang.String pstrName)
Checks whether the named non-terminal is contained in this grammar and gets its index.

Parameters:
pstrName - the name of the non-terminal to look for
Returns:
the index of the non-terminal with the given name or -1 if not found

containsTerminal

public int containsTerminal(java.lang.String pstrName)
Checks whether the named terminal is contained in this grammar and gets its index.

Parameters:
pstrName - the name of the terminal to look for
Returns:
the index of the terminal with the given name or -1 if not found

contains

public int contains(java.lang.String pstrName)
Checks whether a grammar element with a given name (lexeme) exists in the corresponding list or not. If it does, returns it's index; -1 otherwise. First, it searches for non-terminals, and if not found then searches for terminals with the specified name.

Parameters:
pstrName - the name of the grammar element to look for
Returns:
the index of the grammar element with the given name or -1 if not found

contains

public int contains(GrammarElement poGrammarElement)
Similarly to contains(String) looks up the index of the specified grammar element.

Parameters:
poGrammarElement - a grammar element to look up.
Returns:
the index of the grammar element with the given name or -1 if not found
See Also:
contains(String)

getRule

public final Rule getRule(java.lang.String pstrTerminal,
                          int piNonTerminalIndex)
Allows querying for a grammar rule given the name of the terminal and the index of the non-terminal grammar elements.

Parameters:
pstrTerminal - the name of the terminal
piNonTerminalIndex - the index of the non-terminal in the table
Returns:
a reference to the corresponding rule if found; null otherwise

getRule

public final Rule getRule(int piA,
                          int piB,
                          int piC)
Retrieves a grammar rule given the indices of the LHS non-terminal and two grammar elements on the RHS. The rule must be in CNF. TODO: review in detail for error handling.

Parameters:
piA -
piB -
piC -
Returns:
a reference to the rule object given parameters; or null if not found
Throws:
java.lang.RuntimeException - if the rule found is not in CNF

serialize

public boolean serialize(int piOperation)
Performs serialization of this grammar data structure. This particular implementation dumps the data in the textual form to screen to STDOUT. TODO: migrate to the MARF's dump/restore mechanisms

Parameters:
piOperation - UNUSED; (usually 0 means load, 1 means save)
Returns:
true if successful

addTeminal

public void addTeminal(Terminal poTerminal)
Adds a terminal to the set of terminals.

Parameters:
poTerminal - the terminal to add; must not be null

addNonTeminal

public void addNonTeminal(NonTerminal poNonTerminal)
Adds non-terminal to the set of non-terminals.

Parameters:
poNonTerminal - the non-terminal to add; must not be null

addRule

public void addRule(Rule poRule)
Adds a grammar rule to the collection of rules.

Parameters:
poRule - the rule to add; must not be null

getEOFTerminal

public Terminal getEOFTerminal()
Allows querying for the current EOF terminal.

Returns:
the reference to the EOF terminal instance
Since:
0.3.0.5

setEOFTerminal

public void setEOFTerminal(Terminal poTerminal)
Allows setting a new EOF terminal.

Parameters:
poTerminal - the terminal to become EOF
Since:
0.3.0.5

getEpsilonTerminal

public Terminal getEpsilonTerminal()
Allows querying for the current epsilon terminal.

Returns:
the reference to the epsilon terminal instance
Since:
0.3.0.5

setEpsilonTerminal

public void setEpsilonTerminal(Terminal poEpsilonTerminal)
Allows setting a new epsilon terminal.

Parameters:
poEpsilonTerminal - the terminal to become epsilon
Since:
0.3.0.5

getStartNonTerminal

public NonTerminal getStartNonTerminal()
Allows querying for the current starting non-terminal.

Returns:
the reference to the starting non-terminal instance
Since:
0.3.0.5

setStartNonTerminal

public void setStartNonTerminal(NonTerminal poStartNonTerminal)
Allows setting a new starting non-terminal.

Parameters:
poStartNonTerminal - the non-terminal to become the starting point
Since:
0.3.0.5

getTerminalList

public final java.util.Vector getTerminalList()
Allows querying for the collection of terminals of this grammar.

Returns:
the collection of terminals

getNonTerminalList

public final java.util.Vector getNonTerminalList()
Allows querying for the collection of non-terminals of this grammar.

Returns:
the collection of non-terminals

getRules

public final java.util.Vector getRules()
Allows querying for the collection of rules of this grammar.

Returns:
the rules collection

getMARFSourceCodeRevision

public static java.lang.String getMARFSourceCodeRevision()
Retrieves class' revision.

Returns:
revision string


SourceForge Logo