I tried googling finite state machines but having no formal training in computer engineering I must admit I was a bit lost. Do you have any additional pointers?
A FSM is one of the simplest forms of computers. The idea is that you have a finite amount of "state" information and a steady stream of inputs. Each input causes the state to change in a predictable way, based only on the current state and the current input, and causes a predictable output to occur.
So imagine that your inputs are single characters and your output is a single character or a "null" character. Here's a FSM that does what you want:
- states are
OUTSIDE
,INSIDEDOUBLE
andINSIDESINGLE
. - inputs are
"
,'
andx
. (WOLOG letx
stand for any other character.)
We have three states and three inputs so there are nine possible combinations.
- if we are
OUTSIDE
and getx
, stayOUTSIDE
and emitnull
. - if we are
OUTSIDE
and get"
, goINSIDEDOUBLE
and emitnull
. - if we are
OUTSIDE
and get'
, goINSIDESINGLE
and emitnull
. - if we are
INSIDEDOUBLE
and getx
, stayINSIDEDOUBLE
and emitx
- if we are
INSIDEDOUBLE
and get"
, goOUTSIDE
and emitnull
- if we are
INSIDEDOUBLE
and get'
, stayINSIDEDOUBLE
and emit'
- if we are
INSIDESINGLE
and getx
, stayINSIDESINGLE
and emitnull
- if we are
INSIDESINGLE
and get"
, stayINSIDESINGLE
and emitnull
- if we are
INSIDESINGLE
and get'
, goOUTSIDE
and emitnull
The only thing left is to say that the start state is OUTSIDE
.
So let's suppose the inputs are 1 " 2 " 3 ' 4 " 5 " ' 6
. The states and outputs are:
OUTSIDE
gets1
, emitsnull
, staysOUTSIDE
.OUTSIDE
gets"
, emitsnull
, goesINSIDEDOUBLE
.INSIDEDOUBLE
gets2
, emits2
, staysINSIDEDOUBLE
INSIDEDOUBLE
gets"
, emitsnull
, goesOUTSIDE
.OUTSIDE
gets3
, emitsnull
, staysOUTSIDE
.OUTSIDE
gets'
, emitsnull
, goesINSIDESINGLE
... fill in the rest yourself.
Is that enough of a sketch for you to write the code?