对于作业,我必须设计一个DPDA,要求用户输入转换函数并说明最终状态。一切似乎已经在工作,除了当我运行它并开始输入输入时,一个字符的第一个输入似乎推动了需要被推入堆栈的一个额外副本。同样的事情发生在堆栈的第一个弹出窗口,它似乎弹出一个额外的。所以从长远来看它仍然可以工作,但我不知道额外的char来自哪里。
这是负责输入和输出的Driver类的部分
DPDA dad = new DPDA(0, states, transitions, stack, finalStates2);
System.out.print("Current status " + dad.getCurrentState() + ":" + dad.getStack().toString() + ", Enter input: ");
String nextInput = input.next();
while(!nextInput.equals(".")){
dad.transition(nextInput);
System.out.print("Current status " + dad.getCurrentState() + ":" + dad.getStack().toString()+ ", Enter input: ");
nextInput = input.next();
}
if(dad.isFinalState()){
System.out.println("String accepted");
}else
System.out.println("String not accepted");
这是DPDA.transition功能
public void transition(String input){
for(int i = 0; i < transitions.size(); i++){
if(transitions.get(i).getCurrentState() == this.getCurrentState() &&
transitions.get(i).getInput().equals(input) &&
transitions.get(i).getTopStack()==this.getStack().peek()){
stack.pop();
this.pushStringOnToStack(transitions.get(i).getPush());
this.setCurrentState(transitions.get(i).getNextState());
}
else
System.out.println("No transition available");
}
}
这是pushStringOntoStack函数
public void pushStringOnToStack(String pushed){
for(int i = pushed.length()-1; i >=0 ; i--){
if(!pushed.equals(".")){
stack.push(pushed.charAt(i));
}
}
}
示例输入
4
ab
0 a $ 1 A$
1 a A 1 aA
1 a a 1 aa
1 b a 2 .
1 b A 3 .
2 b a 2 .
2 b A 3 .
-1
1 3 -1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(Equivalent to)
number of states = 4
alphabet, {a, b}
transition δ (0,a,$) = (1,"A$")
transition δ (1,a,A) = (1,"aA")
transition δ (1,a,a) = (1,"aa")
transition δ (1,b,a) = (2, λ)
transition δ (1,b,A) = (3, λ)
transition δ (2,b,a) = (2, λ)
transition δ (2,b,A) = (3, λ)
end of transition function
final states: {1, 3}
示例输出
Current status 0:$, Enter input: a
Current status 1:A$, Enter input: a
Current status 1:aA$, Enter input: a
Current status 1:aaA$, Enter input: a
Current status 1:aaaA$, Enter input: b
Current status 2:aaA$, Enter input: b
Current status 2:aA$, Enter input: b
Current status 2:A$, Enter input: b
Current status 3:$, Enter input: .
String accepted.