fri.patterns.interpreter.parsergenerator.syntax
Class Syntax

java.lang.Object
  |
  +--fri.patterns.interpreter.parsergenerator.syntax.Syntax
All Implemented Interfaces:
java.io.Serializable

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

A Syntax is a Rule list that can be converted to a parser table. It provides several methods to check and simplify the grammar. Syntax accepts no duplicate rules (ignores any addition except the first by using a backing hashtable).

Author:
(c) 2004 Fritz Ritzberger
See Also:
Serialized Form

Constructor Summary
Syntax()
           
Syntax(int size)
           
Syntax(java.util.List rules)
           
Syntax(java.lang.String[][] rules)
           
Syntax(java.lang.String[][][] ruleSets)
           
 
Method Summary
 void addRule(Rule rule)
          Appends a new rule to this Syntax.
 void appendRules(java.util.List rules)
          Append rules to this syntax.
 java.util.List findStartRules()
          Returns a List of top level Rules (that do not appear on right side of any contained rule).
 Rule getRule(int i)
          Returns the rule at the given position.
 java.util.Set getUnresolvedNonterminals()
          Returns a set of nonterminals that have no rule within this syntax.
 boolean hasRule(java.lang.String nonterminal)
          Returns true if the passed nonterminal is on the left side of at least one contained rule.
 void insertRule(int i, Rule rule)
          Inserts a new rule into this Syntax (needed to set artificial START rule at position 0).
 void removeRule(int index)
          Removes the rule at passed index from this Syntax.
 void resolveFrom(Syntax resolvingSyntax)
          Resolves undefined rules in this syntax from another syntax.
 void resolveSingulars()
          Simplify the syntax.
 int size()
          Returns the number of rules of this Syntax.
 java.lang.String toString()
          Returns the syntax as a human readable multiline text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Syntax

public Syntax()

Syntax

public Syntax(int size)

Syntax

public Syntax(java.lang.String[][] rules)

Syntax

public Syntax(java.lang.String[][][] ruleSets)

Syntax

public Syntax(java.util.List rules)
Method Detail

appendRules

public void appendRules(java.util.List rules)
Append rules to this syntax. List elements can be Rule or List, second will be converted to Rule.


size

public int size()
Returns the number of rules of this Syntax.


getRule

public Rule getRule(int i)
Returns the rule at the given position.


addRule

public void addRule(Rule rule)
Appends a new rule to this Syntax.


insertRule

public void insertRule(int i,
                       Rule rule)
Inserts a new rule into this Syntax (needed to set artificial START rule at position 0).


removeRule

public void removeRule(int index)
Removes the rule at passed index from this Syntax.


findStartRules

public java.util.List findStartRules()
Returns a List of top level Rules (that do not appear on right side of any contained rule).


resolveSingulars

public void resolveSingulars()
Simplify the syntax. Search rules that have a nonterminal that is defined only once and contains exactly one symbol on right side. Such a rule gets deleted and its references get substituted by the symbol on right side of that rule.

This method is called by default from SerializedObject (base class for all builders).

CAUTION: Such symbols will not be called within Semantic!


resolveFrom

public void resolveFrom(Syntax resolvingSyntax)
Resolves undefined rules in this syntax from another syntax.

Parameters:
resolvingSyntax - the syntax to copy rules from

getUnresolvedNonterminals

public java.util.Set getUnresolvedNonterminals()
Returns a set of nonterminals that have no rule within this syntax.


hasRule

public boolean hasRule(java.lang.String nonterminal)
Returns true if the passed nonterminal is on the left side of at least one contained rule.


toString

public java.lang.String toString()
Returns the syntax as a human readable multiline text.

Overrides:
toString in class java.lang.Object