views:

394

answers:

3

I'm studying for a test on PDA, and I want to know how to design a pushdown automaton that recognizes the following language:

L = {a^max(0,n-m)b^n a^m| n,m >=0}

How can I design a transition function to recognize if n-m is greater than 0?

And please, if you have some course materials with exercises of this level solved, put a link, my course materials suck and I'm facing a pretty tough test in two days.

+1  A: 

you can decide where to go from a current state based on the value on top of the stack, you use symbols on the stack to keep notes about the state of the parsing.

here is how i think this would work: this are symbols read from the input, THIS are symbols on a stack. the symbol X on the botoom of the stack means that n <= m do not confuse X with Z, which is the initial symbol of the stack and helps determine when the stack is empty.

there are probably some problems with this solution, but it the overall approach should be correct.

... and good luck with your test :-)


first you read all the a symbols from the beginning of the string and add A to a stack for each of them, or push X if there was no a

then you read all the b symbols:

  • if the stack is empty (Z is on top), B is on top or X is on top, then you push another B to the stack.
  • if stack has A on top, then you remove it.

the last step is to read the final a symbols.

  • if there is B on the stack, remove it.
  • if there is X on the stack, then keep it there
  • if the stack is empty (Z on top), then this must be the end of the string


another edit:

sorry if the above isn't clear ... i'll try to formalize it.

accepting states are (4) and (5), starting state is (1). and it's nondeterministic

and the transition rules:

state (1) : read the first batch of a symbols

  • (1) a; Z / AZ -> (1)
  • (1) a; A / AA -> (1)
  • (1) epsilon; A / A -> (2)
  • (1) epsilon; Z / Z -> (2)

state (2) : read b symbols

  • (2) b; Z / BZ -> (2)
  • (2) b; X / BX -> (2)
  • (2) b; B / BB -> (2)
  • (2) b; A / epsilon -> (2)
  • (2) epsilon; B / B -> (3)
  • (2) epsilon; X / X -> (3)
  • (2) epsilon; Z / Z -> (3)

state (3) : read the last as

  • (3) a; B / nothing -> (3)
  • (3) epsilon; X / X -> (4)
  • (3) epsilon; Z / Z -> (5)

state (4) : the trailing as if m > n

  • (4) a; X / X -> (4)

state (5) is for accepting the exact string when m < n

(and just to be absolutely clear -- when there is no way of a state and the reading cursor is not at the end of the word, then the word is not accepted)

This could maybe be made a bit simpler by using adidional states instead of the stack symbol X, but i guess you can do it yourself :-)

cube
You are using X as other people use Z right? I mean, the symbol being at the bottom of the stack when starting.
omgzor
no, that's another symbol :-) X is added to the stack only if n <= m.I kinda forgot about Z and assumed that you can tell that the stack is empty by some other means. Thanks for pointing it out, I'll fix that in the answer.
cube
"f the stack is empty (Z is on top), B is on top or X is on top, then you push another B to the stack."If there's X on top do I push BX or just B?
omgzor
or is it BB that I have to push? Sorry, another B is ambiguous.
omgzor
I added a more formal description of the automaton.
cube
Are you sure this language L can be accepted by a PDA?
Sean A.O. Harney
I'm not sure ... however i think that the automaton i described is a valid PDA and that it accepts the language in question ... of cause i could be wrong in any of these two assumptions.
cube
the problem statement says this language should be recognized by a PDA.
omgzor
A: 

I think that language L is a context-sensitive or type 1 language on the Chompsky hierarchy. I could be wrong.

Language L1 = { a^n b^n c^n : n >= 1 } is an example of a type 1 language., and this looks pretty similar. It sounds like a trick question to me! I don't think there is a type 2 or context-free grammar or PDA that could recognize or generate L.

Sean A.O. Harney
A: 

Easiest way is probably to write a grammar for the language and then build a PDA for that. Easiest way to write a grammar is to first split the language based on that 'max' so its easier to see what is going on

L = L1 \union L2
L1 = { b^n a^m | m >= n >= 0 }
L2 = { a^(n-m) b^n a^m | n >= m >= 0 }

now rewrite L1 and L2 to make them a bit simpler ( j = m-n, k = n-m )

L1 = { b^n a^n a^j | j,n >= 0 }
L2 = { a^k b^k b^m a^m | k,m >= 0 }

these turn into very simple grammars

L1 := BA A
L2 := AB BA
AB := a AB b | \epsilon
BA := b BA a | \epsilon
A  := a A | \epsilon
L  := L1 | L2

now build a PDA from them -- easiest to use an automated tool, but can be done manually if you need to show all the work

Chris Dodd