How would i design a PDA that accepts balanced parenthesis and brackets for instance ([][]), I am having a hard time getting started. I need help writing transition functions for this problem. Any help is appreciated
Pushdown Automata is a finite automata with extra memory called stack which helps Pushdown automata to recognize Context Free Languages. A Pushdown Automata (PDA) can be defined as : Q is the set of states. ∑is the set of input symbols.
A push down automata (PDA) is a way to implement a context free grammar (CFG) in a similar way to design the deterministic finite automata (DFA) for a regular grammar. A DFA can remember a finite amount of information but a PDA can remember an infinite amount of information. An input tape. A control unit.
Step 1 − Convert the productions of the CFG into GNF. Step 2 − The PDA will have only one state {q}. Step 3 − The start symbol of CFG will be the start symbol in the PDA. Step 4 − All non-terminals of the CFG will be the stack symbols of the PDA and all the terminals of the CFG will be the input symbols of the PDA.
I normally would not do someone's entire homework for them, but the reality is that when it comes to automata, even if I do, it won't help you much unless you really do understand how these things work and the sad truth is that most schools don't teach them well to begin with.
Let's think about how this PDA operates and forget about states and transitions and whatnot for now: When our PDA gets input it should work like this:
$
for this example) then our PDA accepts the string: it's a properly balanced string of parentheses and brackets.(
or a [
then push the input onto the stack and look at the next input character.)
then:
(
pop the top of the stack, and look at the next input.]
then:
[
pop the top of the state, and look at the next input. Now, knowing what our PDA has to do let's try to think about how to describe our PDA more formally. We will assume that:
(
, )
, [
and ]
}$
(
, [
} ∪ ZUsing a notation similar to what is described on http://en.wikipedia.org/wiki/Pushdown_automaton for PDA state transitions we can think about the states and how things flow:
(q0, ε, top=$
, ACCEPT, nil) This tells our PDA: when you are in state q0 and there is no more input and the top of the stack is a $
go to the ACCEPT state, leaving the stack unchanged.
(q0, ε, top=(
, REJECT, nil) This tells our PDA: when you are in state q0 and there is no more input and the top of the stack is a (
go to the REJECT state, leaving the stack unchanged.
(q0, ε, top=[
, REJECT, nil) This tells our PDA: when you are in state q0 and there is no more input and the top of the stack is a [
go to the REJECT state, leaving the stack unchanged.
(q0, (
, top=top, q0, push (
) This tells our PDA: when you are in state q0 and the input is a (
then, regardless of what is at the top of the stack, go to the state q0 and push a (
onto the stack.
(q0, [
, top=top, q0, push [
) This tells our PDA: when you are in state q0 and the input is a [
then, regardless of what is at the top of the stack, go to the state q0 and push a [
onto the stack.
(q0, )
, top=(
, q0, pop) This tells our PDA: when you are in state q0 and the input is a )
and the top of the stack is a (
then go to the state q0, and pop the top of the stack off.
(q0, )
, top=[
, REJECT, nil) This tells our PDA: when you are in state q0 and the input is a )
and the top of the stack is a [
then go to the REJECT stack, leaving the stack unchanged.
(q0, )
, top=$
, REJECT, nil) This tells our PDA: when you are in state q0 and the input is a )
and the top of the stack is a $
then go to the REJECT stack, leaving the stack unchanged.
(q0, ]
, top=[
, q0, pop) This tells our PDA: when you are in state q0 and the input is a ]
and the top of the stack is a [
then go to the state q0, and pop the top of the stack off.
(q0, ]
, top=(
, REJECT, nil) This tells our PDA: when you are in state q0 and the input is a ]
and the top of the stack is a (
then go to the REJECT stack, leaving the stack unchanged.
(q0, ]
, top=$
, REJECT, nil) This tells our PDA: when you are in state q0 and the input is a ]
and the top of the stack is a $
then go to the REJECT stack, leaving the stack unchanged.
We could have achieved this using more states, but it's interesting to note that one "processing" state is enough.
You may also want to note that depending on your instructor, you may not be required to explicitly add a REJECT state, although it's good form to do so.
I hope this helps.
This might help you get started:
bool check_and_pop(char c) {
if (top() == c) {
pop();
return true;
}
return false;
}
int check_input() {
char c;
while ((c = getc())) {
switch (c) {
case '(': case '{': case '[': push(c); break;
case ')':
if (!check_and_pop('(')
return REJECT;
break;
case '}':
if (!check_and_pop('{')
return REJECT;
break;
// ...
}
// end of input, check the stack to accept/reject
if (stack_size() == 0)
return ACCEPT;
else
return REJECT;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With