Home      Affiliated Colleges      Course content      First Sem     Second Sem     Third Sem     Fourth Sem     Fifth Sem     Sixth Sem     Seventh Sem     Eighth Sem     Lab report 4th sem     Contact    

Monday, April 25, 2011

Compiler Design and Construction: Write the java code to implement DFA accepting odd numbers of zeros :

Compiler Design and Construction
Assignment no.:03
=====================================================================

Q. Write the java code to implement DFA accepting odd numbers of zeros :


Solution:
package dfa;

import java.util.Scanner;

/**
 *
 * @author VK Pandey
 */
public class dfa {

    /**
     * @param args the command line arguments
     */
    static int i,s,q0=0,q1=1;
    static String str;
    public static int move(int s, char c)
    {

            if(s==q0 && c=='1')
            {
                System.out.println("State q0");
                return q0;

            }
            else if(s==q0 && c=='0')
            {
                System.out.println("State q1");
                return q1;

            }
            else if(s==q1 && c=='0')
            {
                System.out.println("State q0");
                return q0;

            }
            else
            {
                System.out.println("State q1");
                return q1;

            }


    }

    public static void main(String[] args) {
        // TODO code application logic here
        int len, s;
        System.out.println("Enter the string:");
        Scanner input = new Scanner(System.in);
        str = input.next();
        len = str.length();
        s = q0;
        char c;
        System.out.println("State q0");

        for(int i=0;i<len;i++)
        {
            c = str.charAt(i);
            s = move(s,c);


        }
        if(s == q1)
            System.out.println("Final state, string accepted");
        else
            System.out.println("string not accepted");


    }
   

}
Output:



No comments:

Post a Comment

^ Scroll to Top Related Posts with Thumbnails ^ Go to Top