PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Hamed Afshar   DFA (deterministic finite automaton)   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage of the class
Class: DFA (deterministic finite automaton)
Implement deterministic finite state machines
Author: By
Last change: Usage instruction has been updated.
Date: 13 years ago
Size: 1,864 bytes


Class file image Download
    This example will use define a DFA to accept all strings on {0,1} that includes 001
    Example of accepted strings: 001 , 1001 , 11001 , 10100101 , 1010011101
    Example of not accepted strings: 0100 , 1010 , 010101010100
    The class should be provided with 3 arrays.
    1) States
    2) Alphabet
    3) Transition
        is just a simple array consists of alphabets used for this machine.
        is simple array too, but with the difference that each item point to a value ("Qs"=>"start"). The first is the name of the state, and the second ("start") is the type of this state.
        each 'States array' should consist of at least 1 start and 1 final item.
        This array will tell the automaton where to go.
        Consider this:
        If current start is 'Qs', and the machine sees 0, it will go to Q0 state and if the machine sees 1, it will go to Qs state again.
    How does it work?
        The automaton will start from the start state, and read 1 character from the input and go to another state according to transition array.
        Once it reaches the end of the input, it will stop and check the current state. if current state is final, then it returns true, otherwise, it will return false.




$myDFA=new DFA($states,$alphabet,$transition);
if (
$myDFA->accept("10101010011101")) {
"NOT Accepted!--";