/* * 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 grammar.Grammar; import grammar.Production; import java.util.*; /** * CYK Parser * It parses grammar that is in CNF form and returns whether the String is accepted by language or not. * * @author Kyung Min (Jason) Lee * */ public class CYKParser { /** Production array that will contain all the productions of grammar */ private Production[] myProductions; /** Start variable of the grammar */ private static String START_VARIABLE; /** Length of the input String */ private int myTargetLength; /** Productions that leads to the answer */ private ArrayList myAnswerProductions; /** Map to store the result of subparts */ private HashMap > myMap; /** Input string that CYK is trying to parse */ private String myTarget; private OrderCorrectly myOrderComparator; /** * Constructor for CYK Parser * @param grammar Grammar that is going to be used in CYK Parsing (It has to be in CNF Form) */ public CYKParser(Grammar grammar) { myProductions=grammar.getProductions(); START_VARIABLE=grammar.getStartVariable(); // System.out.println("GRAMMAR = "+Arrays.asList(grammar.getProductions())); } /** * Check whether the grammar accepts the string or not * using DP */ public boolean solve(String target) { myMap=new HashMap >(); int targetLength=target.length(); myTargetLength=targetLength; myTarget=target; if (target.equals("")) return false; for (int i=0; i temp=new HashSet (); int count=0; for (int j=0; j tempSet=new HashSet (); for (int i=0; i temp2Set=new HashSet (); tempSet.add(myProductions[i].getLHS()); temp2Set.add("0"+A+"/"+key1); temp2Set.add("1"+B+"/"+key2); String tempKey=x+","+y+myProductions[i].getLHS(); if (myMap.get(tempKey)!=null) temp2Set.addAll(myMap.get(tempKey)); myMap.put(tempKey, temp2Set); } } } } } String key=x+","+y; myMap.put(key, tempSet); } /** * Method for getting the trace of how the parser achieved the target String * @return ArrayList of Productions that was applied to attain target String */ public ArrayList getTrace() { myAnswerProductions=new ArrayList (); myOrderComparator=new OrderCorrectly(); // System.out.println("WHOLE MAP = "+myMap); getMoreProductions(START_VARIABLE, "0,"+(myTargetLength-1)); // System.out.println(myAnswerProductions); return myAnswerProductions; } /** * Helper method of getTrace method which recursively backtracks how Parser achieved the target String * @param variable Variable that we are chekcing * @param location Location of the variable */ private void getMoreProductions(String variable, String location) { // //System.out.println("WHOLE MAP = "+myMap); if (myMap.get(location+variable)==null) { // //System.out.println("Location inside = "+location); // //System.out.println("Variable inside = "+variable); int loc=Integer.parseInt(location.substring(0,location.indexOf(","))); myAnswerProductions.add(new Production(variable, myTarget.substring(loc,loc+1))); return; } /* //System.out.println("Map = "+myMap.get(location+variable)); //System.out.println("Location = "+location); //System.out.println("Variable = "+variable);*/ ArrayList optionsA=new ArrayList (); ArrayList optionsB=new ArrayList (); String[] A=new String[2]; String[] B=new String[2]; for (String var : myMap.get(location+variable)) { if (var.startsWith("0")) optionsA.add(var); else optionsB.add(var); } Collections.sort(optionsA, myOrderComparator); Collections.sort(optionsB, myOrderComparator); // //System.out.println("AAA = "+optionsA); // //System.out.println("BBB = "+optionsB); boolean isDone=false; for (int i=0; i