SE250:lab-9:rwan064:tokenise.c
<html> <head>
<title>tokenise.c</title>
</head> <body bgcolor="#ffffff" text="#000000">
<a name="line1"> 1</a> <font color="#444444">/* <a name="line2"> 2</a> File: tokenise.c <a name="line3"> 3</a> Purpose: Tokeniser for SE250 Lab 9. <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"><stdio.h></font></strong></font> <a name="line8"> 8</a> <font color="0000ff"><strong>#include <font color="#008000"><string.h></font></strong></font> <a name="line9"> 9</a> <font color="0000ff"><strong>#include <font color="#008000"><stdlib.h></font></strong></font> <a name="line10"> 10</a> <font color="0000ff"><strong>#include <font color="#008000"><stdarg.h></font></strong></font> <a name="line11"> 11</a> <font color="0000ff"><strong>#include <font color="#008000"><assert.h></font></strong></font> <a name="line12"> 12</a> <font color="0000ff"><strong>#include <font color="#008000"><ctype.h></font></strong></font> <a name="line13"> 13</a> <font color="0000ff"><strong>#include <font color="#008000">"tokenise.h"</font></strong></font> <a name="line14"> 14</a> <font color="0000ff"><strong>#include <font color="#008000">"string.h"</font></strong></font> <a name="line15"> 15</a> <font color="0000ff"><strong>#include <font color="#008000">"scanner.h"</font></strong></font> <a name="line16"> 16</a> <a name="line17"> 17</a> <a name="line18"> 18</a> <font color="#444444">/* <a name="line19"> 19</a> TODO: <a name="line20"> 20</a> Must remove comments first from input file. <a name="line21"> 21</a> */</font> <a name="line22"> 22</a> <a name="line23"> 23</a> <a name="line24"> 24</a> <font color="#444444">/* The tokeniser interface */</font> <a name="line25"> 25</a> <a name="line26"> 26</a> <strong>void</strong> <font color="#2040a0">init_TokenStream</font><font color="4444FF">(</font> <font color="#2040a0">TokenStream</font><font color="4444FF">*</font> <font color="#2040a0">tokens</font>, <strong>char</strong><font color="4444FF">*</font> <font color="#2040a0">s</font> <font color="4444FF">)</font> <a name="line27"> 27</a> <font color="4444FF"><strong>{</strong></font> <a name="line28"> 28</a> <strong>int</strong> <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line29"> 29</a> <strong>int</strong> <font color="#2040a0">j</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <a name="line30"> 30</a> <font color="#2040a0">String</font> <font color="4444FF">*</font><font color="#2040a0">buffer</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="line31"> 31</a> <strong>for</strong> <font color="4444FF">(</font> <font color="4444FF">;</font> <font color="#2040a0">s</font><font color="4444FF">[</font><font color="#2040a0">i</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="#2040a0">s</font><font color="4444FF">+</font><font color="4444FF">+</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line32"> 32</a> // <font color="#2040a0">Read</font> <font color="#2040a0">all</font> <font color="#2040a0">characters</font> <a name="line33"> 33</a> <strong>for</strong> <font color="4444FF">(</font> <font color="4444FF">;</font> <font color="#2040a0">s</font><font color="4444FF">[</font><font color="#2040a0">j</font><font color="4444FF">]</font> <font color="4444FF">!</font><font color="4444FF">=</font> <font color="#008000">'<font color="#77dd77">\n</font>'</font><font color="4444FF">;</font> <font color="#2040a0">s</font><font color="4444FF">+</font><font color="4444FF">+</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line34"> 34</a> // <font color="#2040a0">Read</font> <font color="#2040a0">all</font> <font color="#2040a0">tokens</font> <a name="line35"> 35</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">buffer</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">NULL</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line36"> 36</a> <font color="#2040a0">buffer</font> <font color="4444FF">=</font> <font color="4444FF">(</font><strong>char</strong><font color="4444FF">*</font><font color="4444FF">)</font> <font color="#2040a0">malloc</font><font color="4444FF">(</font><strong>sizeof</strong><font color="4444FF">(</font><strong>char</strong><font color="4444FF">)</font><font color="4444FF">)</font><font color="4444FF">;</font> <a name="line37"> 37</a> <font color="4444FF"><strong>}</strong></font> <a name="line38"> 38</a> <a name="line39"> 39</a> <font color="4444FF"><strong>}</strong></font> <a name="line40"> 40</a> <font color="#2040a0">buffer</font> <font color="4444FF">=</font> <font color="#2040a0">NULL</font><font color="4444FF">;</font> <a name="line41"> 41</a> <font color="4444FF"><strong>}</strong></font> <a name="line42"> 42</a> <font color="4444FF"><strong>}</strong></font> <a name="line43"> 43</a> <a name="line44"> 44</a> <a name="line45"> 45</a> <font color="#2040a0">bool</font> <font color="#2040a0">eqToken</font><font color="4444FF">(</font> <font color="#2040a0">Token</font> <font color="4444FF">*</font><font color="#2040a0">a</font>, <font color="#2040a0">Token</font> <font color="4444FF">*</font><font color="#2040a0">b</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line46"> 46</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">a</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">type</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">b</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">type</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line47"> 47</a> <strong>switch</strong> <font color="4444FF">(</font> <font color="#2040a0">a</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">type</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line48"> 48</a> <strong>case</strong> <font color="#2040a0">T_SYMBOL</font><font color="4444FF">:</font> <a name="line49"> 49</a> <strong>return</strong> <font color="4444FF">(</font><font color="#2040a0">a</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">symval</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">b</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">symval</font><font color="4444FF">)</font> ? <font color="#2040a0">true</font> <font color="4444FF">:</font> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line50"> 50</a> <strong>break</strong><font color="4444FF">;</font> <a name="line51"> 51</a> <strong>case</strong> <font color="#2040a0">T_IDENT</font><font color="4444FF">:</font> <a name="line52"> 52</a> <strong>return</strong> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">a</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">strval</font>, <font color="#2040a0">b</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">strval</font><font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> ? <font color="#2040a0">true</font> <font color="4444FF">:</font> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line53"> 53</a> <strong>break</strong><font color="4444FF">;</font> <a name="line54"> 54</a> <strong>case</strong> <font color="#2040a0">T_INTEGER</font><font color="4444FF">:</font> <a name="line55"> 55</a> <strong>return</strong> <font color="4444FF">(</font><font color="#2040a0">a</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">intval</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">b</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">intval</font><font color="4444FF">)</font> ? <font color="#2040a0">true</font> <font color="4444FF">:</font> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line56"> 56</a> <strong>break</strong><font color="4444FF">;</font> <a name="line57"> 57</a> <strong>case</strong> <font color="#2040a0">T_FLOAT</font><font color="4444FF">:</font> <a name="line58"> 58</a> <strong>return</strong> <font color="4444FF">(</font><font color="#2040a0">a</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">fltval</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">b</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">fltval</font><font color="4444FF">)</font> ? <font color="#2040a0">true</font> <font color="4444FF">:</font> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line59"> 59</a> <strong>break</strong><font color="4444FF">;</font> <a name="line60"> 60</a> <strong>case</strong> <font color="#2040a0">T_STRING</font><font color="4444FF">:</font> <a name="line61"> 61</a> <strong>return</strong> <font color="4444FF">(</font> <font color="#2040a0">strcmp</font><font color="4444FF">(</font> <font color="#2040a0">a</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">strval</font>, <font color="#2040a0">b</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">val</font>.<font color="#2040a0">strval</font><font color="4444FF">)</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font> ? <font color="#2040a0">true</font> <font color="4444FF">:</font> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line62"> 62</a> <strong>break</strong><font color="4444FF">;</font> <a name="line63"> 63</a> <strong>case</strong> <font color="#2040a0">T_END</font><font color="4444FF">:</font> <a name="line64"> 64</a> <strong>return</strong> <font color="#2040a0">true</font><font color="4444FF">;</font> <a name="line65"> 65</a> <strong>break</strong><font color="4444FF">;</font> <a name="line66"> 66</a> <strong>case</strong> <font color="#2040a0">T_NOTHING</font><font color="4444FF">:</font> <a name="line67"> 67</a> <strong>return</strong> <font color="#2040a0">true</font><font color="4444FF">;</font> <a name="line68"> 68</a> <strong>break</strong><font color="4444FF">;</font> <a name="line69"> 69</a> <strong>default</strong><font color="4444FF">:</font> <a name="line70"> 70</a> // <font color="#2040a0">What</font> <font color="#2040a0">to</font> <strong>do</strong> <font color="#2040a0">in</font> <font color="#2040a0">the</font> <strong>default</strong> <strong>case</strong>? <a name="line71"> 71</a> <strong>break</strong><font color="4444FF">;</font> <a name="line72"> 72</a> <font color="4444FF"><strong>}</strong></font> <a name="line73"> 73</a> <strong>return</strong> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line74"> 74</a> <font color="4444FF"><strong>}</strong></font> <a name="line75"> 75</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line76"> 76</a> <strong>return</strong> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line77"> 77</a> <font color="4444FF"><strong>}</strong></font> <a name="line78"> 78</a> <font color="4444FF"><strong>}</strong></font> <a name="line79"> 79</a> <a name="line80"> 80</a> <a name="line81"> 81</a> <strong>void</strong> <font color="#2040a0">print_token</font><font color="4444FF">(</font> <font color="#2040a0">Token</font><font color="4444FF">*</font> <font color="#2040a0">t</font> <font color="4444FF">)</font> <a name="line82"> 82</a> <font color="4444FF"><strong>{</strong></font> <a name="line83"> 83</a> <font color="#444444">/* TO BE IMPLEMENTED */</font> <a name="line84"> 84</a> <font color="4444FF"><strong>}</strong></font> <a name="line85"> 85</a> <a name="line86"> 86</a> <font color="#2040a0">Token</font><font color="4444FF">*</font> <font color="#2040a0">current</font><font color="4444FF">(</font> <font color="#2040a0">TokenStream</font><font color="4444FF">*</font> <font color="#2040a0">tokens</font> <font color="4444FF">)</font> <a name="line87"> 87</a> <font color="4444FF"><strong>{</strong></font> <a name="line88"> 88</a> <strong>return</strong> <font color="#2040a0">tokens</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">current</font><font color="4444FF">;</font> <a name="line89"> 89</a> <font color="4444FF"><strong>}</strong></font> <a name="line90"> 90</a> <a name="line91"> 91</a> <strong>void</strong> <font color="#2040a0">advance</font><font color="4444FF">(</font> <font color="#2040a0">TokenStream</font><font color="4444FF">*</font> <font color="#2040a0">tokens</font> <font color="4444FF">)</font> <a name="line92"> 92</a> <font color="4444FF"><strong>{</strong></font> <a name="line93"> 93</a> <font color="#444444">/* TO BE IMPLEMENTED */</font> <a name="line94"> 94</a> <font color="4444FF"><strong>}</strong></font> <a name="line95"> 95</a> <a name="line96"> 96</a> <strong>void</strong> <font color="#2040a0">expect</font><font color="4444FF">(</font> <font color="#2040a0">TokenStream</font><font color="4444FF">*</font> <font color="#2040a0">tokens</font>, <font color="#2040a0">Token</font><font color="4444FF">*</font> <font color="#2040a0">expected</font> <font color="4444FF">)</font> <a name="line97"> 97</a> <font color="4444FF"><strong>{</strong></font> <a name="line98"> 98</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">eqToken</font><font color="4444FF">(</font> <font color="#2040a0">current</font><font color="4444FF">(</font> <font color="#2040a0">tokens</font> <font color="4444FF">)</font>, <font color="#2040a0">expected</font> <font color="4444FF">)</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line99"> 99</a> <font color="#2040a0">advance</font><font color="4444FF">(</font> <font color="#2040a0">tokens</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line100">100</a> <font color="4444FF"><strong>}</strong></font> <a name="line101">101</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line102">102</a> <font color="#2040a0">error</font><font color="4444FF">(</font> <font color="#008000">"Expected '%c', but found '%c', near <font color="#77dd77">\"</font>%.40s<font color="#77dd77">\"</font>"</font>, <a name="line103">103</a> <font color="#2040a0">expected</font>, <a name="line104">104</a> <font color="#2040a0">current</font><font color="4444FF">(</font> <font color="#2040a0">tokens</font> <font color="4444FF">)</font>, <a name="line105">105</a> <font color="4444FF">*</font><font color="#2040a0">tokens</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line106">106</a> <font color="4444FF"><strong>}</strong></font> <a name="line107">107</a> <font color="4444FF"><strong>}</strong></font> <a name="line108">108</a> <a name="line109">109</a> <strong>void</strong> <font color="#2040a0">error</font><font color="4444FF">(</font> <strong>char</strong><font color="4444FF">*</font> <font color="#2040a0">fmt</font>, ... <font color="4444FF">)</font> <a name="line110">110</a> <font color="4444FF"><strong>{</strong></font> <a name="line111">111</a> <font color="#2040a0">va_list</font> <font color="#2040a0">args</font><font color="4444FF">;</font> <a name="line112">112</a> <font color="#2040a0">va_start</font><font color="4444FF">(</font> <font color="#2040a0">args</font>, <font color="#2040a0">fmt</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line113">113</a> <font color="#2040a0">vprintf</font><font color="4444FF">(</font> <font color="#2040a0">fmt</font>, <font color="#2040a0">args</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line114">114</a> <font color="#2040a0">va_end</font><font color="4444FF">(</font> <font color="#2040a0">args</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line115">115</a> <font color="#2040a0">printf</font><font color="4444FF">(</font> <font color="#008000">"<font color="#77dd77">\n</font>"</font> <font color="4444FF">)</font><font color="4444FF">;</font> <a name="line116">116</a> <font color="4444FF"><strong>}</strong></font> <a name="line117">117</a> <a name="line118">118</a> <font color="#2040a0">bool</font> <font color="#2040a0">isVariable</font><font color="4444FF">(</font> <font color="#2040a0">Token</font><font color="4444FF">*</font> <font color="#2040a0">t</font> <font color="4444FF">)</font> <a name="line119">119</a> <font color="4444FF"><strong>{</strong></font> <a name="line120">120</a> <strong>if</strong> <font color="4444FF">(</font> <font color="#2040a0">t</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">type</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">T_IDENT</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line121">121</a> <strong>return</strong> <font color="#2040a0">true</font><font color="4444FF">;</font> <a name="line122">122</a> <font color="4444FF"><strong>}</strong></font> <a name="line123">123</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line124">124</a> <strong>return</strong> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line125">125</a> <font color="4444FF"><strong>}</strong></font> <a name="line126">126</a> <font color="4444FF"><strong>}</strong></font> <a name="line127">127</a> <a name="line128">128</a> <font color="#2040a0">bool</font> <font color="#2040a0">isConstant</font><font color="4444FF">(</font> <font color="#2040a0">Token</font><font color="4444FF">*</font> <font color="#2040a0">t</font> <font color="4444FF">)</font> <a name="line129">129</a> <font color="4444FF"><strong>{</strong></font> <a name="line130">130</a> <strong>if</strong> <font color="4444FF">(</font> <font color="4444FF">(</font><font color="#2040a0">t</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">type</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">T_INTEGER</font><font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line131">131</a> <font color="4444FF">(</font><font color="#2040a0">t</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">type</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">T_FLOAT</font> <font color="4444FF">)</font> <font color="4444FF">|</font><font color="4444FF">|</font> <a name="line132">132</a> <font color="4444FF">(</font><font color="#2040a0">t</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">type</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">T_STRING</font> <font color="4444FF">)</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font> <a name="line133">133</a> <a name="line134">134</a> <strong>return</strong> <font color="#2040a0">true</font><font color="4444FF">;</font> <a name="line135">135</a> <font color="4444FF"><strong>}</strong></font> <a name="line136">136</a> <strong>else</strong> <font color="4444FF"><strong>{</strong></font> <a name="line137">137</a> // <font color="#2040a0">Not</font> <font color="#2040a0">an</font> <strong>int</strong>, <strong>float</strong> <font color="#2040a0">or</font> <font color="#2040a0">string</font> <font color="#2040a0">constant</font>. <a name="line138">138</a> <strong>return</strong> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line139">139</a> <font color="4444FF"><strong>}</strong></font> <a name="line140">140</a> <font color="4444FF"><strong>}</strong></font> <a name="line141">141</a> <a name="line142">142</a> <font color="#2040a0">bool</font> <font color="#2040a0">end_of_tokens</font><font color="4444FF">(</font> <font color="#2040a0">TokenStream</font><font color="4444FF">*</font> <font color="#2040a0">tokens</font> <font color="4444FF">)</font> <a name="line143">143</a> <font color="4444FF"><strong>{</strong></font> <a name="line144">144</a> <strong>return</strong> <font color="4444FF">(</font><font color="#2040a0">tokens</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">current</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">type</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">T_END</font><font color="4444FF">)</font> ? <font color="#2040a0">true</font> <font color="4444FF">:</font> <font color="#2040a0">false</font><font color="4444FF">;</font> <a name="line145">145</a> <font color="4444FF"><strong>}</strong></font> <a name="line146">146</a> <a name="line147">147</a> <strong>void</strong> <font color="#2040a0">init_predefined_tokens</font><font color="4444FF">(</font> <font color="4444FF">)</font> <a name="line148">148</a> <font color="4444FF"><strong>{</strong></font> <a name="line149">149</a> <font color="#444444">/* TO BE IMPLEMENTED */</font> <a name="line150">150</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>