Finite State Automata... Lexers/Parsers
How do we tackle the problem of recognizing languages while doing Lex/Yacc? We solve it by finite state automaton (FSA).
Before defining FSA, lets take a look at what is language?
A
language could be defined as a set of strings. Languages could be defined by a set of rules called as
grammer.
A finite state automaton is an abstract model of a simple machine used for recognizing simple syntactical structures or patterns in the text strings. Finite state machine (FSM) is all about states and transitions.
The machine can be in a finite number of states. It receives symbols as input, and the result of receiving a particular input in a particular state transitions itself to a specified new state. One of these is start state and other is final state. Others are intermidiate states. If the machine is in one of the finishing states and the input ends , it has ended successfully (or has accepted the input).
FSA could be one of the following:
- Non-deterministic (NFA) (Non-deterministic finite-state machines)
- Deterministic (DFA) (Deterministic finite-state machines)
Before going any further on FSA, let us look at some of the following definition that would enable better understanding about FSA:
(S) State state: State state of the machine
(F) Final state or Accept state: Finishing state of the machine. There could be more than one final states.
(I) Intermediate States: Intermediate states
(T) Token/string: tokens/string that machine waits for. Based on these tokens, they stays in the same state or transitions to next state.
(Tr) Transitions
Say, there are two tokens A and B. There are 4 states( 1,2,3,4).
1 is state state. 4 is the final state. 2 and 3 are intermediate states.
Following defines states, tokens representin transitions in the FSA:
Tr = {(1,A,2),
(1,B,4),
(2,A,2),
(2,B,3),
(3,A,3),
(3,B,4),
(4,A,3),
(4,B,4)}
Above is of the form (S,T,F) or (S,T,F) or (S,T,I) or (I,T,I) or (I,T,F), or (F,T,F). These are various transitions of this particular FSM model.
Above could be read as following:
If machine is 1 state, and it receives A, it transitions to state 2, or else, if it receives B, it transitions to final state. If the machine is in state 2 and receives A, it stays in the same state and wait for further tokens, or else, if it receives B, it transitions to state 4. and so on...
Thus, FSA could be represented as a transition table of states vs symbols where states represents a particular state and symbols represent a token.
The question, now is, what is DFA and NFA. What is difference between NFA or DFA?
Watch out for a detailed discussion... Enjoy <!:-)
The finite-state machine (FSM) enjoy a special place in computer science. It has proven to be a very useful model for many practical tasks and deserves to be among the tools of every practicing computer scientist. Many simple tasks, such as interpreting the commands typed into a keyboard or running a calculator, can be modeled by finite-state machines.
Some of the examples of FSM are Moore machine, Mealy machine etc.
Moore Machine is a FSM which produces output for each state.
Mealy Machine is a FSM which produces output for each transition.