Created
July 30, 2012 10:18
-
-
Save tanayseven/3206021 to your computer and use it in GitHub Desktop.
to implement NFA for language L={aba^n U abab^n | n >= 0 }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//to implement NFA for language L={aba^n U abab^n | n >= 0 } | |
import java.io.*; | |
class LanguageNFA | |
{ | |
static void checkLang(String str) | |
{ | |
char foo; | |
int state = 0; | |
for(int i = 0 ; i < str.length() ; i++) | |
{ | |
foo = str.charAt(i); | |
if(state == 0) | |
{ | |
if(foo == 'a') | |
state = 1; | |
else | |
state = -1; | |
} | |
else if(state == 1) | |
{ | |
if(foo == 'b') | |
state = 2; | |
else | |
state = -1; | |
} | |
else if(state == 2) | |
{ | |
if(foo == 'a' && i != str.length()-1) | |
{ | |
if(str.charAt(i+1) == 'a') | |
state = 3; | |
if(str.charAt(i+1) == 'b') | |
state = 4; | |
} | |
else if(foo == 'a') | |
state = 3; | |
else | |
state = -1; | |
} | |
else if(state == 3) | |
{ | |
if(foo == 'a') | |
state = 3; | |
else | |
state = -1; | |
} | |
else if(state == 4) | |
{ | |
if(foo == 'b') | |
state = 4; | |
else | |
state = -1; | |
} | |
if(state != -1) | |
System.out.println("State: "+state); | |
} | |
if(state == 2 || state == 3 || state == 4) | |
System.out.println("Valid language"); | |
else | |
System.out.println("Invalid language"); | |
} | |
public static void main(String [] args)throws IOException | |
{ | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String inp; | |
System.out.println("Enter a string: "); | |
inp = br.readLine(); | |
checkLang(inp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment