package finiteAutomata; import java.util.Vector; /** * A non-empty set of symbols. Alphabets are naturally associated * with both finite automata (input symbols) and grammars (terminal * symbols). * * @author Barbara Wahl * @version Fall 2007 */ public class Alphabet { // ** DATA FIELDS ** /** * alphabet symbols */ protected StringBuffer alpha; // ** CONSTRUCTOR ** /** * creates an Alphabet from the characters in a given string. * @param inString String containing the desired alphabet characters * in the desired lexicographic order * @pre inString is a non-empty string * @pre inString has no repeated characters * @pre inString has no whitespace characters * @post creates an Alphabet object whose symbols are the characters * in inString */ public Alphabet(String inString) { Assert.pre(inString.length() > 0, "inString is a non-empty string"); Assert.pre(!Util.hasRepeat(inString), "inString has no repeated characters"); Assert.pre(!Util.hasWhiteSpace(inString), "inString has no whitespace characters"); alpha = new StringBuffer(inString); } // ** PUBLIC METHODS ** /** * Returns number of symbols in this Alphabet. * @return alpha.length() */ public int size() { return alpha.length(); } /** * Determines if a given character is a symbol in this Alphabet. * @param c char to be located * @return true iff the given character is in this Alphabet */ public boolean contains(char c) { // indexOf returns a non-negative result if the char // is found in the String return (alpha.toString().indexOf(c) >= 0); } /** * Determines the position of a given character in this Alphabet. * @param c char to be located * @return non-negative integer position of c in this * Alphabet, or -1 if c not found */ public int indexOf(char c) { return alpha.toString().indexOf(c); } /** * Returns the i-th symbol in this Alphabet. * @param i int position of interest * @return char at position i in this Alphabet * @pre 0 <= i < this.size() */ public char charAt(int i) { Assert.pre(0 <= i && i < this.size(),"0 <= i < alpha.size"); return alpha.charAt(i); } /** * Converts this Alphabet to a String of symbols. * @return a String version of this Alphabet with no punctuation * or spaces */ public String toString() // post: returns a String copy of alphabet symbols { return alpha.toString(); } /** * Converts this Alphabet to a String of symbols punctuated with braces * and commas. * @return a pretty String version of this Alphabet */ public String toPrettyString() { StringBuffer returnString = new StringBuffer(); returnString.append("{"); for(int i=0; i= inString.length() * @post if every character in inString is a symbol in this Alphabet, * the initial segment of array will contain the position numbers * of the corresponding characters in inString */ public boolean indexArray(String inString, int[] array) { Assert.pre(array.length >= inString.length(), "array is at least as long as inString"); int index; // for each character in inString ... for(int i=0; i= 0 */ public Vector getLanguage(int n) { Assert.pre(n >= 0, "n is non-negative"); Vector v = new Vector(); v.add(new StringBuffer("")); // first add the empty string int finger = 0; // pointer for generating more strings StringBuffer old = (StringBuffer) v.get(finger); StringBuffer newString; // currently a null reference while(old.length() < n) // use old to generate more strings in language { // for each symbol in Alphabet... for(int i=0; i