grammar.cfg
Class MyGrammar

java.lang.Object
  extended by grammar.Grammar
      extended by grammar.cfg.MyGrammar
All Implemented Interfaces:
java.io.Serializable

public class MyGrammar
extends Grammar

Context-free grammars.

Author:
XZW
See Also:
Serialized Form

Constructor Summary
MyGrammar()
          Creates an instance of grammar.
 
Method Summary
 void addProduction(Production production)
          Adds a production to the set of productions in the grammar.
 void checkProduction(Production production)
          Checks whether a production is invalid
 int getIndexOfStart()
          Returns the index of start variable.
 int[][] getInfoOfPros()
          Returns the information (indices and numbers) of sorted productions w.r.t.
 MyProduction[] getProductions()
          Returns all productions in the grammar.
 java.lang.String[] getTerminals()
          Returns all terminals in the grammar.
 java.lang.String[] getVariables()
          Returns all variables in the grammar.
 boolean isSorted()
          Returns whether the grammar is sorted or not.
 void setIndex()
          Sets the indices of the tokens at the productions.
 void setIndexOfStart(int indexOfStart)
          Sets the index of start variable and changes the start variable simultaneity.
 void setStartVariable(java.lang.String variable)
          Sets the start variable and changes the index of start variable simultaneity.
 void sortAndIndex()
          Sorts the variables, terminals, and productions of the grammar, and sets the indices of the tokens at the productions.
 
Methods inherited from class grammar.Grammar
addProductions, equals, getProductionsNumber, getStartVariable, getTerminalsNumber, getVariablesNumber, hashCode, isEmpty, reachability, toString, usefulness
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MyGrammar

public MyGrammar()
Creates an instance of grammar. The created instance has no productions, no terminals, no variables, and specifically no start variable.

Method Detail

checkProduction

public void checkProduction(Production production)
Description copied from class: Grammar
Checks whether a production is invalid

Specified by:
checkProduction in class Grammar
Parameters:
production - the production to check

getProductions

public MyProduction[] getProductions()
Description copied from class: Grammar
Returns all productions in the grammar.

Overrides:
getProductions in class Grammar
Returns:
all productions in the grammar.

isSorted

public boolean isSorted()
Returns whether the grammar is sorted or not.

Returns:
true if the grammar is sorted.

getIndexOfStart

public int getIndexOfStart()
Returns the index of start variable.

Returns:
the index of start variable.

setIndexOfStart

public void setIndexOfStart(int indexOfStart)
Sets the index of start variable and changes the start variable simultaneity.

Parameters:
indexOfStart - the start variable index to set
Throws:
java.lang.IllegalArgumentException - if the indexOfStart is illegal.

setStartVariable

public void setStartVariable(java.lang.String variable)
Sets the start variable and changes the index of start variable simultaneity.

Overrides:
setStartVariable in class Grammar
Parameters:
variable - the start variable to set
Throws:
java.lang.IllegalArgumentException - if the variable is not in the variables

getVariables

public java.lang.String[] getVariables()
Description copied from class: Grammar
Returns all variables in the grammar.

Overrides:
getVariables in class Grammar
Returns:
all variables in the grammar.

getTerminals

public java.lang.String[] getTerminals()
Description copied from class: Grammar
Returns all terminals in the grammar.

Overrides:
getTerminals in class Grammar
Returns:
all terminals in the grammar.

addProduction

public void addProduction(Production production)
Description copied from class: Grammar
Adds a production to the set of productions in the grammar.

Overrides:
addProduction in class Grammar
Parameters:
production - the production to be added.

getInfoOfPros

public int[][] getInfoOfPros()
Returns the information (indices and numbers) of sorted productions w.r.t. variables: infoOfPros[i][0]: the first index of i-variable's terminal productions; infoOfPros[i][1]: the number of i-variable's terminal productions; infoOfPros[i][2]: the first index of i-variable's variable productions; infoOfPros[i][3]: the number of i-variable's variable productions.

Returns:
the information of sorted productions w.r.t. variables

sortAndIndex

public void sortAndIndex()
Sorts the variables, terminals, and productions of the grammar, and sets the indices of the tokens at the productions.


setIndex

public void setIndex()
Sets the indices of the tokens at the productions.