<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DFA &#8211; Luxing Huang</title>
	<atom:link href="https://luxing.im/tag/dfa/feed/" rel="self" type="application/rss+xml" />
	<link>https://luxing.im</link>
	<description>Thoughs and things</description>
	<lastBuildDate>Fri, 11 Oct 2013 20:48:32 +0000</lastBuildDate>
	<language>en-CA</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
<site xmlns="com-wordpress:feed-additions:1">58771605</site>	<item>
		<title>Deterministic Finite Automata in C</title>
		<link>https://luxing.im/deterministic-finite-automata-in-c/</link>
					<comments>https://luxing.im/deterministic-finite-automata-in-c/#respond</comments>
		
		<dc:creator><![CDATA[Luxing Huang]]></dc:creator>
		<pubDate>Thu, 10 Oct 2013 22:07:52 +0000</pubDate>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[DFA]]></category>
		<category><![CDATA[homework]]></category>
		<guid isPermaLink="false">http://blog.luxing.im/?p=49</guid>

					<description><![CDATA[In our a5p2 question, we need to write a general template for the deterministic finite automata, with input details are listed below: &#8211; the number of states (i.e. &#124;&#124;)(the states are numbered from 0 to &#124;&#124; &#8211; 1) &#8211; the number of final states (i.e. &#124;&#124;) &#8211; the number of final symbols in (i.e. &#124;&#124;) &#8230; <p class="link-more"><a href="https://luxing.im/deterministic-finite-automata-in-c/" class="more-link">Continue reading<span class="screen-reader-text"> "Deterministic Finite Automata in C"</span></a></p>]]></description>
										<content:encoded><![CDATA[<p>In our a5p2 <a href="http://socs.acadiau.ca/~jdiamond/comp2103/assignments/a5.pdf">question</a>, we need to write a general template for the deterministic finite automata, with input details are listed below:</p>
<p><span id="more-49"></span></p>
<p>&#8211; the number of states (i.e. |<img decoding="async" src="https://s0.wp.com/latex.php?latex=Q&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="Q" class="latex" />|)(the states are numbered from 0 to |<img decoding="async" src="https://s0.wp.com/latex.php?latex=Q&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="Q" class="latex" />| &#8211; 1)<br />
&#8211; the number of final states (i.e. |<img decoding="async" src="https://s0.wp.com/latex.php?latex=F&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="F" class="latex" />|)<br />
&#8211; the number of final symbols in <img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" /> (i.e. |<img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" />|)<br />
&#8211; |<img decoding="async" src="https://s0.wp.com/latex.php?latex=F&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="F" class="latex" />| numbers, the final states<br />
&#8211; |<img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" />| characters (any white space char is significant, standing exactly for itself, so there will not be multiple spaces in this list)<br />
&#8211; the transition table, given as a <img decoding="async" src="https://s0.wp.com/latex.php?latex=%7CQ%7C%5Ctimes%7C%5CSigma%7C&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="|Q|&#92;times|&#92;Sigma|" class="latex" /> array of numbers.</p>
<p>If you have the basic concept of what DFA is, let&#8217;s then get started. Firstly, we have to understand that DFA is a model or a template for a specific string processing &#8220;machine&#8221;, thus we do not change the code to adjust the needs for a specific programme, we change the matrix (the <img decoding="async" src="https://s0.wp.com/latex.php?latex=%5Cdelta&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;delta" class="latex" />) aka transition table that allows the DFA to determine its validity.</p>
<p>So, firstly, we need to scan in the necessary data listed at the top of this article. Allow me to take Jim&#8217;s example with notation aside.</p>
<h2>Example 1</h2>
<p>Here is a visualized graph on DFA for determining odd number of 1&#8217;s in a binary input. As long as the 1&#8217;s are odd, the input is accepted.</p>
<p>Graphed like this:</p>
<p><a href="http://blog.luxing.im/wp-content/uploads/2013/10/sc3.png"><img decoding="async" class="alignnone size-medium wp-image-60" alt="sc3" src="http://blog.luxing.im/wp-content/uploads/2013/10/sc3-300x137.png" width="300" height="137" srcset="https://luxing.im/wp-content/uploads/2013/10/sc3-300x137.png 300w, https://luxing.im/wp-content/uploads/2013/10/sc3.png 320w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>&nbsp;</p>
<p>The transition table is correspondingly this:</p>
<p><a href="http://blog.luxing.im/wp-content/uploads/2013/10/sc1.png"><img decoding="async" class="alignnone size-full wp-image-67" alt="sc1" src="http://blog.luxing.im/wp-content/uploads/2013/10/sc1.png" width="131" height="90" /></a></p>
<p>2      //the number of states (<img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0%2C+q_1%2C+...%2C+q_%7Bn-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0, q_1, ..., q_{n-1}" class="latex" />)<br />
1      //the number of final states, we have only 1 final state here.<br />
2      //the number of final symbols in <img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" /><br />
1      //the final state is <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" />. If DFA ends here, then the input is valid.<br />
01   //the valid characters in symbols.<br />
0 1<br />
1 0  //those 2 lines are the transition table, meaning <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0+q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0 q_1" class="latex" /> and <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1+q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1 q_0" class="latex" />.</p>
<p>Let&#8217;s give out an input stream sample: <span style="color: #ff00ff;">0</span>1<span style="color: #008000;">00000<span style="color: #800080;">1</span></span><span style="color: #ffcc00;">1</span></p>
<p>The initial state is <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0" class="latex" />. 0 goes in, it looks for coordinate <img decoding="async" src="https://s0.wp.com/latex.php?latex=%280%2C+q_0%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="(0, q_0)" class="latex" />, the result is still <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0" class="latex" />.</p>
<p>Current state is <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0" class="latex" />, then 1 goes in, it looks for <img decoding="async" src="https://s0.wp.com/latex.php?latex=%281%2C+q_0%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="(1, q_0)" class="latex" />. State is changed to <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" />.</p>
<p>Then five <span style="color: #008000;">0<span style="color: #000000;">s</span></span> goes in, the state is locked to <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" /> based on what the transition table says.</p>
<p>Then <span style="color: #993366;">1</span> goes in, state is now <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0" class="latex" />.</p>
<p>Finally the last <span style="color: #ffcc00;">1</span> is passed in, state is changed to <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" />.</p>
<p>EOF</p>
<p>The DFA stopped with a state at <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" />, because only <img decoding="async" src="https://s0.wp.com/latex.php?latex=n+%5Cin+%7CF%7C&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="n &#92;in |F|" class="latex" /> is valid, in this case, <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" /> is valid, so the input is valid.</p>
<h2>Example 2</h2>
<p>The visual graph is like this:</p>
<p><a href="http://blog.luxing.im/wp-content/uploads/2013/10/sc2.png"><img decoding="async" class="alignnone size-medium wp-image-75" alt="sc2" src="http://blog.luxing.im/wp-content/uploads/2013/10/sc2-300x165.png" width="300" height="165" srcset="https://luxing.im/wp-content/uploads/2013/10/sc2-300x165.png 300w, https://luxing.im/wp-content/uploads/2013/10/sc2.png 532w" sizes="(max-width: 300px) 100vw, 300px" /></a> and the transition table is like this:</p>
<p><a href="http://blog.luxing.im/wp-content/uploads/2013/10/sc4.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-76" alt="sc4" src="http://blog.luxing.im/wp-content/uploads/2013/10/sc4.png" width="177" height="207" /></a></p>
<p>The input for building DFA will be like this:</p>
<p>6<br />
3<br />
3<br />
2 3 4<br />
abc<br />
2 1 1<br />
1 1 1<br />
5 3 4<br />
5 3 5<br />
5 5 4<br />
5 5 5</p>
<p>Similarly, there are 6 states, from 0~5. 3 final states, they are <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_2+q_3+q_4&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_2 q_3 q_4" class="latex" />. There are 3 characters accepted, which are <img decoding="async" src="https://s0.wp.com/latex.php?latex=a%2C+b%2C+c+%5Cin+%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="a, b, c &#92;in &#92;Sigma" class="latex" />, and the matrix map.</p>
<p>How to write this programme?</p>
<p>Well firstly we need to read the data in and build a matrix. Then we need to move around the matrix table and get its state, and finally compare the state to the final states.</p>
<p>The code is relatively simple:</p>
<pre>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define N_ASCII 256
#define ACCEPT 200

int
dfa(int nstate, int nfinal, int nsymbol)
{
    int i, j, c, state = 0;
    int final[nfinal];
    int symbol[N_ASCII] = {0}, acceptable_chars[N_ASCII] = {0};
    int matrix[nstate][nsymbol];
    
    /* Buidling up the DFA */

    /* Scanning the final states */
    for (i = 0; i < nfinal; i++)
    {
        scanf("%d", &#038;final[i]);
    }
    scanf("%*[*^\n]\n");

    /*
     * Scan in the chars that are accepted by DFA, allocate its tag from 0 to
     * 1 in symbol[c].
     */
    for (i = 0; i < nsymbol; i++)
    {
        c = getchar();
        symbol[c] = i;
        acceptable_chars[c] = ACCEPT;
    }
    scanf("%*[*^\n]\n");

    /* Scan in the \delta transition table */
    for (i = 0; i < nstate; i++)
    {
        for (j = 0; j < nsymbol; j++)
        {
            scanf("%d", &#038;matrix[i][j]);
        }
    }
    scanf("%*[*^\n]\n");

    /* 
     * Test the input steam
     * We'll stop at either EOF or \n
     */
    bool empty = true;
    while ((c = getchar()) != EOF &#038;&#038; c != '\n')
    {
        if (!(acceptable_chars[c] == ACCEPT))
        {
            fprintf(stderr, "/%c/ char is not in the symbol list!\n" 
                    "Because your number of symbols is %d " 
                    "and there is a %dth.\n", c, nsymbol, nsymbol + 1);
            return -1;
        }

        /* 
         * Inside the matrix we move around based on the char's nth number,
         * until the last getchar().
         */
        state = matrix[state][symbol[c]];
        empty = false;
    }

    if (empty == true)
    {
        fprintf(stderr, "No test input detected!\n");
        return -1;
    }

    for (i = 0; i < nfinal; i++)
    {
        /* 
         * Then we check the final state, if it's a match with any of the 
         * elements inside of |F|
         */
        if(state == final[i])
        {
            return 1;
        }
    }

    return 0;
}

int
main(int argc, char *argv[])
{
    int nstate, nfinal, nsymbol, ret;
    ret = scanf("%d\n", &#038;nstate);
    if (ret != 1)
    {
        fprintf(stderr, "No. of states is invalid!\n");
        return EXIT_FAILURE;
    }
    ret = scanf("%d\n", &#038;nfinal);
    if (ret != 1)
    {
        fprintf(stderr, "No. of finals is invalid!\n");
        return EXIT_FAILURE;
    }
    ret = scanf("%d\n", &#038;nsymbol);
    if (ret != 1)
    {
        fprintf(stderr, "No. of symbols is invalid!\n");
        return EXIT_FAILURE;
    }
    ret = dfa(nstate, nfinal, nsymbol);

    if (ret == 0)
    {
        printf("Not accepted.\n");
    }
    else if (ret == -1)
    {
        fprintf(stderr, "Invalid input.\n");
        return EXIT_FAILURE;
    }
    else
        printf("Accepted.\n");

    return EXIT_SUCCESS;
}
</pre>
<p>This has to be compiled using -std=c99 flag in gcc. </p>
<p>When I wrote this program, I did not consider '\n' as a valid element of set <img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" />.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://luxing.im/deterministic-finite-automata-in-c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">49</post-id>	</item>
	</channel>
</rss>
