NCDlabfile (Final)
NCDlabfile (Final)
Code:
#include <iostream>
#include <string>
int main() {
std::string state = "";
std::string finalState = "Q0";
std::string input;
std::cout << "Enter the number: ";
std::cin >> input;
for (char i : input) {
if (i == '0' || i == '5') {
state = "Q0";
} else if (i == '1' || i == '6') {
state = "Q1";
} else if (i == '2' || i == '7') {
state = "Q2";
} else if (i == '3' || i == '8') {
state = "Q3";
} else if (i == '4' || i == '9') {
state = "Q4";
} else {
std::cout << "Unidentified symbol" << std::endl;
break;
}
std::cout << state << std::endl;
}
if (flag) {
std::cout << "\nState is " << state << std::endl;
if (state == finalState) {
std::cout << "Accepted" << std::endl
}
}
return 0;
}
Output:
Write an program of mode5 Machine(binary).
Code:
#include <iostream>
#include <string>
int main() {
std::string state = "Q0";
std::string finalState = "Q0";
std::string input;
std::cout << "Enter the number: ";
std::cin >> input;
for (char i : input) {
switch (state[1]) {
case '0':
if (i == '0') state = "Q0";
else if (i == '1') state = "Q1";
else {
std::cout << "Unidentified Symbol\n";
return 1;
}
break;
case '1':
if (i == '0') state = "Q2";
else if (i == '1') state = "Q3";
else {
std::cout << "Unidentified Symbol\n";
return 1;
}
break;
case '2':
if (i == '0') state = "Q4";
else if (i == '1') state = "Q0";
else {
std::cout << "Unidentified Symbol\n";
return 1;
}
break;
case '3':
if (i == '0') state = "Q1";
else if (i == '1') state = "Q2";
else {
std::cout << "Unidentified Symbol\n";
return 1;
}
break;
case '4':
if (i == '0') state = "Q3";
else if (i == '1') state = "Q4";
else {
std::cout << "Unidentified Symbol\n";
return 1;
}
break;
}
}
std::cout << "State is " << state << std::endl;
if (state == finalState)
std::cout << "Accepted" << std::endl;
return 0;
}
Output:
Write an program of mode3 Machine(decimal).
Code:
#include<iostream>
#include<string>
using namespace std;
int main() {
string state = "Q0";
string finalState = "Q0";
string input;
bool flag = true;
cout << "Enter the number: ";
cin >> input;
for (char i : input) {
if (!flag)
break;
if (state=="Q0") {
if (i=='0'|| i=='3'||i =='6'||i =='9') {
state = "Q0";
} else if (i=='1'|| i=='4'|| i=='7') {
state = "Q1";
} else if (i=='2'||i=='5'||i=='8') {
state = "Q2";
} else {
cout << "Unidentified Symbol" << endl;
flag = false;
}
} else if (state=="Q1") {
if (i =='0'|| i=='3'|| i=='6' || i == '9') {
state = "Q1";
} else if (i == '1'||i == '4'|| i=='7') {
state = "Q2";
} else if (i == '2'||i == '5'|| i == '8') {
state = "Q0";
} else {
cout << "Unidentified Symbol" << endl;
flag = false;
}
} else if (state=="Q2") {
if (i == '0'|| i == '3'|| i == '6'||i == '9') {
state = "Q2";
} else if (i == '1'|| i == '4'|| i == '7') {
state = "Q0";
} else if (i == '2'|| i =='5'|| i== '8') {
state = "Q1";
} else {
cout << "Unidentified Symbol" << endl;
flag = false;
}
}
}
if(flag){
cout << "State is "<< state << endl;
}
if(state==finalState) {
cout<<"Accepted"<<endl;
} else {
cout<<"Rejected"<<endl;
}
return 0;
}
Output:
Write an program of mode3 Machine(binary).
Code:
#include<iostream>
#include <string>
using namespace std;
int main() {
int state = 0;
bool flag = true;
string input;
cout << "Enter the number: ";
cin >> input;
for(char i: input) {
switch(state) {
case 0: // for Q0
if(i == '0') {
state = 0;
} else if(i == '1') {
state = 1;
} else {
cout << "Unidentified Symbol" << endl;
flag = false;
}
break;
case 1: // for Q1
if(i == '0') {
state = 2;
} else if(i == '1') {
state = 0;
} else {
cout << "Unidentified Symbol" << endl;
flag = false;
}
break;
case 2: // for Q2
if(i == '0') {
state = 1;
} else if(i == '1') {
state = 2;
} else {
cout << "Unidentified Symbol" << endl;
flag = false;
}
break;
}
if(!flag) break; // Exit the loop if an unidentified symbol is found
}
if(flag) {
cout << "State is Q" << state << endl;
if(state == 0) {
cout << "Accepted" << endl;
} else {
cout << "Not Accepted" << endl;
}
}
return 0;
}
Output:
Write an program of Machine start(010) and end with (0+1)*.
Code:
#include <iostream>
#include <string>
using namespace std;
class DFA {
public:
DFA() {
states[0] = "q0";
states[1] = "q1";
states[2] = "q2";
states[3] = "q3";
initial_state = "q0";
accepting_states[0] = "q3";
}
string transition(const string& state, char symbol) {
if (state == "q0" && symbol == '0') return "q1";
if (state == "q1" && symbol == '1') return "q2";
if (state == "q2" && symbol == '0') return "q3";
if (state == "q3" && (symbol == '0' || symbol == '1')) return "q3";
return "";
}
bool accept(const string& str) {
string state = initial_state;
for (char symbol : str) {
state = transition(state, symbol);
}
for (const auto& acc_state : accepting_states) {
if (state == acc_state) return true;
}
return false;
}
private:
string states[5];
string initial_state;
string accepting_states[1];
};
int main() {
string user_input;
cout << "Enter a string (0s and 1s): ";
cin >> user_input;
DFA dfa;
if (dfa.accept(user_input)) {
cout << "Accepted" << endl;
} else {
cout << "Not accepted" << endl;
}
return 0;}
Output:
Write an program of Machine start(101) and end with (0+1)*.
Code:
#include <iostream>
#include <string>
using namespace std;
class DFA {
public:
DFA() {
states[0] = "q0";
states[1] = "q1";
states[2] = "q2";
states[3] = "q3";
initial_state = "q0";
accepting_states[0] = "q3";
}
string transition(const string& state, char symbol) {
if (state == "q0" && symbol == '1') return "q1";
if (state == "q1" && symbol == '0') return "q2";
if (state == "q2" && symbol == '1') return "q3";
if (state == "q3" && (symbol == '0' || symbol == '1')) return "q3";
return "";
}
bool accept(const string& str) {
string state = initial_state;
for (char symbol : str) {
state = transition(state, symbol);
}
for (const auto& acc_state : accepting_states) {
if (state == acc_state) return true;
}
return false;
}
private:
string states[5];
string initial_state;
string accepting_states[1];
};
int main() {
string user_input;
cout << "Enter a string (0s and 1s): ";
cin >> user_input;
DFA dfa;
if (dfa.accept(user_input)) {
cout << "Accepted" << endl;
} else {
cout << "Not accepted" << endl;
}
return 0;
}
Output:
Write an program of Machine start (0+1)* and end with (010).
Code:
#include <iostream>
#include <string>
using namespace std;
class DFA {
public:
bool accept(const string &input) {
string state = "q0"; // Start from the initial state
// Iterate through the input string and transition between states
for (char symbol : input) {
if (state == "q0") {
if (symbol == '0') {
state = "q1";
} else {
state = "q0";
}
} else if (state == "q1") {
if (symbol == '0') {
state = "q1";
} else {
state = "q2";
}
} else if (state == "q2") {
if (symbol == '0') {
state = "q3";
} else {
state = "q0";
}
} else if (state == "q3") {
if (symbol == '0') {
state = "q1";
} else {
state = "q0";
}
}
}
// Return true if the final state is an accepting state, else false
return (state == "q3");
}
};
int main() {
string user_input;
cout << "Enter a string (0s and 1s): ";
cin >> user_input;
DFA dfa;
if (dfa.accept(user_input)) {
cout << "Accepted" << endl;
} else {
cout << "Rejected" << endl;
}
return 0;
}
Output:
Program that checks if a given string starts with any combination of 0 and
1 and ends with 101.
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class DFA {
public:
DFA() {
states = {"q0", "q1", "q2", "q3"};
initial_state = "q0";
accepting_states = {"q3"};
}
string transition(const string& state, char symbol) {
if (state == "q0" && symbol == '0') return "q0";
if (state == "q0" && symbol == '1') return "q1";
if (state == "q1" && symbol == '0') return "q2";
if (state == "q1" && symbol == '1') return "q1";
if (state == "q2" && symbol == '0') return "q0";
if (state == "q2" && symbol == '1') return "q3";
if (state == "q3" && symbol == '0') return "q0";
if (state == "q3" && symbol == '1') return "q1";
return "";
}
bool accept(const string& str) {
string state = initial_state;
for (char symbol : str) {
state = transition(state, symbol);
}
for (const auto& acc_state : accepting_states) {
if (state == acc_state) return true;
}
return false;
}
private:
vector<string> states;
string initial_state;
vector<string> accepting_states;
};
int main() {
string user_input;
cout << "Enter a string (0s and 1s): ";
cin >> user_input;
DFA dfa;
if (dfa.accept(user_input)) {
cout << "Accepted" << endl;
} else {
cout << "Not accepted" << endl;
}
return 0;
}
Output:
Write a program that generates strings starting with any combination of 0
and 1 (including an empty string), followed by the specific sequence 010,
and ending with any combination of 0 and 1 (including an empty string).
Code:
#include <iostream>
#include <string>
using namespace std;
class DFA {
public:
DFA(){
states[0] = "q0";
states[1] = "q1";
states[2] = "q2";
states[3] = "q3";
states[4] = "q4";
initial_state = "q0";
accepting_states[0] = "q4";
}
string transition(const string& state, char symbol) {
if (state == "q0" && (symbol == '0' || symbol == '1')) return "q1";
if (state == "q1") {
if (symbol == '0') {
return "q2";
} else {
return "q1";
}
}
if (state == "q2" && symbol == '1') return "q3";
if (state == "q3" && symbol == '0') return "q4";
if (state == "q4" && (symbol == '0' || symbol == '1')) return "q4";
return "";
}
bool accept(const string& str) {
string state = initial_state;
for (char symbol : str) {
state = transition(state, symbol);
}
for (const auto& acc_state : accepting_states) {
if (state == acc_state) return true;
}
return false;
}
private:
string states[5];
string initial_state;
string accepting_states[1];
};
int main() {
string user_input;
cout << "Enter a string (0s and 1s): ";
cin >> user_input;
DFA dfa;
if (dfa.accept(user_input)) {
cout << "Accepted" << endl;
} else {
cout << "Not accepted" << endl;
}
return 0;
}
Output:
Write an program to identify identifiers.
Code:
#include <iostream>
#include <string>
#include <cctype> // For isalpha, isalnum
using namespace std;
// Define an enum for machine states
enum State {
Start, // Initial state
Identifier, // Valid identifier characters
Invalid // Invalid identifier
};
bool isIdentifier(const string& input) {
State state = Start;
for (char ch : input) {
switch (state) {
case Start:
if (isalpha(ch) || ch == '_') {
state = Identifier; // Valid start (letter or underscore)
} else {
state = Invalid; // Invalid start (digit or special character)
}
break;
case Identifier:
if (isalnum(ch) || ch == '_') {
state = Identifier; // Valid identifier characters (letters, digits, underscores)
} else {
state = Invalid; // Invalid character encountered
}
break;
case Invalid:
return false; // Once invalid, stay invalid
}
}
return state == Identifier; // Return true if the final state is Identifier
}
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
if (isIdentifier(input)) {
cout << input << " is a valid identifier." << endl;
} else {
cout << input << " is not a valid identifier." << endl;}
return 0;}
Output:
Write an program to identify keywords.
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Define an enum for machine states
enum State {
Start,
Keyword,
Invalid
};
// Function to check if the input is a keyword
bool isKeyword(const string& input) {
// List of some common C++ keywords
vector<string> keywords = {
"int", "float", "double", "char", "return", "if", "else", "for", "while", "switch",
"case", "break", "continue", "void", "default", "do", "enum", "goto", "signed",
"unsigned"
};
State state = Start;
for (const string& keyword : keywords) {
if (input == keyword) {
state = Keyword;
break;
} else {
state = Invalid;
}
}
return state == Keyword;
}
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
if (isKeyword(input)) {
cout << input << " is a keyword." << endl;
} else {
cout << input << " is not a keyword." << endl;
}
return 0;
}
Output:
Write a program to identify constant.
Code:
#include <iostream>
#include <string>
using namespace std;
// Define an enum for machine states
enum State {
Start,
IntegerPart,
DecimalPoint,
FractionPart,
Invalid
};
// Function to identify if the input string is a valid constant
bool isConstant(const string& input) {
State state = Start;
bool isFloatingPoint = false; // To track if the number is a floating-point constant
for (size_t i = 0; i < input.size(); ++i) {
char ch = input[i];
switch (state) {
case Start:
if (ch == '+' || ch == '-') {
state = IntegerPart; // Optional sign
} else if (isdigit(ch)) {
state = IntegerPart; // Valid digit
} else {
state = Invalid; // Invalid character
}
break;
case IntegerPart:
if (isdigit(ch)) {
state = IntegerPart; // Continue reading digits
} else if (ch == '.') {
state = DecimalPoint; // Transition to floating-point constant
isFloatingPoint = true;
} else {
state = Invalid; // Invalid character
}
break;
case DecimalPoint:
if (isdigit(ch)) {
state = FractionPart; // Fractional part after decimal
} else {
state = Invalid; // Invalid character after decimal point
}
break;
case FractionPart:
if (isdigit(ch)) {
state = FractionPart; // Continue reading digits in fractional part
} else {
state = Invalid; // Invalid character in fractional part
}
break;
case Invalid:
return false; // Once invalid, remain invalid
}
}
// A valid constant must end in either IntegerPart or FractionPart (for floating-point
numbers)
return state == IntegerPart || state == FractionPart;
}
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
if (isConstant(input)) {
cout << input << " is a valid constant." << endl;
} else {
cout << input << " is not a valid constant." << endl;
}
return 0;
}
Ouput:
Write an program to identify relation operator.
Code:
#include <iostream>
#include <string>
using namespace std;
// Define an enum for machine states
enum State {
Start, // Initial state
Equal, // After seeing '='
NotEqual, // After seeing '!'
GreaterThan, // After seeing '>'
LessThan, // After seeing '<'
Valid, // Valid relational operator
Invalid // Invalid input
};
// Function to identify if the input string is a valid relational operator
bool isRelationalOperator(const string& input) {
State state = Start;
// Loop through each character in the input string
for (size_t i = 0; i < input.size(); ++i) {
char ch = input[i];
switch (state) {
case Start:
if (ch == '=') {
state = Equal; // Could be '=='==
} else if (ch == '!') {
state = NotEqual; // Could be '!='
} else if (ch == '>') {
state = GreaterThan; // Could be '>' or '>='
} else if (ch == '<') {
state = LessThan; // Could be '<' or '<='
} else {
state = Invalid; // Invalid character
}
break;
case Equal:
case NotEqual:
// Both '==' and '!=' should have an '=' as the next character
state = (ch == '=') ? Valid : Invalid;
break;
case GreaterThan:
case LessThan:
// '>' and '<' are valid by themselves, or can be followed by '=' to be valid
state = (ch == '=') ? Valid : Valid;
break;
case Valid:
case Invalid:
// No further characters should be present after reaching a final state
return state == Valid;
}
}
return state == Valid; // Final check for a valid relational operator
}
int main() {
string input;
cout << "Enter a relational operator: ";
cin >> input;
if (isRelationalOperator(input)) {
cout << input << " is a valid relational operator." << endl;
} else {
cout << input << " is not a valid relational operator." << endl;
}
return 0;
}
Output: