2

I've been studying Computation Theory, and in an exercise I am asked to construct a PDA that accepts the language $L=\{x\in(a,b), \#_a(x) = 2\#_b(x)\}$, where $x$ is the input string and $\#_a(x),\#_b(x)$ the number that $a$ or $b$ appears in the $x$ string.

That means that the following sequences should all be accepted:

aab aba bbaabaaaa baabaa abbaaa

My problem: I've found examples of PDAs being constructed for $a^nb^n$ and even $a^{2n}b^n$ which is pretty close to what I'm looking for, but these languages all have a predetermined sequence of the $a$s coming first, then the $b$s.

I've been trying for literally hours to build a PDA that circles between how many $a$s and $b$s it has, and how many it still needs, but every attempted solution has been wrong (situations where consequent same letters cause pops on empty stacks, halting the PDA).

Any information or methodical approaches on constructing a PDA out of a language like that? I'm at my wit's end.

Raphael
  • 72,336
  • 29
  • 179
  • 389
  • Hint only for now: if you have $a$ you push $a$, if you have second $a$, you push $A$. So how about negative number of $a$ if you start from $b$? – Evil Sep 12 '15 at 19:12
  • @EvilJS You mean b popping $a$ or $A$? What's the point of $b$ inserting negative values if you can only push or pop? – Dimitris Sfounis Sep 12 '15 at 19:51
  • You have to remember how many $a$'s or $b$'s you have. Encode it in terms of $a$. $b$ is not popping $a$ but is popping $A$. If you have $b$ and then add $a$ you are expecting another $a$ to make it even. $a$ = 0.5, $A$ = 1, $b$ = -1, do you follow? – Evil Sep 12 '15 at 19:57
  • I've been trying to solve it like this, yes, always measuring how many $a$s and $b$s I have, then expecting the appropriate number of elements. But I always end up in situations of a state popping the stack, encountering an unexpecting value and halting. A PDA halts when popping the wrong value, right? – Dimitris Sfounis Sep 12 '15 at 20:06
  • Why should it pop unexpected value? You have probably missed some situation. It always should go to the end and state determine if it is ok. Oh, and what you do with $bbaaaa$? – Evil Sep 12 '15 at 20:09
  • Wait, you mean I should construct an Automaton that goes $a, X, X+0.5$ and $b, X, X-1$? Then exits to an accept state if $ε, 0, ε$? (Correct me if I'm wrong, this triple notation means element_encountered, pop_value,push_value) – Dimitris Sfounis Sep 12 '15 at 20:14
  • Here is a hint: It's very hard to memorize two numbers with a PDA. If you think about it, the PDA for ${a^nb^n | n\in\mathbb{N}}$ only memorizes one number. So can you think of a way of accepting $a$'s and $b$'s in any order, just keeping track of a single number that would contain sufficient information for accepting or rejecting in the end? – phs Sep 13 '15 at 08:46
  • @phs Since yesterday, I was able to produce a Pushdown Automaton through transition tables and one that works with a stack Sum. Push 0 as it begins, and with every $a$ pop the top X and push X+0.5, with every $b$ pop the top X and push X-1. Continue to final state with ε, 0->ε. However, I don't know if this is a valid Automaton (can a PDA be instructed to pop whatever value, and push whatever value plus or minus something?) – Dimitris Sfounis Sep 13 '15 at 13:41
  • 1
    @DimitrisSfounis stack alphabet must be a finite set. – Terence Hang Sep 13 '15 at 14:58
  • possible duplicate: How to prove that a language is context-free?. Have you tried the techniques there? Can you make an argument that your question is not answered by the techniques listed in the answers there? – D.W. Sep 14 '15 at 06:07
  • What does that question have to do with anything, D.W? My question regards constructing a distinct Automaton, not proving that a language is context free. – Dimitris Sfounis Sep 14 '15 at 06:18
  • Hint: use two stack symbols to encode how many more as resp. bs than the other you have seen. – Raphael Sep 14 '15 at 08:31
  • @DimitrisSfounis, that question shows how to construct a context-free grammar or pushdown automaton for the language. (If you can get a context-free grammar, you can convert it mechanically to a pushdown automaton using standard constructions found in automata theory textbooks everywhere.) So it seems like it's exactly your situation. You do have to read the answers, not just read the title of the question; they show how to do more than just prove the language is a CFL -- the way they prove the language is context-free is by constructing an explicit CFG or PDA. – D.W. Sep 14 '15 at 15:53

1 Answers1

1

GPDA$\to$PDA Approach

See you have got a solution from chat. But the first thing popped my mind is a Generalized_pushdown_automaton(GPDA), which can be constructed pretty straight forward.

GPDA construction

$M=(Q,\Sigma,\Gamma,\delta,q_0,Z,\{q_f\})$, where $Q=\{q_0,q_f\},\Sigma=\{a,b\},\Gamma=\{a,A,Z\}$,$\delta$ includes following rules:

  1. $(q_0,a,Z)\to(q_0,aZ)$
  2. $(q_0,b,Z)\to(q_0,AAZ)$
  3. $(q_0,a,a)\to(q_0,aa)$
  4. $(q_0,b,A)\to(q_0,AAA)$
  5. $(q_0,a,A)\to(q_0,\epsilon)$
  6. $(q_0,b,aa)\to(q_0,\epsilon)$
  7. $(q_0,b,aZ)\to(q_0,AZ)$
  8. $(q_0,\epsilon,Z)\to(q_f,\epsilon)$

GPDA$\to$PDA convertion

Using the algorithm provided on Wikipedia, we can convert it to PDA easily. Only 6. and 7. need to convert:

6.1 $(q_0,b,a)\to(p_0,\epsilon)$

6.2 $(p_0,\epsilon,a)\to(q_0,\epsilon)$

7.2 $(p_0,\epsilon,Z)\to(q_0,AZ)$

Resulting PDA is $M=(Q',\Sigma,\Gamma,\delta',q_0,Z,\{q_f\})$ where $Q'=\{q_0,p_0,q_f\}$ and $\delta'=(\delta\setminus\{6,7\})\cup\{6.1,6.2,7.2\}$. The constructed PDA is non-deterministic.

Convert to DPDA

To convert to DPDA, the only affected transitions are 1. 2. and 8 in above rules:

$M=(Q,\Sigma,\Gamma,\delta,q_0,Z,\{q_f\})$, where $Q=\{q_0,p_0,q_f\},\Sigma=\{a,b\},\Gamma=\{a,A,Z\}$,$\delta$ includes following rules:

  1. $\mathbf{(q_0,\epsilon,Z)\to(q_f,Z)}$
  2. $(q_0,a,a)\to(q_0,aa)$
  3. $(q_0,b,A)\to(q_0,AAA)$
  4. $(q_0,a,A)\to(q_0,\epsilon)$
  5. $(q_0,b,a)\to(p_0,\epsilon)$
  6. $(p_0,\epsilon,a)\to(q_0,\epsilon)$
  7. $(p_0,\epsilon,Z)\to(q_0,AZ)$
  8. $\mathbf{(q_f,a,Z)\to(q_0,aZ)}$
  9. $\mathbf{(q_f,b,Z)\to(q_0,AAZ)}$
Terence Hang
  • 789
  • 4
  • 7