<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: c code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 12:48:09 GMT</pubDate>
    <description>DZone Snippets: c code</description>
    <item>
      <title>Simple finite state machine in C</title>
      <link>http://snippets.dzone.com/posts/show/3793</link>
      <description>I'm vaguely plotting a finite state machine -&gt; C compiler (as toy code, not for a serious project - I know there are plenty already out there), so thought I'd write a sample one by hand to see how it should look. Here's the code for it.&lt;br /&gt;&lt;br /&gt;This checks if stdin contains the phrase 'foo' or 'bar'&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;    int c;&lt;br /&gt;&lt;br /&gt;    START: &lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'f' : goto F;&lt;br /&gt;            case 'b' : goto B;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default: goto START; }&lt;br /&gt;&lt;br /&gt;    F:&lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'o' : goto FO;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default  : goto START;}&lt;br /&gt;    &lt;br /&gt;    FO:&lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'o' : goto SUCCESS;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default  : goto START;}&lt;br /&gt;&lt;br /&gt;    B:&lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'a' : goto BA;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default  : goto START;}&lt;br /&gt;&lt;br /&gt;    BA:&lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'r' : goto SUCCESS;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default  : goto START;}&lt;br /&gt;&lt;br /&gt;    FAIL: &lt;br /&gt;        printf("Does not match.\n");&lt;br /&gt;        return;&lt;br /&gt;    SUCCESS:&lt;br /&gt;        printf("Matches.\n");&lt;br /&gt;        return;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Apr 2007 17:28:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3793</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
  </channel>
</rss>
