/*
* 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 pumping.cf;
import pumping.*;
/**
* The context-free pumping lemma for L =
* {w1cw2cw3cw4, :
* w1 = w2 or
* w3 = w4,
* wi ∈ {a, b}*,
* |wi| > 0}.
*
* @author Chris Morgan
*/
public class W1CW2CW3CW4 extends ContextFreePumpingLemma{
public String getTitle()
{
return "w1cw2cw3cw4 : w1 = w2 or w3 = w4, wi element_of {ab}*, |wi| >= 5";
}
public String getHTMLTitle()
{
return "w1cw2cw3cw4, : " +
"w1 = w2 or " +
"w3 = w4, " +
"wi "+ ELEMENT_OF + " " + AB_STAR +
", |wi| > 0";
}
public void setDescription()
{
partitionIsValid = false;
explanation = "For any m value, a possible value for w is \"am" +
"bmcambmcacb\". If either v or y " +
"together span two 'wn's or span less but possess a \"c\", then pumping that value could result " +
"in more or less than three \"c\"s, which is not permissible. If either v or y span " +
"'w3' or 'w4', then if i = 0, |'w3'| = 0 or |'w4'| = 0. " +
"If either v or y span 'w1' or 'w2', then for any i " + NOT_EQUAL +
" 1, 'w1' " + NOT_EQUAL + " 'w2'. Thus, this language is not context-free.";
}
protected void addCases()
{
// TODO Auto-generated method stub
}
protected void chooseW()
{
w = pumpString("a", m) + pumpString("b", m) + 'c'
+ pumpString("a", m) + pumpString("b", m) + "cacb";
}
public void chooseDecomposition()
{
String[] wArray = getWs(w);
int[] decomp = checkIfPossibility(wArray[0], wArray[1], 0);
if (decomp!=null) {
setDecomposition(decomp);
return;
}
decomp = checkIfPossibility(wArray[2], wArray[3], wArray[0].length() + wArray[1].length() + 2);
if (decomp!=null) {
setDecomposition(decomp);
return;
}
super.chooseDecomposition();
}
public void chooseI()
{
i = 0;
}
/**
* Will return a String[] containing all the w[k] values from w[0]...w[3]
* in the given string.
*
* @param s the string to check.
* @return the w[k] values.
*/
private String[] getWs(String s)
{
String[] w = new String[4];
String temp = s;
int c;
for (int i=0; i<3; i++) {
c = temp.indexOf('c');
if (c == -1)
return null;
w[i] = temp.substring(0, c);
temp = temp.substring(c+1);
}
w[3] = temp;
return w;
}
/**
* Checks to see if it is possible to find a decomposition with the two w strings given.
*
* @param s0 - the first string
* @param s1 - the second string
* @param shift - the amount to shift over the decomposition, based on the position of the first
* string.
* @return null if no decomposition is adequate, the decomposition if it is.
*/
private int[] checkIfPossibility(String s0, String s1, int shift) {
int x;
String first, last;
if (s0.length() == 1 && s1.length() == 1)
return null;
else if (s0.length() == 1)
return new int[]{shift+s0.length()+1, 1, 0, 0};
else if (s1.length() == 1 || !s0.equals(s1))
return new int[]{shift, 1, 0, 0};
else if (m >= s1.length() + 2)
return new int[] {shift, 1, s0.length(), 1};
//Otherwise, check to see if there is a pattern.
for (int i=s1.length()-m+2; i