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
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 :-)