Showing posts with label Automata. Show all posts
Showing posts with label Automata. Show all posts

Wednesday, May 2, 2012

Context Free Grammar



Context Free Grammar

Context Free grammar or CGF, G is represented by four components that is G= (V, T, P, S), where V is the set of variables, T the terminals, P the set of productions and S the start symbol.
Example: The grammar Gpal for palindromes is represented by
Gpal = ({P}, {0, 1}, A, P)
Where A represents the set of five productions
1. P
2. P0
3. P1
4. P0P0
5. P1P1 
Derivation using Grammar 

Example 1: Leftmost Derivation
The inference that a * (a+b00) is in the language of variable E can be reflected in a derivation of that string, starting with the string E. Here is one such derivation:

E E * E I * E a * E a * (E) a * (E + E) a * (I + E) a * (a + E) a * (a + I) a * (a + I0) a * (a + I00) a * (a + b00)
Leftmost Derivation - Tree


Example 2: Rightmost Derivations
The derivation of Example 1 was actually a leftmost derivation. Thus, we can describe the same derivation by:
E E * E E *(E) E * (E + E)
E * (E + I) E * (E +I0) E * (E + I00) E * (E + b00)
E * (I + b00) E * (a +b00) I * (a + b00) a * (a + b00)
We can also summarize the leftmost derivation by saying
E a * (a + b00), or express several steps of the derivation by expressions such as E * E a * (E).
Rightmost Derivation - Tree

There is a rightmost derivation that uses the same replacements for each variable, although it makes the replacements in different order. This rightmost derivation is:
E E * E E * (E) E * (E + E)
E * (E + I) E * (E + I0) E * (E + I00) E * (E + b00)
E * (I + b00) E * (a + b00) I * (a + b00) a * (a + b00)
This derivation allows us to conclude E a * (a + b00)
Consider the Grammar for string(a+b)*c
EE + T | T
T T * F | F
F ( E ) | a | b | c
Leftmost Derivation
ETT*FF*F(E)*F(E+T)*F(T+T)*F(F+T)*F (a+T)*F (a+F)*F (a+b)*F(a+b)*c
Rightmost derivation
ETT*FT*cF*c(E)*c(E+T)*c(E+F)*c(E+b)*c(T+b)*c(F+b)*c(a+b)*c
Example 2:
Consider the Grammar for string (a,a)
S->(L)|a
L->L,S|S
Leftmost derivation
S(L)(L,S)(S,S)(a,S)(a,a)
Rightmost Derivation
S(L)(L,S)(L,a)(S,a)(a,a)
The Language of a Grammar
If G(V,T,P,S) is a CFG, the language of G, denoted by L(G), is the set of terminal strings that have derivations from the start symbol.
L(G) = {w in T | S w}
Sentential Forms
Derivations from the start symbol produce strings that have a special role called “sentential forms”. That is if G = (V, T, P, S) is a CFG, then any string in (V T)* such that S a is a sentential form. If S a, then is a left – sentential form, and if S a , then is a right – sentential form. Note that the language L(G) is those sentential
forms that are in T*; that is they consist solely of terminals.
For example, E * (I + E) is a sentential form, since there is a derivation
E E * E E * (E) E * (E + E) E * (I + E)
However this derivation is neither leftmost nor rightmost, since at the last step, the middle E is replaced.
As an example of a left – sentential form, consider a * E, with the leftmost derivation.
E E * E I * E a * E
Additionally, the derivation
E E * E E * (E) E * (E + E)
Shows that
E * (E + E) is a right – sentential form.
Ambiguity
A context – free grammar G is said to be ambiguous if there exists some w L(G) which has at least two distinct derivation trees. Alternatively, ambiguity implies the existence of two or more left most or rightmost derivations.
Ex:-
Consider the grammar G=(V,T,E,P) with V={E,I}, T={a,b,c,+,*,(,)}, and productions.
EI,
EE+E,
EE*E,
E(E),
Ia|b|c

Consider two derivation trees for a + b * c.

Now unambiguous grammar for the above
Example:
ET, TF, FI, EE+T, TT*F,
F(E), Ia|b|c
Inherent Ambiguity
A CFL L is said to be inherently ambiguous if all its grammars are ambiguous
Example:Condider the Grammar for string aabbccdd
SAB | C
A aAb | ab
BcBd | cd
C aCd | aDd
D->bDc | bc
Parse tree for string aabbccdd

Applications of Context – Free Grammars
  • Parsers
  • The YACC Parser Generator
  • Markup Languages
  • XML and Document type definitions

Thursday, April 5, 2012

Conversion from NFA to DFA


Let MN = (QN, ΣN, δN, q0, AN) be an NFA and accepts the language L(MN). There should be an equivalent DFA MD = (QD, ΣD, δD, q0, AD) such that L(MD) = L(MN). The procedure to convert an NFA to its equivalent DFA is shown below:

Step1:
The start state of NFA MN is the start state of DFA MD. So, add q0(which is the start state of NFA) to QD and find the transitions from this state. The way to obtain different transitions is shown in step2.
Step2:
For each state [qi, qj,….qk] in QD, the transitions for each input symbol in Σ can be obtained as shown below:
  1. δD([qi, qj,….qk], a) = δN(qi, a) U δN(qj, a) U ……δN(qk, a)
= [ql, qm,….qn] say.
  1. Add the state [ql, qm,….qn] to QD, if it is not already in QD.
  2. Add the transition from [qi, qj,….qk] to [ql, qm,….qn] on the input symbol a iff the state [ql, qm,….qn] is added to QD in the previous step.
Step3:
The state [qa, qb,….qc] QD is the final state, if at least one of the state in qa, qb, ….. qc AN i.e., at least one of the component in [qa, qb,….qc] should be the final state of NFA.
Step4:
If epsilon () is accepted by NFA, then start state q0 of DFA is made the final state.

Obtain an NFA to accept the following language L = {w | w ∈ abab(power)n or aba(power)n where n >=0}


The machine to accept either ababn or aban where n 0 is shown below:


Non deterministic finite automata(NFA)


Definition: An NFA is a 5-tuple or quintuple M = (Q, Σ, δ, q0, A) where
Q is non empty, finite set of states.
Σ is non empty, finite set of input alphabets.
δ is transition function which is a mapping from
Q x {Σ U ε} to subsets of 2Q. This function shows
the change of state from one state to a set of states
based on the input symbol.
q0 Q is the start state.
A Q is set of final states.

Acceptance of language

Definition: Let M = (Q, Σ, δ, q0, A) be a DFA where Q is set of finite states, Σ is set of input alphabets (from which a string can be formed), δ is transition function from Q x {ΣU} to 2Q, q0 is the start state and A is the final or accepting state. The string (also called language) w accepted by an NFA can be defined in formal notation as:


L(M) = { w | w Σ*and δ*(q0, w) = Q with atleast one
Component of Q in A}

Regular language and Applications of Finite Automata


Definition: Let M = (Q, Σ, δ, q0, A) be a DFA. The language L is regular if there exists a machine M such that L = L(M).

* Applications of Finite Automata *

String matching/processing
Compiler Construction
The various compilers such as C/C++, Pascal, Fortran or any other compiler is designed using the finite automata. The DFAs are extensively used in the building the various phases of compiler such as
  • Lexical analysis (To identify the tokens, identifiers, to strip of the comments etc.)
  • Syntax analysis (To check the syntax of each statement or control statement used in the program)
  • Code optimization (To remove the un wanted code)
  • Code generation (To generate the machine code)


Other applications
The concept of finite automata is used in wide applications. It is not possible to list all the applications as there are infinite number of applications. This section lists some applications:
  1. Large natural vocabularies can be described using finite automaton which includes the applications such as spelling checkers and advisers, multi-language dictionaries, to indent the documents, in calculators to evaluate complex expressions based on the priority of an operator etc. to name a few. Any editor that we use uses finite automaton for implementation.
  2. Finite automaton is very useful in recognizing difficult problems i.e., sometimes it is very essential to solve an un-decidable problem. Even though there is no general solution exists for the specified problem, using theory of computation, we can find the approximate solutions.
  3. Finite automaton is very useful in hardware design such as circuit verification, in design of the hardware board (mother board or any other hardware unit), automatic traffic signals, radio controlled toys, elevators, automatic sensors, remote sensing or controller etc.
  4. In game theory and games wherein we use some control characters to fight against a monster, economics, computer graphics, linguistics etc., finite automaton plays a very important role.

DFA Examples


Draw a DFA to accept string of 0’s and 1’s ending with the string 011.
Obtain a DFA to accept strings of a’s and b’s having a sub string aa
Obtain a DFA to accept strings of a’s and b’s except those containing the substring aab.
 
 
Obtain a DFA to accept strings of a’s and b’s having even number of a’s and b’s
 

DFA to accept strings of a’s and b’s starting with the string ab


Obtain a DFA to accept strings of a’s and b’s starting with the string ab 


Transition diagram to accept string ab(a+b)*


So, the DFA which accepts strings of a’s and b’s starting with the string ab is given by M = (Q, Σ , δ, q0, A) where
Q = {q0, q1, q2, q3}
Σ = {a, b}
q0­ is the start state
A = {q2}.
q3=Dead State









Deterministic Finite Automata (DFA)

Definition: A DFA is 5-tuple or quintuple M = (Q, Σ, δ, q0, A) where
Q is non-empty, finite set of states.
Σ is non-empty, finite set of input alphabets.
δ is transition function, which is a mapping from Q x Σ to Q.
q0 Q is the start state.
A Q is set of accepting or final states.
Note: For each input symbol a, from a given state there is exactly one transition (there can be no transitions from a state also) and we are sure (or can determine) to which state the machine enters. So, the machine is called Deterministic machine. Since it has finite number of states the machine is called Deterministic finite machine or Deterministic Finite Automaton or Finite State Machine (FSM).
The language accepted by DFA is
L(M) = { w | w Σ* and δ*(q0, w) A }
The non-acceptance of the string w by an FA or DFA can be defined in formal notation as:

L(M) = { w | w Σ* and δ*(q0, w) A }

Tuesday, March 27, 2012

DFA of Binary number divisible by 5 (Details)


Question:  
Construct DFA  to accept set of all strings over alphabet set {0,1}( i.e, binary integer) is divisible by 5 eg 110, 1010,....?
OR
Construct DFA of strings whose binary interpretation is divisible by 5 over alphabet set {0,1}.
OR
Draw a DFA accepting the following languages over the alphabet {0,1}The set of all strings that, when interpreted as a binary integer, is a multiple of 5. 

Answer:

First we try to look the table, how a binary number is changed with respect to divisibility by 5, when 0 or 1 is placed at right:
---------------------------------------------------------------------------------------------------------
Number | Divisibility(Modulo) |  Inserted 0/(Modulo) | Inserted 1/(Modulo)    |
---------------------------------------------------------------------------------------------------------
0                Mod_0                          00 (Mod_0)                    01(Mod_1)
1                Mod_1                          10 (Mod_2)                    11(Mod_3)
10              Mod_2                          100 (Mod_4)                  101(Mod_0)
11              Mod_3                          110 (Mod_1)                  111(Mod_2)
100            Mod_4                          1000 (Mod_3)                1001(Mod_4)

101            Mod_0                          1010 (Mod_0)                1011(Mod_1)
110            Mod_1                          1100 (Mod_2)                1101(Mod_3)
111            Mod_2                          1110 (Mod_4)                1111(Mod_0)
1000          Mod_3                          10000 (Mod_1)              10001(Mod_2)
1001          Mod_4                          10010 (Mod_3)              10011(Mod_4)

1010          Mod_0                          10100 (Mod_0)              10101(Mod_1)
1011          Mod_1                          10110 (Mod_2)              10111(Mod_3)
1100          Mod_2                          11000 (Mod_4)              11001(Mod_0)
1101          Mod_3                          11010 (Mod_1)              11011(Mod_2)
1110          Mod_4                          11100 (Mod_3)              11101(Mod_4)

1111          Mod_0                          11110 (Mod_0)              11111(Mod_1)
10000        Mod_1                          100000 (Mod_2)            100001(Mod_3)
 -------------------------------------------------------------------------------------------------
And so on....


The State Transition Table is:

-----------------------------------------------------------------------------------
             Mod_0          Mod_1       Mod_2      Mod_3        Mod_4
------------------------------------------------------------------------------------
0           Mod_0           Mod_2      Mod_4      Mod_1       Mod_3
------------------------------------------------------------------------------------
1           Mod_1           Mod_3       Mod_0     Mod_2       Mod_4
------------------------------------------------------------------------------------


Finally the DFA of binary number divisible by 5 is :
 
Where MOD_0 is the final state.