Compiler Construction Lab
Compiler Construction Lab
GOTO
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;
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;
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++;
}
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