Toc Experiment
Toc Experiment
Toc Experiment
Outcomes: After going through this section, you should be able to:
Recognize, understand and make use of various type of DFA machine
Theory:
DFA, for each input symbol, one can determine the state to which the machine will move.
Hence,itiscalledDeterministicAutomaton.Asithasafinitenumberofstates,themachine is
called Deterministic Finite Machine or Deterministic FiniteAutomaton.
Instructions: NA
Output:
Program of 2’s compliment
#include <stdio.h>
#define SIZE 3
int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
int i, carry=1;
printf("Enter %d bit binary value: ", SIZE);
gets(binary);
for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';
printf("Original binary = %s\n", binary);
printf("Ones complement = %s\n", onesComp);
printf("Twos complement = %s\n", twosComp);
return 0;
}
Output:
Conclusion ;-
________________________________________________________________________
________________________________________________________________________
Q1. Design a mealy machine for 1's complement.
____________________________________________________________________________________
___________________________________________________
Aim / Title: Design a Program to create PDA machine that accept the well-formed parenthesis
Problem Statement: Construct a PDA for that accept the well-formed parenthesis
Objectives: Given an expression string exp , write a program to examine whether the pairs and
the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.
Outcomes:
Input: exp = “[()]{}{[()()]()}”
Output: Balanced
Input: exp = “[(])”
Output: Not Balanced
Pushdown Automata for the Language of Both Balanced Parentheses and Brackets
Pre-requisite: An automaton can use the stack to recognize balanced parenthesis • e.g.
(())() is balanced, but ())() and (() are not – On seeing a ( push it on the stack – On seeing a )
pop a ( from the stack – If attempt to pop an empty stack, reject – If stack not empty at the
end, reject
Theory:
Declare a character stack S.
Now traverse the expression string exp.
1. If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then push it to stack.
2. If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then pop from stack and if
the popped character is the matching starting bracket then fine else parenthesis are not
balanced.
3. After complete traversal, if there is some starting bracket left in stack then “not
balanced”
Program:
// CPP program to check for balanced parenthesis.
#include<bits/stdc++.h>
using namespace std;
4|Page
// Traversing the Expression
for (int i=0; i<expr.length(); i++)
{
if (expr[i]=='('||expr[i]=='['||expr[i]=='{')
{
// Push the element in the stack
s.push(expr[i]);
continue;
}
switch (expr[i])
{
case ')':
case '}':
case ']':
5|Page
// Check Empty Stack
return (s.empty());
}
if (areParanthesisBalanced(expr))
cout << "Balanced";
else
cout << "Not Balanced";
return 0;
}
TimeComplexity: O(n)
Auxiliary Space: O(n) for stack.
6|Page
Conclusion:
PDA:-
δ(q0,(,z0)=(q0,(z0)δ(q0,(,z0)=(q0,(z0)
δ(q0,(,()=(q0,(()δ(q0,(,()=(q0,(()
δ(q1,),()=(q1,ε)δ(q1,),()=(q1,ε)
δ(q0,ε,z0)=(qf,ε)δ(q0,ε,z0)=(qf,ε)
2. Define the pushdown automata for language {anbncn | n > 0} in Short.(only Machine)
................................................................................................................................................
................................................................................................................................
7|Page
EXPERIMENT-NO.8
Outcomes: First replace a 0 from front by X, and then keep moving right till you find a 1 and
replace this 1 by Y. Again, keep moving right till you find a 2, replace it by Z and move left.
Now keep moving left till you find a X. When you find it, move a right, then follow the same
procedure as above.
A condition comes when you find a X immediately followed by a Y. At this point we keep
moving right and keep on checking that all 1’s and 2’s have been converted to Y and Z. If not
then string is not accepted. If we reach $ then string is accepted.
Step-1:
Replace 0 by X and move right, Go to state Q1.
Step-2:
Replace 0 by 0 and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
Replace 1 by Y and move right, go to state Q2.
Step-3:
Replace 1 by 1 and move right, Remain on same state
Replace Z by Z and move right, Remain on same state
Replace 2 by Z and move right, go to state Q3.
Step-4:
Replace 1 by 1 and move left, Remain on same state
Replace 0 by 0 and move left, Remain on same state
Replace Z by Z and move left, Remain on same state
Replace Y by Y and move left, Remain on same state
Replace X by X and move right, go to state Q0.
Step-5:
If symbol is Y replace it by Y and move right and Go to state Q4
Else go to step 1
Step-6:
Replace Z by Z and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO TO FINAL
STATE Q5
8|Page
be accepted by this language. The beginning and end of string is marked by $ sign.
Theory: A Pushdown Automaton (PDA) is like an epsilon Non deterministic Finite Automata
(NFA) with infinite stack. PDA is a way to implement context free languages. Hence, it is
important to learn, how to draw PDA.
Instructions:1: On receiving an element, check if symbol scanned is ‘1’ and top of stack
also contain ‘1’ or if symbol scanned is ‘0’ and top of stack also contain ‘0’ then pop the
element from top of stack else move to dead state. Keep on repeating step 3 until string
becomes empty.
2: Check if symbol scanned is ‘$’ and stack does not contain any element then move to
final state else move to dead state.
Program(Machine):
Conclusion:
For construction of even length palindrome, user has to use Non Deterministic Pushdown
Automata (NPDA). A NPDA is basically an NFA with a stack added to it.
The NPDA for this language is identical to the previous one except for epsilon transition.
However, there is a significant difference, that this PDA must guess when to stop pushing
symbols, jump to the final state and start matching off of the stack. Therefore this machine is
decidedly non-deterministic.
Output:-
Input : 0 0 0 1 1 1 2 2 2 2
Output : Not accepted
Sample Viva Questions and Answers:
9|Page
4. Define Turing Machine With Short Example
______________________________________________________________________________
_____________________________________________________________________
_______________________________________________________________________
10 | P a g e
EXPERIMENT-NO.09
Outcomes: First replace a 0 from front by X, and then keep moving right till you find a 1 and
replace this 1 by Y. Again, keep moving right till you find a 2, replace it by Z and move left.
Now keep moving left till you find a X. When you find it, move a right, then follow the same
procedure as above.
A condition comes when you find a X immediately followed by a Y. At this point we keep
moving right and keep on checking that all 1’s and 2’s have been converted to Y and Z. If not
then string is not accepted. If we reach $ then string is accepted.
Step-1:
Replace 0 by X and move right, Go to state Q1.
Step-2:
Replace 0 by 0 and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
Replace 1 by Y and move right, go to state Q2.
Step-3:
Replace 1 by 1 and move right, Remain on same state
Replace Z by Z and move right, Remain on same state
Replace 2 by Z and move right, go to state Q3.
Step-4:
Replace 1 by 1 and move left, Remain on same state
Replace 0 by 0 and move left, Remain on same state
Replace Z by Z and move left, Remain on same state
Replace Y by Y and move left, Remain on same state
Replace X by X and move right, go to state Q0.
Step-5:
If symbol is Y replace it by Y and move right and Go to state Q4
Else go to step 1
11 | P a g e
Step-6:
Replace Z by Z and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO TO FINAL
STATE Q5
Theory: A Pushdown Automaton (PDA) is like an epsilon Non deterministic Finite Automata
(NFA) with infinite stack. PDA is a way to implement context free languages. Hence, it is
important to learn, how to draw PDA.
Instructions:1: On receiving an element, check if symbol scanned is ‘1’ and top of stack
also contain ‘1’ or if symbol scanned is ‘0’ and top of stack also contain ‘0’ then pop the
element from top of stack else move to dead state. Keep on repeating step 3 until string
becomes empty.
2: Check if symbol scanned is ‘$’ and stack does not contain any element then move to
final state else move to dead state.
Program(Machine):
12 | P a g e
Conclusion:
We have designed state transition diagram for anbncn | n ≥ 1 as follows:
1. Following Steps:
a. Mark 'a' with 'X' and move towards unmarked 'b'
b. Move towards unmarked 'b' by passing all 'a's
c. To move towards unmarked 'b' also pass all 'Y's if exist
2. Following Steps:
a. Mark 'b' with 'Y' and move towards unmarked 'c'
b. Move towards unmarked 'c' by passing all 'b's
c. To move towards unmarked 'c' also pass all 'Z's if exist
3. Following Steps:
a. Mark 'c' with 'Z' and move towards first 'X' (in left)
b. Move towards first 'X' by passing all 'Z's, 'b's, 'Y's and 'a's
c. When 'X' is reached just move one step right by doing nothing.
4. To check all the 'a's, 'b's and 'c's are over add loops for checking 'Y' and 'Z' after "we get 'X' followed by 'Y'"
To reach final state(qf) just replace BLANK with BLANK and move either direction
Output:-
Input : 0 0 0 1 1 1 2 2 2 2
Output : Not accepted
Sample Viva Questions and Answers:
13 | P a g e
7. Define Turing Machine With Short Example
______________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
14 | P a g e