1

I am studying Automata theory. DFAs and NFAs seem pretty straightforward to me, but I don't quite understand how to design push-down automata for context-free languages.

If I have context-free language where the input alphabet is $\{a,b\}$ $L = \{w \text{ where }w\text{ is NOT of the form }b^n a^n\}$

How would I design a push-down automata for it? What are the steps I would need to take, and what would it look like (formal description)?

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
am2021
  • 67
  • 3

2 Answers2

1

The PDA has two main states:

  • No $a$ has been encountered.
  • Some $a$ has been encountered.

While in the first state, it pushes $B$ onto the stack for any $b$ it encounters. It moves to the second state when encountering an $a$, at which point it removes $B$ from the stack for any $a$ it encounters (including the first $a$). If it ever realizes that it has seen more $a$s than $b$s, or if it encounters $b$ again, it can immediately accept (depending on your model, you might have to read the remainder of the input). After finishing reading the entire input, if the stack contains at least one $B$ then it accepts, and otherwise it rejects. You can implement this by requiring the PDA to pop at least one $B$ before accepting.

Formally describing this PDA depends on the exact definition of PDAs in your class, and in any case, it is an exercise which is best for you to do on your own.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
1

In a related question you obtained a context-free grammar for this language.

  • $S\to bSa \mid A\mid B$
  • $A\to aA \mid a$
  • $B\to bB \mid b$

Although I agree with Yuval that directly constructing a push-down automaton shows you understood the concepts of a PDA, I like to recall that there are direct constructions between PDA and CFG. As wikipedia mentions the construction from CFG to PDA is straightforward. The other direction is more tedious.

The construction in wikipedia is called expand-match. One obtains a PDA that accepts by empty stack that way, which is slightly nonstandard. I will add the details for a PDA with final state acceptance.

The new PDA has three states, initial state $q_0$, working state $q_1$ and accepting state $q_2$. The initial push-down symbol is $Z$. First step: push the axiom on the stack.

  • $(q_0,\varepsilon,Z, q_1, SZ)$

Now perform the CFG derivation on the stack (expand), checking the derived terminals with the tape symbols (match)

  • $(q_1,\varepsilon ,A,q_1,\alpha )$ for each rule $A\to \alpha$
  • $(q_1,a,a,q_1,\varepsilon )$ for each terminal symbol $a$

Finally, move to accept when the stack reaches bottom.

  • $(q_1,\varepsilon,Z,q_2,Z)$

As an example, the CFG production $S\to bSa$ is directly translated into the PDA instruction $(q_1,\varepsilon ,S,q_1,bSa )$.

Hendrik Jan
  • 30,578
  • 1
  • 51
  • 105