0% found this document useful (1 vote)
178 views

Compiler Construction Lab

This document describes a lab assignment to simulate a deterministic finite automaton (DFA) that accepts the regular language defined by the regular expression a(bb)*bc. The tasks include: (1) drawing the DFA, (2) determining the accepted language, and (3) implementing the DFA in C/C++/Java using both goto statements and switch statements. The implementation is tested on the input strings abc, abbc, abcd, abbbc, and abbbbc.

Uploaded by

Sunny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
178 views

Compiler Construction Lab

This document describes a lab assignment to simulate a deterministic finite automaton (DFA) that accepts the regular language defined by the regular expression a(bb)*bc. The tasks include: (1) drawing the DFA, (2) determining the accepted language, and (3) implementing the DFA in C/C++/Java using both goto statements and switch statements. The implementation is tested on the input strings abc, abbc, abcd, abbbc, and abbbbc.

Uploaded by

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

Department of Computing

Lab [01]: DFA Simulation

Name: Haseeb Saeed


Cms : 218698

Date: 8th Oct, 2020


Time: Thursday (09:00 – 12:00)
Instructor: Dr Adnan Rashid
Lab Instructor: M . Danyal Sadiq
Lab [01] : DFA Simulation
Lab Tasks
1. Consider the following Regular Expression:
a(bb)*bc
a. Draw a DFA for the above RE.
b. Determine the language accepted by this automaton
c. Implement this DFA in C/C++/Java.
i. Your implementation should validate the input string for alphabet i.e. ∑ =
{a, b, c}, before using it in the DFA.
ii. Your first implementation of DFA should use goto statements only.

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

void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;

printf("Input the string : ");


fgets(str, sizeof str, stdin);
while(str[l]!='\0')
{ if(str[l]!=’a’ || str[l]!=’b’ || str[l]!= ‘c’){
Goto trap}

printf("%c ", str[l]);


l++;
}

l=0;

if (str[l]=='a'){
l++;
goto one;
}
else{
goto trap;
}

one:
if(str[l]=='b'){
l++;
goto two;
}

else{
l++;
goto trap;
}

two:
if(str[l]=='c'){
l++;
goto final;
}
if(str[l]=='b'){
l++;

goto three;
}

else{
l++;
goto trap;
}

three:
if(str[l]=='b'){
l++;

goto four;
}
else{
l++;
goto trap;
}

four:
if(str[l]=='b'){
l++;

goto three;
}
if(str[l]=='c'){
l++;

goto final;
}
else{
l++;
goto trap;
}

trap:
printf("\n\n\nSTRING FAILED");
return ;
final:
printf("%c",str[l]);
if (str[l]=='a'||str[l]=='b'||str[l]=='c'||str[l]=='d')
{
goto trap;
}

else{
printf("STRING PASSED");
}

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

Swtich
Task 2:
#include <stdio.h>

int main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;

printf("Input the string : ");


fgets(str, sizeof str, stdin);
while(str[l]!='\0')
{ if(str[l]!=’a’ || str[l]!=’b’ || str[l]!= ‘c’){
Printf(“String FAILED”);
Return 0;
}

printf("%c ", str[l]);


l++;
}

l=0;

switch(str[l]){
case 'a':
printf("\n1 passed\n");
l++;
break;
default :
printf("STRING FAILED");
return 0;
}

switch(str[l])
{
case 'b':
printf("2 passed\n");
l++;
break;
default :
printf("STRING FAILED");
return 0;
}
switch(str[l]){
case 'b':
printf("3 passed\n");
l++;
break;
default :
printf("STRING FAILED");
return 0;
}

switch(str[l]){
case 'b':
printf("4 passed\n");
l++;
break;
default :
printf("STRING FAILED");
return 0;
}

int B_counter=0;
while(str[l]=='b'){
B_counter++;

l++;
}

int result = B_counter%2;

switch(result){

case 0:

break;
case 1:
printf("STRING FAILED\n");
return 0;
}

switch(str[l]){
case 'c':
printf("5 passed\n");
l++;
break;
default :
printf("STRING FAILED\n");
return 0;}

switch(str[l]){
case 'a':
printf("STRING FAILED\n");
return 0;
case 'd':
printf("STRING FAILED\n");
return 0;
case 'c':
printf("STRING FAILED\n");
return 0;
default :
printf("STRING PASSED");
return 0;}

return 0;
}
d. Test your implementation using the following inputs:
abc, abbc, abcd, abbbc, abbbbc

Goto:
String Output

abc

abbc

abcd
abbbc

abbbbc

Switch:
String Output

abc
abbc

abcd

abbbc

abbbbc

You might also like