SE250:lab-9:rwan064:scanner.c
Jump to navigation
Jump to search
<html> <head>
<title>scanner.c</title>
</head> <body bgcolor="#ffffff" text="#000000">
<a name="line1"> 1</a> <font color="#444444">/* <a name="line2"> 2</a> File: scanner.c <a name="line3"> 3</a> Purpose: Code that takes an input stream of characters and seperates it into operators, identifiers and seperators. <a name="line4"> 4</a> Author: Rajitha Wannigama <a name="line5"> 5</a> */</font> <a name="line6"> 6</a> <a name="line7"> 7</a> <font color="0000ff"><strong>#include <font color="#008000"><string.h></font></strong></font> <a name="line8"> 8</a> <a name="line9"> 9</a> <font color="0000ff"><strong>#include <font color="#008000">"string.h"</font></strong></font> <a name="line10"> 10</a> <font color="0000ff"><strong>#include <font color="#008000">"tokenise.h"</font></strong></font> <a name="line11"> 11</a> <font color="0000ff"><strong>#include <font color="#008000">"scanner.h"</font></strong></font> <a name="line12"> 12</a> <a name="line13"> 13</a> <strong>const</strong> <strong>int</strong> <font color="#2040a0">CPFILE_OP</font> <font color="4444FF">=</font> <font color="#FF0000">8</font><font color="4444FF">;</font> // <font color="#2040a0">Operator</font> <font color="#2040a0">was</font> <font color="#2040a0">read</font> <font color="#2040a0">last</font> <a name="line14"> 14</a> <strong>const</strong> <strong>int</strong> <font color="#2040a0">CPFILE_EOF</font> <font color="4444FF">=</font> <font color="#FF0000">9</font><font color="4444FF">;</font> // <font color="#2040a0">EOF</font> <font color="#2040a0">was</font> <font color="#2040a0">read</font> <font color="#2040a0">last</font> <a name="line15"> 15</a> <strong>const</strong> <strong>int</strong> <font color="#2040a0">CPFILE_NL</font> <font color="4444FF">=</font> <font color="#FF0000">10</font><font color="4444FF">;</font> // <font color="#2040a0">NEW</font><font color="4444FF">-</font><font color="#2040a0">LINE</font> <font color="#2040a0">character</font> <font color="#2040a0">was</font> <font color="#2040a0">read</font> <font color="#2040a0">last</font> <a name="line16"> 16</a> <a name="line17"> 17</a> // <font color="#2040a0">Also</font> <font color="#2040a0">need</font> <font color="#2040a0">to</font> <font color="#2040a0">know</font> <strong>if</strong> <font color="#2040a0">previous</font> <font color="#2040a0">character</font> <font color="#2040a0">read</font> <font color="#2040a0">was</font> <font color="#2040a0">a</font> <font color="#2040a0">SPACE</font> <font color="#2040a0">or</font> <font color="#2040a0">TAB</font> <a name="line18"> 18</a> <strong>const</strong> <strong>int</strong> <font color="#2040a0">CPFILE_SP</font> <font color="4444FF">=</font> <font color="#FF0000">11</font><font color="4444FF">;</font> // <font color="#2040a0">SPACE</font> <font color="#2040a0">was</font> <font color="#2040a0">read</font> <font color="#2040a0">last</font> <a name="line19"> 19</a> <strong>const</strong> <strong>int</strong> <font color="#2040a0">CPFILE_TAB</font> <font color="4444FF">=</font> <font color="#FF0000">12</font><font color="4444FF">;</font> // <font color="#2040a0">TAB</font> <font color="#2040a0">was</font> <font color="#2040a0">read</font> <font color="#2040a0">last</font> <a name="line20"> 20</a> <a name="line21"> 21</a> <strong>const</strong> <strong>int</strong> <font color="#2040a0">CPFILE_UN</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> // <font color="#2040a0">Last</font> <font color="#2040a0">character</font> <font color="#2040a0">read</font> <font color="#2040a0">was</font> <font color="#2040a0">unknown</font>. <font color="#2040a0">e</font>.<font color="#2040a0">g</font>. <font color="#2040a0">not</font> <font color="#2040a0">a</font> <font color="#2040a0">valid</font> <font color="#2040a0">program</font> <font color="#2040a0">source</font> <font color="#2040a0">code</font> <font color="#2040a0">file</font> <font color="#2040a0">character</font>. <a name="line22"> 22</a> <a name="line23"> 23</a> // <font color="#2040a0">Returns</font> <font color="#2040a0">true</font> <strong>if</strong> <font color="#2040a0">the</font> <font color="#2040a0">character</font> <font color="#2040a0">passed</font> <font color="#2040a0">as</font> <font color="#2040a0">a</font> <font color="#2040a0">paramter</font> <font color="#2040a0">is</font> <font color="#2040a0">a</font> <font color="#2040a0">C</font> <font color="#2040a0">operator</font>, <font color="#2040a0">false</font> <font color="#2040a0">otherwise</font>. <a name="line24"> 24</a> <font color="#2040a0">bool</font> <font color="#2040a0">is_operator</font><font color="4444FF">(</font> <strong>char</strong> <font color="#2040a0">c</font> <font color="4444FF">)</font> <a name="line25"> 25</a> <font color="4444FF"><strong>{</strong></font> <a name="line26"> 26</a> <strong>if</strong> <font color="4444FF">(</font> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\\</font>'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line27"> 27</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'('</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line28"> 28</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">')'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line29"> 29</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'{'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line30"> 30</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'}'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line31"> 31</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\'</font>'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line32"> 32</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'-'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line33"> 33</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'!'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line34"> 34</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'"'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line35"> 35</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'#'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line36"> 36</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'$'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line37"> 37</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'%'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line38"> 38</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'&'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line39"> 39</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'*'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line40"> 40</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">','</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line41"> 41</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'.'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line42"> 42</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'/'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line43"> 43</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">':'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line44"> 44</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'?'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line45"> 45</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'@'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line46"> 46</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'['</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line47"> 47</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">']'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line48"> 48</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'^'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line49"> 49</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'_'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line50"> 50</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'`'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line51"> 51</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'|'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line52"> 52</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'~'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line53"> 53</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'+'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line54"> 54</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line55"> 55</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'='</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line56"> 56</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'>'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line57"> 57</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">';'</font> <font color="4444FF">)</font> <a name="line58"> 58</a> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line59"> 59</a> <strong>return</strong> <font color="#2040a0">true</font><font color="4444FF">;</font> <a name="line60"> 60</a> <font color="4444FF"><strong>}</strong></font> <a name="line61"> 61</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line62"> 62</a> <strong>return</strong> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line63"> 63</a> <font color="4444FF"><strong>}</strong></font> <a name="line64"> 64</a> <font color="4444FF"><strong>}</strong></font> <a name="line65"> 65</a> <a name="line66"> 66</a> <a name="line67"> 67</a> <font color="#444444">/* <a name="line68"> 68</a> Seperators are SPACE's, TAB's, NEWLINE's or EOF's. <a name="line69"> 69</a> */</font> <a name="line70"> 70</a> <font color="#2040a0">bool</font> <font color="#2040a0">is_seperator</font><font color="4444FF">(</font> <strong>char</strong> <font color="#2040a0">c</font> <font color="4444FF">)</font> <a name="line71"> 71</a> <font color="4444FF"><strong>{</strong></font> <a name="line72"> 72</a> <strong>if</strong> <font color="4444FF">(</font> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">EOF</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line73"> 73</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\n</font>'</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line74"> 74</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">' '</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line75"> 75</a> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\t</font>'</font> <font color="4444FF">)</font> <a name="line76"> 76</a> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line77"> 77</a> <a name="line78"> 78</a> <strong>return</strong> <font color="#2040a0">true</font><font color="4444FF">;</font> <a name="line79"> 79</a> <font color="4444FF"><strong>}</strong></font> <a name="line80"> 80</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line81"> 81</a> <strong>return</strong> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line82"> 82</a> <font color="4444FF"><strong>}</strong></font> <a name="line83"> 83</a> <font color="4444FF"><strong>}</strong></font> <a name="line84"> 84</a> <a name="line85"> 85</a> <font color="#444444">/* <a name="line86"> 86</a> Returns true if str is a keyword of C, false otherwise. <a name="line87"> 87</a> */</font> <a name="line88"> 88</a> <font color="#2040a0">bool</font> <font color="#2040a0">is_keyword</font><font color="4444FF">(</font> <strong>char</strong> <font color="4444FF">*</font><font color="#2040a0">str</font> <font color="4444FF">)</font> <a name="line89"> 89</a> <font color="4444FF"><strong>{</strong></font> <a name="line90"> 90</a> <strong>if</strong> <font color="4444FF">(</font> // <font color="#2040a0">Data</font> <font color="#2040a0">types</font> <a name="line91"> 91</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"int"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line92"> 92</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"char"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line93"> 93</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"double"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line94"> 94</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"float"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line95"> 95</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"short"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line96"> 96</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"long"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line97"> 97</a> <a name="line98"> 98</a> // <font color="#2040a0">Other</font> <font color="#2040a0">keywords</font> <a name="line99"> 99</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"return"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line100">100</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"auto"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line101">101</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"break"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line102">102</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"case"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line103">103</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"const"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line104">104</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"continue"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line105">105</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"default"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line106">106</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"do"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line107">107</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"else"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line108">108</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"enum"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line109">109</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"extern"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line110">110</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"for"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line111">111</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"goto"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line112">112</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"if"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line113">113</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"register"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line114">114</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"signed"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line115">115</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"sizeof"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line116">116</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"static"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line117">117</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"struct"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line118">118</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"switch"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line119">119</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"typedef"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line120">120</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"union"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line121">121</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"unsigned"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line122">122</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"void"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line123">123</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"volatile"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line124">124</a> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#008000">"while"</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <a name="line125">125</a> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line126">126</a> <strong>return</strong> <font color="#2040a0">true</font><font color="4444FF">;</font> <a name="line127">127</a> <font color="4444FF"><strong>}</strong></font> <a name="line128">128</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line129">129</a> <strong>return</strong> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line130">130</a> <font color="4444FF"><strong>}</strong></font> <a name="line131">131</a> <font color="4444FF"><strong>}</strong></font> <a name="line132">132</a> <a name="line133">133</a> <a name="line134">134</a> <a name="line135">135</a> <font color="#444444">/* <a name="line136">136</a> Read till a space, new line, an operator or EOF is reached. <a name="line137">137</a> If an operator is read as the first character then that <a name="line138">138</a> operator is returned and function exits. Otherwise the <a name="line139">139</a> whole word is returned till another operator is found. <a name="line140">140</a> <a name="line141">141</a> NOTE: (Added: 10:49 p.m. 27/05/2008) <a name="line142">142</a> Input source file must be in UNIX file for this function to work properly. <a name="line143">143</a> <a name="line144">144</a> NOTE: What about NULL characters or other non-ASCII characters? <a name="line145">145</a> This function should be made to ignore non-alpha-numeric characters,. <a name="line146">146</a> e.g. characters with ASCII code (in decimal) < 32 and > 126 should be ignored since it's <a name="line147">147</a> not used in source code anyway. But maybe this should also be reported to the user so the person <a name="line148">148</a> knows that hidden or non-alpha-numeric characters are actually used in their source file? <a name="line149">149</a> NOTE: New lines, tabs and EOF's have ASCII values (in decimal) of less than 32 so this has to be taken <a name="line150">150</a> into account. <a name="line151">151</a> NOTE: Also what about Mac, UNIX and DOS/Windows ANSI style formatting issues? <a name="line152">152</a> e.g. how Windows uses both a line feed and a carriage return, but UNIX uses only a new line. <a name="line153">153</a> <a name="line154">154</a> */</font> <a name="line155">155</a> <strong>int</strong> <font color="#2040a0">read_word</font><font color="4444FF">(</font> <font color="#2040a0">FILE</font> <font color="4444FF">*</font><font color="#2040a0">fp</font>, <strong>char</strong> <font color="4444FF">*</font><font color="#2040a0">str</font> <font color="4444FF">)</font> <a name="line156">156</a> <font color="4444FF"><strong>{</strong></font> <a name="line157">157</a> <a name="line158">158</a> <strong>char</strong> <font color="#2040a0">buffer</font><font color="4444FF">[</font><font color="#FF0000">256</font><font color="4444FF">]</font><font color="4444FF">;</font> <a name="line159">159</a> <strong>char</strong> <font color="#2040a0">c</font><font color="4444FF">;</font> <a name="line160">160</a> <strong>int</strong> <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line161">161</a> <strong>int</strong> <font color="#2040a0">stop</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line162">162</a> <a name="line163">163</a> // <font color="#2040a0">Get</font> <font color="#2040a0">first</font> <font color="#2040a0">character</font> <a name="line164">164</a> <font color="#2040a0">c</font> <font color="4444FF">=</font> <font color="#2040a0">fgetc</font><font color="4444FF">(</font> <font color="#2040a0">fp</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line165">165</a> <strong>if</strong> <font color="4444FF">(</font> <font color="4444FF">(</font> <font color="#2040a0">is_operator</font><font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">true</font> <font color="4444FF">)</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line166">166</a> <a name="line167">167</a> <font color="#2040a0">buffer</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">c</font><font color="4444FF">;</font> <a name="line168">168</a> <font color="#2040a0">buffer</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\0</font>'</font><font color="4444FF">;</font> <a name="line169">169</a> <font color="#2040a0">strcpy</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#2040a0">buffer</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line170">170</a> // <font color="#2040a0">NOTE</font><font color="4444FF">:</font> <font color="#2040a0">But</font> <font color="#2040a0">I</font> <font color="#2040a0">don</font>'<font color="#2040a0">t</font> <font color="#2040a0">know</font> <font color="#2040a0">that</font> <font color="#2040a0">what</font> <font color="#2040a0">stopped</font> <font color="#2040a0">this</font> <font color="#2040a0">operator</font> <font color="#2040a0">is</font> <font color="#2040a0">another</font> <font color="#2040a0">operator</font>???? <a name="line171">171</a> // <font color="#2040a0">NOTE</font><font color="4444FF">:</font> <font color="#2040a0">Ok</font> <font color="#2040a0">I</font> <font color="#2040a0">found</font> <font color="#2040a0">that</font> <font color="#2040a0">it</font> <font color="#2040a0">doesn</font>'<font color="#2040a0">t</font> <font color="#2040a0">really</font> <font color="#2040a0">matter</font> <font color="#2040a0">at</font> <font color="#2040a0">this</font> <font color="#2040a0">stage</font>. <a name="line172">172</a> <strong>return</strong> <font color="#2040a0">CPFILE_OP</font><font color="4444FF">;</font> <a name="line173">173</a> <font color="4444FF"><strong>}</strong></font> <a name="line174">174</a> <a name="line175">175</a> <font color="#2040a0">buffer</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\0</font>'</font><font color="4444FF">;</font> <a name="line176">176</a> // <font color="#2040a0">Below</font> <font color="#2040a0">two</font> <font color="#2040a0">declarations</font> <font color="#2040a0">moved</font> <font color="#2040a0">to</font> <font color="#2040a0">top</font> <font color="4444FF">-</font> <font color="#2040a0">Visual</font> <font color="#2040a0">Studio</font> <font color="#2040a0">requires</font> <font color="#2040a0">all</font> <font color="#2040a0">declarations</font> <a name="line177">177</a> // <font color="#2040a0">at</font> <font color="#2040a0">the</font> <font color="#2040a0">beginning</font> <font color="#2040a0">of</font> <font color="#2040a0">a</font> <font color="#2040a0">function</font>. <a name="line178">178</a> //<strong>int</strong> <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line179">179</a> //<strong>int</strong> <font color="#2040a0">stop</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line180">180</a> <a name="line181">181</a> <strong>while</strong> <font color="4444FF">(</font> <font color="4444FF">(</font> <font color="4444FF">!</font><font color="4444FF">(</font><font color="#2040a0">is_seperator</font><font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">)</font><font color="4444FF">)</font> <font color="4444FF">)</font> <font color="4444FF">&</font><font color="4444FF">&</font> <a name="line182">182</a> <font color="4444FF">(</font> <font color="#2040a0">stop</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> <a name="line183">183</a> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line184">184</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">is_operator</font><font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">true</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line185">185</a> <a name="line186">186</a> // <font color="#2040a0">Operator</font> <font color="#2040a0">already</font> <font color="#2040a0">read</font> <font color="#2040a0">so</font> <font color="#2040a0">either</font> <font color="#2040a0">make</font> <font color="#2040a0">internal</font> <font color="#2040a0">file</font> <font color="#2040a0">pointer</font> <font color="#2040a0">point</font> <font color="#2040a0">one</font> <font color="#2040a0">back</font> <font color="#2040a0">or</font> <font color="#2040a0">save</font> <font color="#2040a0">this</font> <font color="#2040a0">one</font> <a name="line187">187</a> <font color="#2040a0">fseek</font><font color="4444FF">(</font> <font color="#2040a0">fp</font>, <font color="4444FF">-</font><font color="#FF0000">1</font>, <font color="#2040a0">SEEK_CUR</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line188">188</a> <font color="#2040a0">stop</font> <font color="4444FF">=</font> <font color="#FF0000">1</font><font color="4444FF">;</font> <a name="line189">189</a> <font color="4444FF"><strong>}</strong></font> <a name="line190">190</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line191">191</a> <a name="line192">192</a> <font color="#2040a0">buffer</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">c</font><font color="4444FF">;</font> <a name="line193">193</a> <font color="#2040a0">i</font><font color="4444FF">+</font><font color="4444FF">+</font><font color="4444FF">;</font> <a name="line194">194</a> <font color="#2040a0">buffer</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\0</font>'</font><font color="4444FF">;</font> <a name="line195">195</a> <font color="#2040a0">c</font> <font color="4444FF">=</font> <font color="#2040a0">fgetc</font><font color="4444FF">(</font> <font color="#2040a0">fp</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line196">196</a> <font color="4444FF"><strong>}</strong></font> <a name="line197">197</a> <font color="4444FF"><strong>}</strong></font> <a name="line198">198</a> <a name="line199">199</a> <font color="#2040a0">strcpy</font><font color="4444FF">(</font> <font color="#2040a0">str</font>, <font color="#2040a0">buffer</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line200">200</a> <a name="line201">201</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">EOF</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line202">202</a> <strong>return</strong> <font color="#2040a0">CPFILE_EOF</font><font color="4444FF">;</font> <a name="line203">203</a> <font color="4444FF"><strong>}</strong></font> <a name="line204">204</a> <strong>else</strong> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\n</font>'</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line205">205</a> <strong>return</strong> <font color="#2040a0">CPFILE_NL</font><font color="4444FF">;</font> <a name="line206">206</a> <font color="4444FF"><strong>}</strong></font> <a name="line207">207</a> <strong>else</strong> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">' '</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line208">208</a> <strong>return</strong> <font color="#2040a0">CPFILE_SP</font><font color="4444FF">;</font> <a name="line209">209</a> <font color="4444FF"><strong>}</strong></font> <a name="line210">210</a> <strong>else</strong> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">c</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\t</font>'</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line211">211</a> <strong>return</strong> <font color="#2040a0">CPFILE_TAB</font><font color="4444FF">;</font> <a name="line212">212</a> <font color="4444FF"><strong>}</strong></font> <a name="line213">213</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line214">214</a> <strong>return</strong> <font color="#2040a0">CPFILE_UN</font><font color="4444FF">;</font> <a name="line215">215</a> <font color="4444FF"><strong>}</strong></font> <a name="line216">216</a> <font color="4444FF"><strong>}</strong></font> <a name="line217">217</a> <a name="line218">218</a> <font color="#444444">/* <a name="line219">219</a> Make all characters in the string pointed to by "word" to NULL. <a name="line220">220</a> NOTE: Assumes the string is exactly 256 characters long! <a name="line221">221</a> */</font> <a name="line222">222</a> <strong>void</strong> <font color="#2040a0">clear_word</font><font color="4444FF">(</font> <strong>char</strong> <font color="4444FF">*</font><font color="#2040a0">word</font> <font color="4444FF">)</font> <a name="line223">223</a> <font color="4444FF"><strong>{</strong></font> <a name="line224">224</a> <strong>int</strong> <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line225">225</a> <strong>for</strong> <font color="4444FF">(</font> <font color="4444FF">;</font> <font color="#2040a0">i</font> <font color="4444FF"><</font> <font color="#FF0000">256</font><font color="4444FF">;</font> <font color="#2040a0">i</font><font color="4444FF">+</font><font color="4444FF">+</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line226">226</a> <font color="#2040a0">word</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\0</font>'</font><font color="4444FF">;</font> <a name="line227">227</a> <font color="4444FF"><strong>}</strong></font> <a name="line228">228</a> <font color="4444FF"><strong>}</strong></font> <a name="line229">229</a> <a name="line230">230</a> // <font color="#2040a0">First</font> <font color="#2040a0">part</font> <font color="#2040a0">is</font> <font color="#2040a0">strip</font> <font color="#2040a0">out</font> <font color="#2040a0">the</font> <font color="#2040a0">comments</font>. <a name="line231">231</a> // <font color="#2040a0">Then</font> <font color="#2040a0">tokenise</font>. <a name="line232">232</a> <a name="line233">233</a> <font color="#444444">/* <a name="line234">234</a> Scan the input C file and seperate the tokens. <a name="line235">235</a> */</font> <a name="line236">236</a> <font color="#2040a0">String</font><font color="4444FF">*</font> <font color="#2040a0">scan</font><font color="4444FF">(</font> <font color="#2040a0">FILE</font> <font color="4444FF">*</font><font color="#2040a0">fpIn</font>, <font color="#2040a0">FILE</font> <font color="4444FF">*</font><font color="#2040a0">fpOut</font> <font color="4444FF">)</font> <a name="line237">237</a> <font color="4444FF"><strong>{</strong></font> <a name="line238">238</a> // <font color="#2040a0">Keep</font> <font color="#2040a0">track</font> <font color="#2040a0">of</font> <font color="#2040a0">line</font> <font color="#2040a0">numbers</font> <font color="#2040a0">to</font> <font color="#2040a0">report</font> <font color="#2040a0">errors</font>. <font color="#2040a0">e</font>.<font color="#2040a0">g</font>. <font color="#2040a0">syntax</font> <font color="#2040a0">errors</font> <a name="line239">239</a> <strong>int</strong> <font color="#2040a0">line_num</font> <font color="4444FF">=</font> <font color="#FF0000">1</font><font color="4444FF">;</font> <a name="line240">240</a> <strong>char</strong> <font color="#2040a0">word</font><font color="4444FF">[</font><font color="#FF0000">256</font><font color="4444FF">]</font><font color="4444FF">;</font> // <font color="#2040a0">MAXIMUM</font> <font color="#2040a0">length</font> <font color="#2040a0">of</font> <font color="#2040a0">one</font> <font color="#2040a0">word</font> <a name="line241">241</a> <strong>int</strong> <font color="#2040a0">check</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line242">242</a> <a name="line243">243</a> // <font color="#2040a0">Program</font> <font color="#2040a0">code</font> <font color="#2040a0">is</font> <font color="#2040a0">stored</font> <font color="#2040a0">in</font> <font color="#2040a0">an</font> <font color="#2040a0">array</font> <font color="#2040a0">of</font> <font color="#2040a0">strings</font>. <a name="line244">244</a> <font color="#2040a0">String</font> <font color="4444FF">*</font><font color="#2040a0">code</font> <font color="4444FF">=</font> <font color="#2040a0">create_empty_string</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">;</font> <a name="line245">245</a> <a name="line246">246</a> // <font color="#2040a0">The</font> <font color="#2040a0">last</font> <font color="#2040a0">operator</font> <font color="#2040a0">read</font> <a name="line247">247</a> <strong>char</strong> <font color="#2040a0">last_op</font> <font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\0</font>'</font><font color="4444FF">;</font> <a name="line248">248</a> <strong>int</strong> <font color="#2040a0">is_inquotes</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line249">249</a> <a name="line250">250</a> <strong>int</strong> <font color="#2040a0">still_reading</font> <font color="4444FF">=</font> <font color="#FF0000">1</font><font color="4444FF">;</font> <a name="line251">251</a> <strong>while</strong> <font color="4444FF">(</font> <font color="#2040a0">still_reading</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">1</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line252">252</a> // <font color="#2040a0">So</font> <font color="#2040a0">I</font> <font color="#2040a0">know</font> <font color="#2040a0">a</font> <font color="#2040a0">real</font> <font color="#2040a0">token</font> <font color="#2040a0">is</font> <font color="#2040a0">read</font> <font color="#2040a0">and</font> <font color="#2040a0">not</font> <font color="#2040a0">just</font> <font color="#2040a0">whitespace</font>. <a name="line253">253</a> // <font color="#2040a0">This</font> <font color="#2040a0">is</font> <strong>for</strong> <font color="#2040a0">appending</font> <font color="#2040a0">the</font> <font color="#2040a0">new</font> <font color="#2040a0">line</font> <font color="#2040a0">at</font> <font color="#2040a0">the</font> <font color="#2040a0">end</font> <font color="#2040a0">of</font> <font color="#2040a0">the</font> <font color="#2040a0">token</font>. <a name="line254">254</a> <font color="#2040a0">bool</font> <font color="#2040a0">token_not_null</font> <font color="4444FF">=</font> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line255">255</a> <a name="line256">256</a> <font color="#2040a0">clear_word</font><font color="4444FF">(</font> <font color="#2040a0">word</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line257">257</a> <font color="#2040a0">check</font> <font color="4444FF">=</font> <font color="#2040a0">read_word</font><font color="4444FF">(</font> <font color="#2040a0">fpIn</font>, <font color="#2040a0">word</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line258">258</a> <a name="line259">259</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">word</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\0</font>'</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line260">260</a> <font color="#2040a0">printf</font><font color="4444FF">(</font> <font color="#008000">"word: %s<font color="#77dd77">\n</font>"</font>, <font color="#008000">"NULL"</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line261">261</a> <font color="4444FF"><strong>}</strong></font> <a name="line262">262</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line263">263</a> <font color="#2040a0">printf</font><font color="4444FF">(</font> <font color="#008000">"word: %s<font color="#77dd77">\n</font>"</font>, <font color="#2040a0">word</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line264">264</a> <font color="#2040a0">fputs</font><font color="4444FF">(</font> <font color="#2040a0">word</font>, <font color="#2040a0">fpOut</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line265">265</a> <font color="#2040a0">fputs</font><font color="4444FF">(</font> <font color="#008000">"<font color="#77dd77">\n</font>"</font>, <font color="#2040a0">fpOut</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line266">266</a> <a name="line267">267</a> <font color="#2040a0">append_cstring</font><font color="4444FF">(</font> <font color="#2040a0">code</font>, <font color="#2040a0">word</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line268">268</a> <font color="#2040a0">token_not_null</font> <font color="4444FF">=</font> <font color="#2040a0">true</font><font color="4444FF">;</font> <a name="line269">269</a> <font color="4444FF"><strong>}</strong></font> <a name="line270">270</a> <a name="line271">271</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">check</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">CPFILE_EOF</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line272">272</a> <font color="#2040a0">still_reading</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line273">273</a> <font color="4444FF"><strong>}</strong></font> <a name="line274">274</a> <strong>else</strong> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">check</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">CPFILE_NL</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line275">275</a> <font color="#2040a0">line_num</font><font color="4444FF">+</font><font color="4444FF">+</font><font color="4444FF">;</font> <a name="line276">276</a> <font color="4444FF"><strong>}</strong></font> <a name="line277">277</a> <strong>else</strong> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">check</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">CPFILE_SP</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line278">278</a> // <font color="#2040a0">Space</font> <font color="#2040a0">has</font> <font color="#2040a0">been</font> <font color="#2040a0">read</font> <a name="line279">279</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">is_inquotes</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">1</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line280">280</a> <font color="#2040a0">append_cstring</font><font color="4444FF">(</font> <font color="#2040a0">code</font>, <font color="#008000">" "</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line281">281</a> <font color="4444FF"><strong>}</strong></font> <a name="line282">282</a> <font color="4444FF"><strong>}</strong></font> <a name="line283">283</a> <strong>else</strong> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">check</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">CPFILE_TAB</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line284">284</a> // <font color="#2040a0">TAB</font> <font color="#2040a0">has</font> <font color="#2040a0">been</font> <font color="#2040a0">read</font> <a name="line285">285</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">is_inquotes</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">1</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line286">286</a> <font color="#2040a0">append_cstring</font><font color="4444FF">(</font> <font color="#2040a0">code</font>, <font color="#008000">"<font color="#77dd77">\t</font>"</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line287">287</a> <font color="4444FF"><strong>}</strong></font> <a name="line288">288</a> <font color="4444FF"><strong>}</strong></font> <a name="line289">289</a> <strong>else</strong> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">check</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">CPFILE_OP</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line290">290</a> // <font color="#2040a0">Operator</font> <font color="#2040a0">read</font> <a name="line291">291</a> <font color="#2040a0">last_op</font> <font color="4444FF">=</font> <font color="#2040a0">word</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font><font color="4444FF">;</font> <a name="line292">292</a> <strong>if</strong> <font color="4444FF">(</font> <font color="4444FF">(</font><font color="#2040a0">last_op</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'"'</font><font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <font color="4444FF">(</font><font color="#2040a0">last_op</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\'</font>'</font><font color="4444FF">)</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line293">293</a> <font color="#2040a0">is_inquotes</font> <font color="4444FF">=</font> <font color="4444FF">(</font><font color="#2040a0">is_inquotes</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">1</font><font color="4444FF">)</font> ? <font color="#FF0000">0</font> <font color="4444FF">:</font> <font color="#FF0000">1</font><font color="4444FF">;</font> <a name="line294">294</a> <font color="4444FF"><strong>}</strong></font> <a name="line295">295</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line296">296</a> // <font color="#2040a0">What</font> <font color="#2040a0">about</font> <font color="#2040a0">single</font> <font color="#2040a0">characters</font>? <font color="#2040a0">Like</font> <font color="#008000">'a'</font> ? <a name="line297">297</a> // <font color="#2040a0">In</font> <font color="#2040a0">this</font> <strong>case</strong> <font color="#2040a0">no</font> <font color="#2040a0">need</font> <font color="#2040a0">to</font> <font color="#2040a0">worry</font> <font color="#2040a0">about</font> <font color="#2040a0">spaces</font>? <a name="line298">298</a> <font color="4444FF"><strong>}</strong></font> <a name="line299">299</a> <font color="4444FF"><strong>}</strong></font> <a name="line300">300</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line301">301</a> // <font color="#2040a0">Unknown</font> <font color="#2040a0">character</font> <font color="#2040a0">read</font> <a name="line302">302</a> <font color="4444FF"><strong>}</strong></font> <a name="line303">303</a> <a name="line304">304</a> // <font color="#2040a0">Append</font> <font color="#2040a0">a</font> <font color="#2040a0">new</font> <font color="#2040a0">line</font> <font color="#2040a0">to</font> <font color="#008000">"code"</font> <font color="#2040a0">to</font> <font color="#2040a0">let</font> <font color="#2040a0">the</font> <font color="#2040a0">tokeniser</font> <font color="#2040a0">know</font> <font color="#2040a0">where</font> <font color="#2040a0">the</font> <a name="line305">305</a> // <font color="#2040a0">tokens</font> <font color="#2040a0">seperate</font>. <a name="line306">306</a> <strong>if</strong> <font color="4444FF">(</font> <font color="4444FF">(</font><font color="#2040a0">token_not_null</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">true</font><font color="4444FF">)</font> <font color="4444FF">&</font><font color="4444FF">&</font> <font color="4444FF">(</font><font color="#2040a0">is_inquotes</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">)</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line307">307</a> <font color="#2040a0">append_cstring</font><font color="4444FF">(</font> <font color="#2040a0">code</font>, <font color="#008000">"<font color="#77dd77">\n</font>"</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line308">308</a> <font color="4444FF"><strong>}</strong></font> <a name="line309">309</a> <font color="4444FF"><strong>}</strong></font> <a name="line310">310</a> <font color="#2040a0">printf</font><font color="4444FF">(</font> <font color="#008000">"Num of lines: %d<font color="#77dd77">\n</font>"</font>, <font color="#2040a0">line_num</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line311">311</a> <font color="#2040a0">print_string</font><font color="4444FF">(</font> <font color="#2040a0">code</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line312">312</a> //<font color="#2040a0">fputs</font><font color="4444FF">(</font> <font color="#2040a0">get_cstring</font><font color="4444FF">(</font> <font color="#2040a0">code</font> <font color="4444FF">)</font>, <font color="#2040a0">fpOut</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line313">313</a> <a name="line314">314</a> <strong>return</strong> <font color="#2040a0">code</font><font color="4444FF">;</font> <a name="line315">315</a> <font color="4444FF"><strong>}</strong></font>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body> </html>