Class GroupingSymbolTable

java.lang.Object
  |
  +--GroupingSymbolTable
Direct Known Subclasses:
GenericGroupingSymbolTable

public class GroupingSymbolTable
extends java.lang.Object

Much like OperatorTable this call serves as a holder for a set of other objects and acts as an interface to allow use of them. In this case the objects are GroupingSymbols.

The most common usage of GroupingSymbolTable is GenericGroupingSymbolTable.


Field Summary
protected  int capacity
          The current maximum number of elements possible
protected  int size
          The current number of elements
protected  GroupingSymbol[] symbols
          The elements contained in the table
 
Constructor Summary
GroupingSymbolTable()
          Creates a new empty table with a capacity of 20 elements
GroupingSymbolTable(int i)
          Creates a new empty table with a capacity of i elements.
 
Method Summary
 void add(GroupingSymbol o)
          Adds a GroupingSymbol to the table.
 java.lang.String[] getTokens()
           
 boolean isAClosingSymbol(java.lang.String token)
          Identifies String token as being an closing symbol or not.
 boolean isAGroupingSymbol(java.lang.String token)
          Identifies String token as being a symbol in the table or not.
 boolean isAnOpeningSymbol(java.lang.String token)
          Identifies String token as being an opening symbol or not.
 boolean isBalanced(java.lang.String s)
          Checks if an infix expression is balanced.
 GroupingSymbol symbolFor(java.lang.String token)
          Identifies String token and returns the appropriate GroupingSymbol.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

capacity

protected int capacity
The current maximum number of elements possible

size

protected int size
The current number of elements

symbols

protected GroupingSymbol[] symbols
The elements contained in the table
Constructor Detail

GroupingSymbolTable

public GroupingSymbolTable()
Creates a new empty table with a capacity of 20 elements

GroupingSymbolTable

public GroupingSymbolTable(int i)
Creates a new empty table with a capacity of i elements. If i is 0 then the capacity is set to 1.
Method Detail

add

public void add(GroupingSymbol o)
Adds a GroupingSymbol to the table. If there is not room in the table for the symbol then (in the fashion of Vector) the capacity is doubled thereby making space.
Parameters:
o - symbol to be added

isAGroupingSymbol

public boolean isAGroupingSymbol(java.lang.String token)
Identifies String token as being a symbol in the table or not.
Parameters:
token - String to be identified
Returns:
true if token is in the table

isAnOpeningSymbol

public boolean isAnOpeningSymbol(java.lang.String token)
Identifies String token as being an opening symbol or not. If token is not in the table then a NotATableElementExcepetion will be thrown (once I get that running.)
Parameters:
token - String to be identified
Returns:
true if token may begin a group

isAClosingSymbol

public boolean isAClosingSymbol(java.lang.String token)
Identifies String token as being an closing symbol or not. If token is not in the table then a NotATableElementExcepetion will be thrown (once I get that running.)
Parameters:
token - String to be identified
Returns:
true if token may end a group

symbolFor

public GroupingSymbol symbolFor(java.lang.String token)
Identifies String token and returns the appropriate GroupingSymbol. If token is not in the table then a NotATableElementExcepetion will be thrown (once I get that running.)
Parameters:
token - String to be identified
Returns:
the GroupingSymbol for token.

getTokens

public java.lang.String[] getTokens()
Returns:
a String array contianing the tokens for the GroupingSymbols in the table.

isBalanced

public boolean isBalanced(java.lang.String s)
Checks if an infix expression is balanced. An expression has balanced grouping symbols if every grouping symbol that is opened is closed and each closing symbol closes the most recent opening symbol.

For example, ((x + y) / 3) * 5 is balanced because there are two open parens '(' and two closes and each close works on the most recently opened symbol. ([x + y] / 3) * 5 is balanced for the same reason, but ([x + y) / 3] * 5 is not because when the closing symbol ')' is reached the current open symbol is ']' and the two don't match. Similarly ([x + y] / 3 * 5 is not valid because there is no close for the initial '('.
Parameters:
s - infix equation to check the balance of
Returns:
whether s is balanced or not