Toc Experiment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

EXPERIMENT NO 5

Aim / Title: construct DFA for 2’s complement in binary number .

Problem Statement: Study and execute DFA

Objectives: To get familiarity with DFA in Toc.

Outcomes: After going through this section, you should be able to:
Recognize, understand and make use of various type of DFA machine

Pre-requisite: Basic knowledge about Autometa.

Hardware requirements: Intel G645 Dual Core 3.0 GHz


4 GB RAM, 500 GB Hitachi HDD

Software requirements: Jflap

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

Program:design a program to find 2’s complementof a given binary number.

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.

____________________________________________________________________________________

Q2.Finite automata requires minimum _______ number of stacks.


a) 1
b) 0
c) 2
d) None of the mentioned

Q3. Which of the following is an application of Finite Automaton?


a) Compiler Design
b) Grammar Parsers
c) Text Search
d) All of the mentioned

___________________________________________________

Roll Nameof Date of Date of Grade Sign of Signof


No. Student Performance Evaluation Student Faculty
EXPERIMENT-NO.7

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

Hardware requirements: Previous Exp.


Software requirements: Previous Exp.

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;

// function to check if paranthesis are balanced


bool areParanthesisBalanced(string expr)
{
stack<char> s;
char x;

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;
}

// IF current current character is not opening


// bracket, then it must be closing. So stack
// cannot be empty at this point.
if (s.empty())
return false;

switch (expr[i])
{
case ')':

// Store the top element in a


x = s.top();
s.pop();
if (x=='{' || x=='[')
return false;
break;

case '}':

// Store the top element in b


x = s.top();
s.pop();
if (x=='(' || x=='[')
return false;
break;

case ']':

// Store the top element in c


x = s.top();
s.pop();
if (x =='(' || x == '{')
return false;
break;
}
}

5|Page
// Check Empty Stack
return (s.empty());
}

// Driver program to test above function


int main()
{
string expr = "{()}[]";

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,ε)

Sample Viva Questions and Answers:


1. Design Palindrome Using PDA. In Short(only Machine)
................................................................................................................................................
.........................................................................................................................

2. Define the pushdown automata for language {anbncn | n > 0} in Short.(only Machine)
................................................................................................................................................
................................................................................................................................

3. Define P and NP.

Roll Name of Date of Date of Grade Sign of Sign of


No. Student Performance Evaluation Student Faculty

7|Page
EXPERIMENT-NO.8

Aim / Title: Construct a Turing Machine for language L = {0n1n2n | n≥1}.


Problem Statement: Input: 0 0 1 1 2 2
Output: Accepted
Input: 0 0 0 1 1 1 2 2 2 2
Objectives: We will replace 0 by X, 1 by Y and 2 by Z

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

Pre-requisite: Turing Machine


The language L = {0n1n2n | n≥1} represents a kind of language where we use only 3 character,
i.e., 0, 1 and 2. In the beginning language has some number of 0’s followed by equal number of
1’s and then followed by equal number of 2’s. Any such string which falls in this category will

8|Page
be accepted by this language. The beginning and end of string is marked by $ sign.

Hardware requirements: Previous Exp.


Software requirements: Previous Exp.

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

5. Design Turing Machine for language {anb2n | n > 0} only Machine

______________________________________________________________________________

_____________________________________________________________________

6. Types of Turing Machine.

_______________________________________________________________________

Roll Name of Date of Date of Grade Sign of Sign of


No. Student Performance Evaluation Student Faculty

10 | P a g e
EXPERIMENT-NO.09

Aim / Title: Construct a Turing Machine for language L = {0n1n2n | n≥1}.


Problem Statement: Input: 0 0 1 1 2 2
Output: Accepted
Input: 0 0 0 1 1 1 2 2 2 2
Objectives: We will replace 0 by X, 1 by Y and 2 by Z

1. Mark 'a' then move right.


2. Mark 'b' then move right
3. Mark 'c' then move left
4. Come to far left till we get 'X'
5. Repeat above steps till all 'a', 'b' and 'c' are marked
6. At last if everything is marked that means string is accepted.

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

Pre-requisite: Turing Machine


The language L = {0n1n2n | n≥1} represents a kind of language where we use only 3 character,
i.e., 0, 1 and 2. In the beginning language has some number of 0’s followed by equal number of
1’s and then followed by equal number of 2’s. Any such string which falls in this category will
be accepted by this language. The beginning and end of string is marked by $ sign.

Hardware requirements: Previous Exp.


Software requirements: Previous Exp.

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

______________________________________________________________________________

8. Design Turing Machine for language {anb2n | n > 0} only Machine

__________________________________________________________________________

9. Types of Turing Machine.

__________________________________________________________________________

Roll Name of Date of Date of Grade Sign of Sign of


No. Student Performance Evaluation Student Faculty

14 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy