THL Practical 02 v01

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

        

  

     
        
    
    

   
    
  

                  


                  
                 
           
                   
   
        initializeDFA1  initializeDFA2  
        
                  
          fi  
                    
                   
     

// Step 1: Create Two I nitiali zation Functions for The DFAs to Be


Combined

// Function to initialize the first DFA


void initial izeDFA1 ( struct DFA * dfa ) {
// Parameters for DFA1
 }

 // Function to initialize the second DFA
 void initial izeDFA2 ( struct DFA * dfa ) {
 // Parameters for DFA2
}

int main () {
// Declare the DFAs
struct DFA dfa1 , dfa2 , combinedDFA ;

 // Initialize the first DFA
 initi alizeDF A1 (& dfa1 ) ;

 // Initialize the second DFA
initi alizeDF A2 (& dfa2 ) ;

// ...
return 0;
 }

          

                


      combineDFAs          
                   
      
                   
          ff 

// Step 2: Create The DFA Combination Function

// Function to initialize the first DFA


void initial izeDFA1 ( struct DFA * dfa ) {
// Parameters for DFA1
 }

 // Function to initialize the second DFA
 void initial izeDFA2 ( struct DFA * dfa ) {
 // Parameters for DFA2
}

// Function to combine two DFAs into one DFA


void combineDFAs ( struct DFA * dfa1 , struct DFA * dfa2 , struct DFA *
resultDFA , char * operation ) {
// Initialize the combined DFA
 // Determine the number of states based on the number of states
of DFA1 and DFA2
 // Copy the alphabet from any of the other DFAs
 // Compute transitions for the combined DFA
 // Set the initial state of the combined DFA
 // Set the final states of the combined DFA
// If operation == " and " , take the intersection
// If operation == " or " , take the union
// If operation == " minus " , take the difference
}

 int main () {
 // Declare the DFAs
 struct DFA dfa1 , dfa2 , combinedDFA ;

 // Initialize the first DFA
initi alizeDF A1 (& dfa1 ) ;

// Initialize the second DFA


initi alizeDF A2 (& dfa2 ) ;

 // Combine the two DFAs


 combineDFAs (& dfa1 , & dfa2 , & combinedDFA , " and " ) ;

 // ...

return 0;
}

       


                 
 
              𝑝𝑖 , 𝑞𝑗     
        
    𝑖  𝑗  𝑝𝑖 , 𝑞𝑗             
               
        

// Step 2: Create The DFA Combination Function

// Function to initialize the first DFA


void initial izeDFA1 ( struct DFA * dfa ) {
// Parameters for DFA1
 }

 // Function to initialize the second DFA
 void initial izeDFA2 ( struct DFA * dfa ) {
 // Parameters for DFA2
}

// Function to combine two DFAs into one DFA


void combineDFAs ( struct DFA * dfa1 , struct DFA * dfa2 , struct DFA *
resultDFA , char * operation ) {
// Initialize the combined DFA
 // Determine the number of states based on the number of states
of DFA1 and DFA2
 // Copy the alphabet from any of the other DFAs
 // Compute transitions for the combined DFA
 // Set the initial state of the combined DFA
 // Set the final states of the combined DFA
// If operation == " and " , take the intersection
// If operation == " or " , take the union
// If operation == " minus " , take the difference
}

 // Function to simulate DFA on input string


 void s i m u l a t e C o m b i n e d D F A (...) {
 // ...
 // Handle the state naming . States must have the form ( p_i , q_j )
.
 // ...
}

int main () {
// Declare the DFAs
struct DFA dfa1 , dfa2 , combinedDFA ;

 // Initialize the first DFA
 initi alizeDF A1 (& dfa1 ) ;

 // Initialize the second DFA
initi alizeDF A2 (& dfa2 ) ;

// Combine the two DFAs


combineDFAs (& dfa1 , & dfa2 , & combinedDFA , " and " ) ;

 // Input string to be processed


 char inputString [100];

 // Continuous input until " exit " is entered or Ctrl - D is
encountered
 while (1) {
// ...

// Simulate the DFA on the input string


s i m u l a t e C o m b i n e d D F A (...) ;
}

 return 0;
 }

       

$ gcc practical_02 . c -o practical_02


$ ./ practical_02
Enter the input string ( binary ) , or type ’ exit ’ to end : abbabba
Initial State : p0q0
Input a -> p1q0
 Input b -> p1q1
 Input b -> p1q0
 Input a -> p2q0
 Input b -> p2q1
 Input b -> p2q0
Input a -> p0q0
String Rejected
Enter the input string ( binary ) , or type ’ exit ’ to end :

      

You might also like