TOC Lab Manual
TOC Lab Manual
Subject: TOC
Semester: VI
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
int main() {
char binary_input[100];
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 main() {
char binary_input[100];
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>
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>
int main() {
char decimal_input[100];
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>
int main()
{
char binary_input[100];
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>
int main() {
char binary_input[100];
int ones = 0, zeros = 0;
count_ones_zeros(binary_input, &ones,
&zeros);
#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;
// 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];
twos_complement(binary);
return 0;
}
Exp8. Design a program which will increment the given binary number by 1
#include <stdio.h>
#include <string.h>
int main() {
char binary[100];
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
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++;
}
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);
}
}
}
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 $
}
int main() {
PDAMachine pda;
string 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;
}