<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>Simple 
Scanner</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is <span lang="en-ca">first edition of my simple scanner, and there is nothing important except it uses the DFA transition</span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>functions. I put all transition functions in an uniform format so that they can be put into an array of function</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>pointers. The return value of each function is the index of state it leads to. So we can call the series of </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>checking functions in a comfortable way. At least it suits my habit.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>To write a simplest scanner to check if a token is a legal identifier of C++. That is, it should </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>starts with a alphabetic character and followed by alphabetic or digital character or '-'. But there </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>should be at most one '-' which is not the end of token.</b></font></span></pre>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  กก</div>
<p><span lang="en-ca"><b>The translation of DFA into programming language can be 
one-to-one and it must be! The scanner</b></span></p>
<p><span lang="en-ca"><b>continually call each scan function after it checks if 
the token ends. And if the return value of </b></span></p>
<p><span lang="en-ca"><b>checking function is -1---means rejected by checking, 
it returns false. If the string ends, scanner </b></span></p>
<p><span lang="en-ca"><b>checks if the state is final state by looking at the 
boolean array of state. Very similar to an DFA?</b></span></p>
<p><span lang="en-ca"><b>It actually is. Since each state transition function 
returns a deterministic value. This is </b></span></p>
<p><span lang="en-ca"><b>FUNCTION DEFINITION BY DISCRETE MATHEMATICS!</b></span></p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;

int scan0(char ch);
int scan1(char ch);
int scan2(char ch);


bool finalStates[3]={false, true, false};

int (*scanArray[3])(char ch)={scan0, scan1, scan2};

bool scanner(char* str);

int main()
{
	char buffer[20];
	cout&lt;&lt;&quot;please input your token\nc:&gt;&quot;;
	while (true)
	{
		cin&gt;&gt;buffer;
		if (strcmp(buffer, &quot;exit&quot;)==0)
		{
			break;
		}
		cout&lt;&lt;buffer&lt;&lt;(scanner(buffer)?&quot; is accepted!&quot;:&quot; is rejected!&quot;)&lt;&lt;endl;
		cout&lt;&lt;&quot;c:&gt;&quot;;
	}
	
	return 0;
}

bool scanner(char* str)
{
	int counter=0, result=0;
	while (str[counter]!=NULL)//scan all char
	{
		if (result==-1)//rejected
		{
			return false;
		}
		result = scanArray[result](str[counter++]);//recursive?loop?		
	}
	return finalStates[result];	
}

	

int scan0(char ch)
{
	if (ch&gt;='a'&amp;&amp;ch&lt;='z'||ch&gt;='A'&amp;&amp;ch&lt;='Z')
	{
		return 1;
	}
	else
	{
		return -1;
	}
}

int scan1(char ch)
{
	if (ch&gt;='a'&amp;&amp;ch&lt;='z'||ch&gt;='A'&amp;&amp;ch&lt;='Z'||ch&gt;='0'&amp;&amp;ch&lt;='9')
	{
		return 1;
	}
	else
	{
		if (ch=='-')
		{
			return 2;
		}
		else
		{
			return -1;
		}
	}
}


int scan2(char ch)
{
	if (ch&gt;='a'&amp;&amp;ch&lt;='z'||ch&gt;='A'&amp;&amp;ch&lt;='Z'||ch&gt;='0'&amp;&amp;ch&lt;='9')
	{
		return 1;
	}
	else
	{
		return -1;
	}
}

</pre>
<pre>
</pre>
<pre><font color="#0000FF"><b>Here is the result:</b></font></pre>

<pre>please input your token
c:&gt;abd
abd is accepted!
c:&gt;a-bkdk0
a-bkdk0 is accepted!
c:&gt;0ofjj
0ofjj is rejected!
c:&gt;afo-b9sf
afo-b9sf is accepted!
c:&gt;1-
1- is rejected!
c:&gt;afgddf-
afgddf- is rejected!
c:&gt;ao9-pppo0po-o
ao9-pppo0po-o is accepted!
c:&gt;exit
Press any key to continue</pre>

<pre></pre>
<pre>	
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>