0% found this document useful (0 votes)
2 views

TOC Lab Manual

The document is a lab manual for the TOC subject, detailing various programming experiments for creating machines that accept specific binary and decimal inputs. It includes code examples for accepting strings with three consecutive ones, strings ending with '101', checking divisibility by 2 and 3, and counting 1's and 0's, among others. Additionally, it covers converting NFA to DFA and designing a PDA for well-formed parentheses.

Uploaded by

pawanyadav7015
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

TOC Lab Manual

The document is a lab manual for the TOC subject, detailing various programming experiments for creating machines that accept specific binary and decimal inputs. It includes code examples for accepting strings with three consecutive ones, strings ending with '101', checking divisibility by 2 and 3, and counting 1's and 0's, among others. Additionally, it covers converting NFA to DFA and designing a PDA for well-formed parentheses.

Uploaded by

pawanyadav7015
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Lab Manual

Faculty Name: Prof. Khushboo Verma

Subject: TOC

Subject Code: AL-601

Academic Year: Jan-June 2025

Semester: VI

Designation: Assistant Professor


TOCEXPERIMENTS

Exp1.Design a Program for creating machine that accepts three consecutive one.

#include <stdio.h>
#include <string.h>

int accepts_three_consecutive_ones(char
*binary_string) {
int state = 0; // Initial state

for (int i = 0; binary_string[i] != '\0'; i++) {


if (binary_string[i] == '1') {
state++; // Increase state on consecutive
1s
if (state == 3) {
return 1; // Accept if three consecutive
1s are found
}
} else {
state = 0; // Reset state if '0' is
encountered
}
}
return 0; // Reject if no three consecutive 1s
are found
}

int main() {
char binary_input[100];

printf("Enter a binary string: ");


scanf("%s", binary_input);

if
(accepts_three_consecutive_ones(binary_input))
{
printf("Accepted\n");
} else {
printf("Rejected\n");
}

return 0;
}
Exp2.Design a Program for creating machine that accepts the string always ending with 101.

#include <stdio.h>
#include <string.h>

int ends_with_101(char *binary_string) {


int length = strlen(binary_string);

// Check if the last three characters are "101"


if (length >= 3 &&
binary_string[length - 3] == '1' &&
binary_string[length - 2] == '0' &&
binary_string[length - 1] == '1') {
return 1; // Accepted
}
return 0; // Rejected
}

int main() {
char binary_input[100];

printf("Enter a binary string: ");


scanf("%s", binary_input);

if (ends_with_101(binary_input)) {
printf("Accepted\n");
} else {
printf("Rejected\n");
}
return 0;
}
Exp 3 Design a program for Mode 3 machine

#include <stdio.h>
#include <string.h>

// Function to determine if a binary string is


divisible by 3
int is_divisible_by_3(char *binary_string)
{
int state = 0; // Start at state q0 (remainder 0)

for (int i = 0; binary_string[i] != '\0'; i++) {


char bit = binary_string[i];
if (bit == '0') {
// Transition: state = (state * 2) % 3
state = (state * 2) % 3;
}
else if (bit == '1') {
// Transition: state = (state * 2 + 1) % 3
state = (state * 2 + 1) % 3;
}
else {
printf("Invalid input! Only binary digits (0
and 1) are allowed.\n");
return -1;
}
}

return (state == 0); // Accept if final state is q0


(remainder 0)
}
int main() {
char binary_input[100];

printf("Enter a binary number: ");


scanf("%s", binary_input);

if (is_divisible_by_3(binary_input)) {
printf("Accepted: The number is divisible by
3.\n");
} else {
printf("Rejected: The number is NOT divisible
by 3.\n");
}
return 0;
}
Exp4.Design a program for accepting decimal number divisible by 2.

#include <stdio.h>
#include <string.h>

// Function to check if a decimal number is divisible by 2


int is_divisible_by_2(char *decimal_string) {
int length = strlen(decimal_string);

// Get the last digit of the string


char last_digit = decimal_string[length - 1];

// Check if the last digit is even (0, 2, 4, 6, 8)


if (last_digit == '0' || last_digit == '2' || last_digit == '4' ||
last_digit == '6' || last_digit == '8') {
return 1; // Accepted
}
return 0; // Rejected
}

int main() {
char decimal_input[100];

printf("Enter a decimal number: ");


scanf("%s", decimal_input);

if (is_divisible_by_2(decimal_input)) {
printf("Accepted: The number is divisible by 2.\n");
} else {
printf("Rejected: The number is NOT divisible by 2.\n");
}

return 0;
}
Exp5.Design a program for creating a machine which accepts string having equal no. of 1’s and 0’s.

#include <stdio.h>
#include <string.h>

// Function to check if the string has equal


number of 1's and 0's
int is_equal_ones_zeros(char *binary_string)
{
int count = 0; // Counter to track difference

for (int i = 0; binary_string[i] != '\0'; i++)


{
if (binary_string[i] == '1') {
count++; // Increase for '1'
}
else if (binary_string[i] == '0')
{
count--; // Decrease for '0'
}
else {
printf("Invalid input! Only binary digits (0
and 1) are allowed.\n");
return -1; // Invalid input case
}
}

return (count == 0); // Accept if equal number


of 1s and 0s
}

int main()
{
char binary_input[100];

printf("Enter a binary string: ");


scanf("%s", binary_input);

if (is_equal_ones_zeros(binary_input))
{
printf("Accepted: The string has an equal
number of 1s and 0s.\n");
}
else
{
printf("Rejected: The string does NOT have
an equal number of 1s and 0s.\n");
}

return 0;
}
Exp6.Design a program for creating a machine which count number of 1’s and 0’s in a given string.

#include <stdio.h>
#include <string.h>

// Function to count 1's and 0's in a binary string


void count_ones_zeros(char *binary_string, int
*count_ones, int *count_zeros) {
*count_ones = 0;
*count_zeros = 0;

for (int i = 0; binary_string[i] != '\0'; i++) {


if (binary_string[i] == '1') {
(*count_ones)++; // Increment count of
1s
}
else if (binary_string[i] == '0') {
(*count_zeros)++; // Increment count of
0s
}
else {
printf("Invalid input! Only binary digits (0
and 1) are allowed.\n");
return;
}
}
}

int main() {
char binary_input[100];
int ones = 0, zeros = 0;

printf("Enter a binary string: ");


scanf("%s", binary_input);

count_ones_zeros(binary_input, &ones,
&zeros);

printf("Number of 1's: %d\n", ones);


printf("Number of 0's: %d\n", zeros);
return 0;
}
Exp7.Design a Program to find 2’s complement of a given binary number.

#include <stdio.h>
#include <string.h>
// Function to find the 2's complement of a binary number
void twos_complement(char *binary) {
int length = strlen(binary);
int i;

// Step 1: Flip all bits (1 -> 0, 0 -> 1)


for (i = 0; i < length; i++) {
if (binary[i] == '1')
binary[i] = '0';
else if (binary[i] == '0')
binary[i] = '1';
else {
printf("Invalid input! Only binary digits (0 and 1) are allowed.\n");
return;
}
}

// Step 2: Add 1 to the flipped binary number


int carry = 1;
for (i = length - 1; i >= 0; i--) {
if (binary[i] == '1' && carry == 1) {
binary[i] = '0'; // 1 + 1 = 10 (carry remains 1)
} else if (binary[i] == '0' && carry == 1) {
binary[i] = '1'; // 0 + 1 = 1 (carry becomes 0)
carry = 0;
}
}

// If there's still a carry (all 1s case), print extra '1' at the beginning
if (carry == 1) {
printf("2's Complement: 1%s\n", binary);
} else {
printf("2's Complement: %s\n", binary);
}
}
int main() {
char binary[100];

printf("Enter a binary number: ");


scanf("%s", binary);

twos_complement(binary);

return 0;
}
Exp8. Design a program which will increment the given binary number by 1

#include <stdio.h>
#include <string.h>

// Function to increment a binary


number by 1
void increment_binary(char *binary)
{
int length = strlen(binary);
int carry = 1; // Start with carry =
1 (to add 1)

// Traverse from rightmost bit to


left
for (int i = length - 1; i >= 0; i--) {
if (binary[i] == '1' && carry == 1)
{
binary[i] = '0'; // 1 + 1 = 10
(carry remains 1)
} else if (binary[i] == '0' &&
carry == 1) {
binary[i] = '1'; // 0 + 1 = 1
(carry becomes 0)
carry = 0;
break;
}
}

// If carry is still 1, we need to add


an extra '1' at the beginning
if (carry == 1) {
printf("Incremented Binary:
1%s\n", binary);
} else {
printf("Incremented Binary: %s\
n", binary);
}
}

int main() {
char binary[100];

printf("Enter a binary number: ");


scanf("%s", binary);

increment_binary(binary);

return 0;
}
Exp9. Design a program to convert NFA to DFA

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STATES 10
#define MAX_SYMBOLS 2 //
Assuming binary alphabet {0,1}
#define MAX_DFA_STATES 100

// NFA transition table


int nfa[MAX_STATES]
[MAX_SYMBOLS][MAX_STATES];
int nfa_states, dfa_states;
int final_nfa[MAX_STATES],
final_dfa[MAX_DFA_STATES];
int dfa[MAX_DFA_STATES]
[MAX_SYMBOLS];

// Stores the DFA states as sets of


NFA states
int
dfa_state_table[MAX_DFA_STATES]
[MAX_STATES];
int dfa_state_count = 0;

// Function to check if a state is


already present in DFA states
int find_state(int state_set[], int
state_count) {
for (int i = 0; i < dfa_state_count;
i++) {
int match = 1;
for (int j = 0; j < nfa_states; j++)
{
if (dfa_state_table[i][j] !=
state_set[j]) {
match = 0;
break;
}
}
if (match) return i;
}
return -1;
}

// Function to convert NFA to DFA


void convert_nfa_to_dfa() {
int queue[MAX_DFA_STATES]
[MAX_STATES]; // Queue for
processing states
int front = 0, rear = 0;

// Start with the NFA's initial state


{q0}
int start_state[MAX_STATES] = {0};
start_state[0] = 1; // q0 is in the
start state
memcpy(queue[rear++],
start_state, sizeof(start_state));

while (front < rear) {


int current_state[MAX_STATES];
memcpy(current_state,
queue[front++],
sizeof(current_state));

int existing_state =
find_state(current_state,
nfa_states);
if (existing_state == -1) {

memcpy(dfa_state_table[dfa_state_
count], current_state,
sizeof(current_state));
existing_state =
dfa_state_count++;
}

for (int symbol = 0; symbol <


MAX_SYMBOLS; symbol++) {
int new_state[MAX_STATES] =
{0};

for (int i = 0; i < nfa_states; i+


+) {
if (current_state[i]) {
for (int j = 0; j <
nfa_states; j++) {
if (nfa[i][symbol][j]) {
new_state[j] = 1;
}
}
}
}

if (find_state(new_state,
nfa_states) == -1) {
memcpy(queue[rear++],
new_state, sizeof(new_state));
}

dfa[existing_state][symbol] =
find_state(new_state, nfa_states);
}
}
}

// Function to print DFA transition


table
void print_dfa() {
printf("\nDFA Transition Table:\
n");
printf("State\t0\t1\n");
for (int i = 0; i < dfa_state_count;
i++) {
printf("q%d\tq%d\tq%d\n", i,
dfa[i][0], dfa[i][1]);
}
}
int main() {
printf("Enter the number of NFA
states: ");
scanf("%d", &nfa_states);

printf("Enter transition table (for


each state, input 0 and 1
destinations, -1 if none):\n");
for (int i = 0; i < nfa_states; i++) {
for (int j = 0; j < MAX_SYMBOLS;
j++) {
printf("q%d -- %d --> ", i, j);
for (int k = 0; k < nfa_states;
k++) {
scanf("%d", &nfa[i][j][k]);
}
}
}

convert_nfa_to_dfa();
print_dfa();

return 0;
}
Experiment 10. Design a Program to create PDA machine that accept the well-formed parenthesis.

#include <iostream>
#include <stack>
#include <string>
using namespace std;

class PDAMachine {
private:
stack<char> pdaStack;

public:
PDAMachine() {
pdaStack.push('$'); // Initialize
the stack with the bottom marker $
}

bool processInput(string inputStr)


{
for (char symbol : inputStr) {
if (symbol == '(') {
pdaStack.push('('); // Push
an open parenthesis onto the stack
} else if (symbol == ')' &&
pdaStack.top() == '(') {
pdaStack.pop(); // Pop the
open parenthesis from the stack for
a matching close parenthesis
} else {
return false; // Invalid input
}
}

// Check if the stack is empty


except for the bottom marker $
return pdaStack.size() == 1 &&
pdaStack.top() == '$';
}
};

int main() {
PDAMachine pda;
string inputString;

cout << "Enter a string containing


parentheses: ";
cin >> inputString;

bool result =
pda.processInput(inputString);

if (result) {
cout << "Input string is well-
formed." << endl;
} else {
cout << "Input string is not well-
formed." << endl;
}
return 0;
}

You might also like