0% found this document useful (0 votes)
31 views6 pages

Name: Fazal Hadi: Compiler First 1 Assignment

This C++ program implements a deterministic finite automaton (DFA) to accept strings over the alphabet {a, b} where the number of a's plus the number of b's is even. It defines six states and functions for each state's transitions. The isAccepted function uses the state functions to simulate the DFA on an input string and returns 1 if accepted or 0 if not. The main function tests the string "aaabbb", which is accepted as the number of a's and b's sums to an even number.

Uploaded by

fazal hadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Name: Fazal Hadi: Compiler First 1 Assignment

This C++ program implements a deterministic finite automaton (DFA) to accept strings over the alphabet {a, b} where the number of a's plus the number of b's is even. It defines six states and functions for each state's transitions. The isAccepted function uses the state functions to simulate the DFA on an input string and returns 1 if accepted or 0 if not. The main function tests the string "aaabbb", which is accepted as the number of a's and b's sums to an even number.

Uploaded by

fazal hadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Compiler first 1 Assignment

Name: Fazal Hadi


Roll No: 171116
------------------------------------------------------------------

// C++ program to implement DFA that accepts

// all strings which follow the language

// L = { a^n b^m ; n+m=even }

#include <bits/stdc++.h>

using namespace std;

// dfa tells the number associated

// with the present state

int dfa = 0;

// This function is for

// the starting state (zeroth) of DFA

void start(char c)

if (c == 'a')

dfa = 1;

else if (c == 'b')

dfa = 2;
// -1 is used to check for any invalid symbol

else

dfa = -1;

// This function is for the first state of DFA

void state1(char c)

if (c == 'a')

dfa = 0;

else if (c == 'b')

dfa = 5;

else

dfa = -1;

// This function is for the second state of DFA

void state2(char c)

if (c == 'b')

dfa = 3;

else

dfa = -1;

// This function is for the third state of DFA

void state3(char c)

{
if (c == 'b')

dfa = 4;

else

dfa = -1;

// This function is for the fourth state of DFA

void state4(char c)

if (c == 'b')

dfa = 3;

else

dfa = -1;

// This function is for the fifth state of DFA

void state5(char c)

if (c == 'b')

dfa = 6;

else

dfa = -1;

// This function is for the sixth state of DFA

void state6(char c)

if (c == 'b')

dfa = 5;
else

dfa = -1;

int isAccepted(char str[])

// Store length of string

int i, len = strlen(str);

for (i = 0; i < len; i++) {

if (dfa == 0)

start(str[i]);

else if (dfa == 1)

state1(str[i]);

else if (dfa == 2)

state2(str[i]);

else if (dfa == 3)

state3(str[i]);

else if (dfa == 4)

state4(str[i]);

else if (dfa == 5)

state5(str[i]);

else if (dfa == 6)
state6(str[i]);

else

return 0;

if (dfa == 3 || dfa == 5)

return 1;

else

return 0;

// Driver code

int main()

char str[] = "aaabbb";

if (isAccepted(str))

cout << "\nACCEPTED\n";

else

cout << "NOT ACCEPTED\n";

return 0;

// This code is contributed by SHUBHAMSINGH10


Even Length Accepted |||||| Odd Length Not Accepted

You might also like