<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Bison on Abdullah Diab&#39;s Blog</title>
    <link>https://mpcabd.xyz/tags/bison/</link>
    <description>Recent content in Bison on Abdullah Diab&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 07 Oct 2009 16:47:49 +0000</lastBuildDate>
    <atom:link href="https://mpcabd.xyz/tags/bison/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Binding NetBeans with Flex</title>
      <link>https://mpcabd.xyz/binding-netbeans-with-flex/</link>
      <pubDate>Wed, 07 Oct 2009 16:47:49 +0000</pubDate>
      <guid>https://mpcabd.xyz/binding-netbeans-with-flex/</guid>
      <description>&lt;p&gt;In my faculty – Informatics Engineering, Damascus University -, in the 4th year of Software Engineering Department, we have to build a compiler :shock:.&lt;/p&gt;
&lt;p&gt;I like using NetBeans for developing applications, it supports lots of languages and tools; like C, C++, Java, Python, Ruby and more. The compiler would be implemented in C++.&lt;/p&gt;
&lt;p&gt;The first two phases of implementing a compiler are: building a lexer (tokenizer) and building a parser. The lexer is implemented using &lt;a href=&#34;http://www.gnu.org/software/flex/&#34; target=&#34;_blank&#34;&gt;GNU Flex&lt;/a&gt; while the parser is implemented using &lt;a href=&#34;http://www.gnu.org/software/bison/&#34; target=&#34;_blank&#34;&gt;GNU Bison&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id=&#34;the-problem&#34;&gt;The Problem&lt;/h1&gt;
&lt;p&gt;The first problem I faced when starting the project was to tell NetBeans how to handle the lex file:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The lex file must be passed to the flex tool to generate a C++ code file.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The second problem was that when the code file is generated it contains errors:&lt;/p&gt;
&lt;pre class=&#34;lang:c++ decode:true&#34;&gt;#ifdef __cplusplus

#include &amp;lt;stdlib.h&amp;gt;
class istream;
#include &amp;lt;unistd.h&amp;gt;
&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;When using building the file you’ll find that it contains errors regarding the usage of istream. (You might not face this problem, then you’re a lucky programmer).&lt;/em&gt;&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>In my faculty – Informatics Engineering, Damascus University -, in the 4th year of Software Engineering Department, we have to build a compiler :shock:.</p>
<p>I like using NetBeans for developing applications, it supports lots of languages and tools; like C, C++, Java, Python, Ruby and more. The compiler would be implemented in C++.</p>
<p>The first two phases of implementing a compiler are: building a lexer (tokenizer) and building a parser. The lexer is implemented using <a href="http://www.gnu.org/software/flex/" target="_blank">GNU Flex</a> while the parser is implemented using <a href="http://www.gnu.org/software/bison/" target="_blank">GNU Bison</a>.</p>
<h1 id="the-problem">The Problem</h1>
<p>The first problem I faced when starting the project was to tell NetBeans how to handle the lex file:</p>
<p><em>The lex file must be passed to the flex tool to generate a C++ code file.</em></p>
<p>The second problem was that when the code file is generated it contains errors:</p>
<pre class="lang:c++ decode:true">#ifdef __cplusplus

#include &lt;stdlib.h&gt;
class istream;
#include &lt;unistd.h&gt;
</pre>
<p><em>When using building the file you’ll find that it contains errors regarding the usage of istream. (You might not face this problem, then you’re a lucky programmer).</em></p>
<h1 id="the-first-problem-binding-netbeans-with-flex">The first problem: Binding NetBeans with Flex</h1>
<p>From NetBeans –&gt; Projects Side Tab –&gt; right click on the lex file –&gt; Properties:</p>
<p>Categories –&gt; General:</p>
<ul>
<li>
<p>Uncheck “Excluded From Build”.</p>
</li>
<li>
<p>Select “Custom Build Tool” in the Tool drop down list.</p>
</li>
</ul>
<p>Categories –&gt; Custom Build Step:</p>
<ul>
<li>
<p>Enter in the Command Line field: flex lex.l <em>Assuming that your lex file is called lex.l</em></p>
</li>
<li>
<p>Enter in the Outputs field: lex.yy.cc</p>
</li>
<li>
<p>Enter in the Description: Running Flex …</p>
</li>
</ul>
<p>Press Ok.</p>
<p>Now when you compile the solution your lex file will be passed the the flex tool.</p>
<h1 id="the-second-problem-fixing-the-error">The second problem: Fixing the error</h1>
<p>When I started fixing the error, I was doing it by hand, but I got bored of the process so I wrote a small Python script to do this for me:</p>
<p><em>Assuming that the output file of the lex file is called lex.yy.cc:</em></p>
<pre class="lang:python decode:true">f = open('lex.yy.cc')
lines = f.readlines()
i = 0
for i in range(len(lines)):
    if lines[i] == 'class istream;\n':
        break;
else:
    return;
lines[i] = '#include &lt;iostream&gt;\n'
lines.insert(i + 1, 'using namespace std;\n')
f.close()
f = open('lex.yy.cc', 'w')
f.writelines(lines)
f.close()
</pre>
<p>The script is easy, it opens the file, changes the line from “class istream;” to “#include <iostream>”, inserts “using namespace std;” next.</p>
<p>Put the file next to your sources and run it after the build process to fix the error.</p>
<h1 id="fixing-both-problems-without-executing-anything-manually">Fixing both problems without executing anything manually:</h1>
<p>After a while I got bored of executing the script manually and I decided that the script must be executed automatically.</p>
<p>To do that the script must be called by NetBeans, so back to the lex file properties I changed the Custom Build Command from “flex lex.l” to “python fixer.py”.</p>
<p>Now the fixer script is called to build the lex file, so the script must change to call the flex from the script itself:</p>
<pre class="lang:python decode:true">import os

os.system("flex token.l")

f = open('lex.yy.cc')
lines = f.readlines()
i = 0
for i in range(len(lines)):
    if lines[i] == 'class istream;\n':
        break;
else:
    return;
lines[i] = '#include &lt;iostream&gt;\n'
lines.insert(i + 1, 'using namespace std;\n')
f.close()
f = open('lex.yy.cc', 'w')
f.writelines(lines)
f.close()
</pre>
<p>Now all my problems are solved, hope that yours will be solved too 🙂</p>]]></content:encoded>
    </item>
  </channel>
</rss>
