net.percederberg.grammatica.parser
Class ProductionPatternRule

java.lang.Object
  |
  +--net.percederberg.grammatica.parser.ProductionPatternRule

public class ProductionPatternRule
extends java.lang.Object

A production pattern rule. This class represents a list of production pattern elements. In order to provide alternatives that cannot be represented with the element occurance counters, multiple rules must be created and added to the same production pattern. A production pattern rule is always contained within a production pattern.

Version:
1.0
Author:
Per Cederberg,

Constructor Summary
ProductionPatternRule()
          Creates a new production pattern rule.
 
Method Summary
 void addElement(ProductionPatternElement elem)
          Adds a production pattern element to this rule.
 void addElement(ProductionPatternElement elem, int min, int max)
          Adds a production pattern element to this rule.
 void addProduction(int id, int min, int max)
          Adds a production to this rule.
 void addToken(int id, int min, int max)
          Adds a token to this rule.
 ProductionPatternElement getElement(int pos)
          Returns an element in this rule.
 int getElementCount()
          Returns the number of elements in this rule.
 int getMaxElementCount()
          Returns the maximum number of elements needed to satisfy this rule.
 int getMinElementCount()
          Returns the minimum number of elements needed to satisfy this rule.
 ProductionPattern getPattern()
          Returns the production pattern containing this rule.
 boolean isLeftRecursive()
          Checks if this rule is recursive on the left-hand side.
 boolean isMatchingEmpty()
          Checks if this rule would match an empty stream of tokens.
 boolean isRightRecursive()
          Checks if this rule is recursive on the right-hand side.
 java.lang.String toString()
          Returns a string representation of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ProductionPatternRule

public ProductionPatternRule()
Creates a new production pattern rule.

Method Detail

isLeftRecursive

public boolean isLeftRecursive()
Checks if this rule is recursive on the left-hand side. This method checks all the possible left side elements and returns true if the pattern itself is among them.

Returns:
true if the rule is left side recursive, or false otherwise

isRightRecursive

public boolean isRightRecursive()
Checks if this rule is recursive on the right-hand side. This method checks all the possible right side elements and returns true if the pattern itself is among them.

Returns:
true if the rule is right side recursive, or false otherwise

isMatchingEmpty

public boolean isMatchingEmpty()
Checks if this rule would match an empty stream of tokens. This check is equivalent of getMinElementCount() returning zero (0).

Returns:
true if the rule can match an empty token stream, or false otherwise

getPattern

public ProductionPattern getPattern()
Returns the production pattern containing this rule.

Returns:
the production pattern for this rule

getElementCount

public int getElementCount()
Returns the number of elements in this rule.

Returns:
the number of elements in this rule

getMinElementCount

public int getMinElementCount()
Returns the minimum number of elements needed to satisfy this rule. The value returned is the sum of all the elements minimum count.

Returns:
the minimum number of elements

getMaxElementCount

public int getMaxElementCount()
Returns the maximum number of elements needed to satisfy this rule. The value returned is the sum of all the elements maximum count.

Returns:
the maximum number of elements

getElement

public ProductionPatternElement getElement(int pos)
Returns an element in this rule.

Parameters:
pos - the element position, 0 <= pos < count
Returns:
the element found

addToken

public void addToken(int id,
                     int min,
                     int max)
Adds a token to this rule. The token is appended to the end of the element list. The multiplicity values specified define if the token is optional or required, and if it can be repeated.

Parameters:
id - the token (pattern) id
min - the minimum number of occurancies
max - the maximum number of occurancies, or -1 for infinite

addProduction

public void addProduction(int id,
                          int min,
                          int max)
Adds a production to this rule. The production is appended to the end of the element list. The multiplicity values specified define if the production is optional or required, and if it can be repeated.

Parameters:
id - the production (pattern) id
min - the minimum number of occurancies
max - the maximum number of occurancies, or -1 for infinite

addElement

public void addElement(ProductionPatternElement elem)
Adds a production pattern element to this rule. The element is appended to the end of the element list.

Parameters:
elem - the production pattern element

addElement

public void addElement(ProductionPatternElement elem,
                       int min,
                       int max)
Adds a production pattern element to this rule. The multiplicity values in the element will be overridden with the specified values. The element is appended to the end of the element list.

Parameters:
elem - the production pattern element
min - the minimum number of occurancies
max - the maximum number of occurancies, or -1 for infinite

toString

public java.lang.String toString()
Returns a string representation of this object.

Overrides:
toString in class java.lang.Object
Returns:
a token string representation