You are on page 1of 12

COMPILER CONSTRUCTION

LAB REPORT # 03

Submitted To:

Sir Shahzad Arif

Submitted By:

Malik M.Shahzaib

Reg No:

17-CS-036
Lab #03

DFA IMPLEMENTATION

Objectives:

 To learn about the basics Deterministic Finite Automata.


 To implement DFA for some languages.
 To implement a language in C++.

Software tools:
 Codeblocks

Theory:
A deterministic finite automaton (DFA)—also known as deterministic finite state machine—is a
finite state machine that accepts/rejects finite strings of symbols and only produces a unique
computation (or run) of the automaton for each input string. 'Deterministic' refers to the uniqueness
of the computation.

In this lab we learned about the Deterministic Finite Automata. We implemented different DFA’s
for languages.

Procedure:

Task 01:

Consider the following language:

L(M) = {w | w € {a, b}* and contains even number of a’s and b’s}

a. Draw a DFA for the above language

i. Your implementation should validate the input string for alphabet i.e. Σ = {a, b}, before using it
in the DFA.

ii. Your first implementation of DFA should use goto statements only.

iii. Your second implementation of DFA should use switch statement instead of the goto’s.

b. Test your implementation using the following inputs: aa, ab, aba, abab, aabbaabb
DFA:

a
S1 S2

b b b b

a S4
S3
a

Implementation #01:

Input:

#include <iostream>

#include <string.h>

using namespace std;

int main()

string language;

cout<<"Enter The String: ";

cin>>language;

int sizes=language.length();

char arr[sizes+1];
strcpy(arr,language.c_str());

int trued=0;

for(int i=0;i<sizes;i++)

switch(arr[i])

case 'a':

continue;

break;

case 'b':

continue;

break;

case 'A':

continue;

break;

case 'B':

continue;

break;

default:

trued=1;

break;
}

if(trued==1)

cout<<"False String";

else

int place=0;

cout<<"True String Now Applying.."<<endl;

S1:

if(place>=sizes)

cout<<"Correct Language";

return 0;

if(arr[place]=='a')

place++;

goto S2;
}

else

place++;

goto S3;

S2:

if(place>=sizes)

cout<<"INcorrect Language";

return 0;

if(arr[place]=='a')

place++;

goto S1;

else

place++;

goto S4;

S3:
if(place>=sizes)

cout<<"INcorrect Language";

return 0;

if(arr[place]=='a')

place++;

goto S4;

else

place++;

goto S1;

S4:

if(place>=sizes)

cout<<"INcorrect Language";

return 0;

if(arr[place]=='a')

{
place++;

goto S3;

else

place++;

goto S2;

}}}

Output:

Implementation #02:

Input:

#include <iostream>

#include <string.h>

using namespace std;


int main()

string language;

cout<<"Enter The String: ";

cin>>language;

int sizes=language.length();

char arr[sizes+1];

strcpy(arr,language.c_str());

int trued=0;

for(int i=0;i<sizes;i++)

switch(arr[i])

case 'a':

continue;

break;

case 'b':

continue;

break;

case 'A':
continue;

break;

case 'B':

continue;

break;

default:

trued=1;

break;

if(trued==1)

cout<<"False String";

else

int place=0;

int A_count=0;

int B_count=0;

cout<<"True String Now Applying.."<<endl;

while(sizes>place)
{

switch(arr[place])

case 'a':

A_count++;

place++;

break;

case 'b':

B_count++;

place++;

break;

int A_check=A_count%2;

int B_check=B_count%2;

if(A_check==0 && B_check==0)

cout<<"Correct Language";

else
{

cout<<"Incorrect Language";

}}}

Output:

Conclusion:
In this lab we learned to implement DFA we also performed several tasks related to Deterministic
Finite Automata.

=======================================================

You might also like