/* * JFLAP - Formal Languages and Automata Package * * * Susan H. Rodger * Computer Science Department * Duke University * August 27, 2009 * Copyright (c) 2002-2009 * All rights reserved. * JFLAP is open source software. Please see the LICENSE for terms. * */ package grammar.parse; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import javax.swing.tree.TreeNode; import grammar.Grammar; import grammar.Production; /** * Similar to BruteParser abstract class, UserParser abstract class is created to deal with User Parsing * * NOTE: This code is very similar to BruteParser and it would be better to combine two classes and extract hiearchy. * However, since Brute Parser is fully functional, I did not want to mess wtih BruteParser class. * * @author Kyung Min (Jason) Lee * */ public abstract class UserParser { /** Stuff for the possibilities. **/ private static final Production[] P = new Production[0]; private static final int[] S = new int[0]; /** Starting ParseNode **/ private static final ParseNode E = new ParseNode("", P, S); /** This is the grammar. */ protected Grammar myGrammar; /** The array of productions. */ protected Production[] myProductions; /** This is the target string. */ protected String myTarget; /** This should be set to done when the operation has completed. */ private boolean isDone = false; /** The "answer" to the parse question. */ private ParseNode myAnswer; /** * The "smaller" set, those symbols that may possibly reduce to nothing. */ protected Set mySmallerSet; /** The Production rule that the User has set to apply to the String **/ private Production myCurrentProduction; /** Integer variable that shows how many times the derivation has occured **/ private int myCount=0; /** This holds the list of nodes for the BFS. */ private LinkedList myQueue = new LinkedList(); /** * Constructor for UserParser abstract class. * This is intialized by sub-classes of UserParser class. * * @param grammar The grammar that is going to be used for parsing * @param target The target string that user is trying to derive */ public UserParser(Grammar grammar, String target) { initialize(grammar, target); } /** * Intialize all the variables before starting Parser. * This method is called from the constructor. * * @param grammar The grammar that is going to be used for parsing * @param target The target string that user is trying to derive */ private void initialize(Grammar grammar, String target) { for (int i = 0; i < target.length(); i++) if (!grammar.isTerminal(target.substring(i, i + 1))) throw new IllegalArgumentException( "String to parse has nonterminal " + target.substring(i, i + 1) + "."); if (grammar == null) return; myQueue.clear(); myAnswer=new ParseNode(grammar.getStartVariable(), P, S); myQueue.add(myAnswer); mySmallerSet = Collections.unmodifiableSet(Unrestricted .smallerSymbols(grammar)); myGrammar = grammar; myProductions = grammar.getProductions(); myTarget = target; } /** * This factory method will return a user parser appropriate for the * grammar. * * @param grammar * the grammar to get a brute force parser for * @param target * the target string */ public static UserParser get(Grammar grammar, String target) { if (Unrestricted.isUnrestricted(grammar)) { return new UnrestrictedUserParser(grammar, target); } return new RestrictedUserParser(grammar, target); } /** * Given an index of Productino rule array, * this method finds LHS variable of chosen production rule and * count how many of LHS variable is present in our current String. * * @param index Index of our production rule that is selected by user. * @return Return the count of LHS variables present in the String. */ public int checkValidAndParse(int index) { /* for (int i=0; i