<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>boriel.com &#187; Compilers</title>
	<atom:link href="http://www.boriel.com/category/tag/informatica/compilers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.boriel.com</link>
	<description>Hacks, science and personal rants</description>
	<lastBuildDate>Wed, 28 Dec 2011 22:50:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Self-replication</title>
		<link>http://www.boriel.com/2010/02/10/selfreplication/?lang=en</link>
		<comments>http://www.boriel.com/2010/02/10/selfreplication/?lang=en#comments</comments>
		<pubDate>Wed, 10 Feb 2010 21:40:06 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=1229</guid>
		<description><![CDATA[Reading an article linked from Barrapunto (a Spanish Slashdot-like site) entitled Reflections on Trusting Trust it came into my attention the paragraph about self-reproducing programs. The author of the article, Ken Thomson says: More precisely stated, the problem is to write a source program that, when compiled and executed, will produce as output an exact [...]]]></description>
			<content:encoded><![CDATA[<p>Reading an article linked from <a href="http://barrapunto.com/articles/10/02/09/1126247.shtml">Barrapunto</a> (a Spanish Slashdot-like site) entitled <a href="http://cm.bell-labs.com/who/ken/trust.html">Reflections on Trusting Trust</a> it came into my attention the paragraph about self-reproducing programs. The author of the article,  <a href="http://en.wikipedia.org/wiki/Ken_Thompson">Ken Thomson</a> says:</p>
<blockquote><p>More precisely stated, the problem is to write a source program that, when compiled and executed, will produce as output an exact copy of its source. If you have never done this, I urge you to try it on your own. The discovery of how to do it is a revelation that far surpasses any benefit obtained by being told how to do it.</p></blockquote>
<p>I&#8217;ve never tried to code such a thing, so I put my shoulder to the wheel and try this exercise in python. After a few minutes, I got this solution. It&#8217;s not the shortest (especially considering it reproduces the shebang and the coding lines), but it works. As Mr. Thomson says, it&#8217;s a very interesting exercise. Here&#8217;s the code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python
# -*- coding: utf-8 -*-
</span><span class="hl-identifier">data</span><span class="hl-default"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">#!/usr/bin/env python</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string"># -*- coding: utf-8 -*-</span><span class="hl-quotes">'</span><span class="hl-code">,
        </span><span class="hl-quotes">&quot;</span><span class="hl-string">for x in data[:2]:</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">    print x</span><span class="hl-quotes">&quot;</span><span class="hl-code">,
        </span><span class="hl-quotes">&quot;</span><span class="hl-string">print 'data = ', data</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">for x in data[2:]:</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">    print x</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">]
</span><span class="hl-reserved">for </span><span class="hl-identifier">x </span><span class="hl-reserved">in </span><span class="hl-identifier">data</span><span class="hl-brackets">[</span><span class="hl-code">:</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">:
    </span><span class="hl-reserved">print </span><span class="hl-identifier">x
</span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">data = </span><span class="hl-quotes">'</span><span class="hl-default">, </span><span class="hl-identifier">data
</span><span class="hl-reserved">for </span><span class="hl-identifier">x </span><span class="hl-reserved">in </span><span class="hl-identifier">data</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-code">:</span><span class="hl-brackets">]</span><span class="hl-default">:
    </span><span class="hl-reserved">print </span><span class="hl-identifier">x</span></pre></div></div>
<p>Save it as prog1.py, and then execute <strong>python prog1.py</strong>.<br />
Even better, try <strong>python prog1.py &gt; prog2.py</strong> and then <strong>python prog2.py &gt; prog3.py</strong> and so on.</p>
<div id="tweetbutton1229" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2010%2F02%2F10%2Fselfreplication%2F%3Flang%3Den&amp;via=boriel&amp;text=Self-replication%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2010%2F02%2F10%2Fselfreplication%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2010/02/10/selfreplication/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ZX BASIC 1.0 released!</title>
		<link>http://www.boriel.com/2008/06/29/zx-basic-10-released/?lang=en</link>
		<comments>http://www.boriel.com/2008/06/29/zx-basic-10-released/?lang=en#comments</comments>
		<pubDate>Sun, 29 Jun 2008 23:08:14 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[ZX Spectrum]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=125</guid>
		<description><![CDATA[Well, first things first: I&#8217;m not a footbal (soccer) fan, but must say congratulations to the Spaniards footbal team and their fans for having won the Eurocup (read the news -Spanish- at El País and El Mundo, for example). I, on my behalf, have something else to celebrate: I&#8217;ve eventually released 1.0 version of the [...]]]></description>
			<content:encoded><![CDATA[<p>Well, first things first: I&#8217;m not a footbal (soccer) fan, but must say <i>congratulations</i> to the Spaniards footbal team and their fans <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  for having won the Eurocup (read the news -Spanish- at <a href="http://www.elpais.com/articulo/deportes/gloria/mejor/elpepudep/20080629elpepudep_11/Tes">El País</a> and <a href="http://www.elmundo.es/eurocopa/2008/2008/06/29/espana/1214759858.html">El Mundo</a>, for example).</p>
<p>I, on my behalf, have something else to celebrate: <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  I&#8217;ve eventually released 1.0 version of the <a href="http://www.boriel.com/2008/05/05/zx-spectrum-basic-compiler/">ZX Spectrum BASIC compiler</a>. You can <a href="http://www.boriel.com/files/zxb/">download</a> it and give it a try, if interested.</p>
<p>If you&#8217;re having troubles either installing or using it, or suspect of a bug, please ask for help at the <a href="http://www.boriel.com/forum/">forum</a>.</p>
<div id="tweetbutton125" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2008%2F06%2F29%2Fzx-basic-10-released%2F%3Flang%3Den&amp;via=boriel&amp;text=ZX%20BASIC%201.0%20released%21%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2008%2F06%2F29%2Fzx-basic-10-released%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2008/06/29/zx-basic-10-released/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A ZX Spectrum BASIC Compiler</title>
		<link>http://www.boriel.com/2008/05/05/zx-spectrum-basic-compiler/?lang=en</link>
		<comments>http://www.boriel.com/2008/05/05/zx-spectrum-basic-compiler/?lang=en#comments</comments>
		<pubDate>Mon, 05 May 2008 20:06:52 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[ZX Spectrum]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=121</guid>
		<description><![CDATA[Wow! My last post was two months ago! What have I been doing meanwhile? Well, right now, besides attending my job I&#8217;m attending the PhD course on Physics and Computer Science at Universidad de La Laguna. My favorite subjects are, yes, you guess it, Parallel computing and Computer languages (Compilers, Language processors, etc&#8230;) I&#8217;m currently [...]]]></description>
			<content:encoded><![CDATA[<p>Wow! My last post was two months ago!<br />
What have I been doing meanwhile? Well, right now, besides attending my job <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I&#8217;m attending the PhD course on Physics and Computer Science at <a href="http://www.ull.es">Universidad de La Laguna</a>.</p>
<p>My favorite subjects are, yes, you guess it, Parallel computing and Computer languages (Compilers, Language processors, etc&#8230;)</p>
<p>I&#8217;m currently programming a ZX Spectrum BASIC cross compiler in my (little) spare time. It&#8217;s entirely written in python, and almost finished (hope to release V1.0 this month). It&#8217;s a three stage retargeable compiler. The backend outputs Z80 code, so it should be easily portable to other Z80 micros such as AMSTRAD or MSX. Porting to other architectures, or even to .NET or Java it&#8217;s also possible by changing the backend module could also be implemented.</p>
<p>Regarding to the language, It&#8217;s not the ZX Spectrum original BASIC but a dialect very close to <a href="http://www.freebasic.net/wiki">FreeBasic</a> which allows function definitions and more complex control-flow structures. The URL contains an almost complete specification, although not all of them has been implemented in the ZX for obvious reasons.</p>
<p>Some features:</p>
<ul>
<li>Function definitions with local variable scopes and parameters by reference or value</li>
<li>Multidimensional arrays of any data type</li>
<li>Strings passed by value or reference</li>
<li>Dynamic memory heap</li>
<li>Inline <b>ASM</b> code</li>
<li>Completely extensible with your own functions</li>
</ul>
<p>You can <a href="http://www.boriel.com/files/zxb">download</a> current alpha builds or visit the <a href="http://www.worldofspectrum.org/forums/showthread.php?t=20225">forum</a> for suggestions, comments, etc.</p>
<p><strong>Update: </strong>I&#8217;ve just finished it! Visit the <a href="http://www.boriel.com/the-zx-basic-compiler/">compiler page</a> for more information.</p>
<div id="tweetbutton121" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2008%2F05%2F05%2Fzx-spectrum-basic-compiler%2F%3Flang%3Den&amp;via=boriel&amp;text=A%20ZX%20Spectrum%20BASIC%20Compiler%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2008%2F05%2F05%2Fzx-spectrum-basic-compiler%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2008/05/05/zx-spectrum-basic-compiler/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>GCC Compiler &#8220;now&#8221; supports OpenMP</title>
		<link>http://www.boriel.com/2008/03/03/gcc-supports-openmp/?lang=en</link>
		<comments>http://www.boriel.com/2008/03/03/gcc-supports-openmp/?lang=en#comments</comments>
		<pubDate>Mon, 03 Mar 2008 19:26:15 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Parallel Computing]]></category>
		<category><![CDATA[Gcc]]></category>
		<category><![CDATA[GNU]]></category>
		<category><![CDATA[High Performance Computing]]></category>

		<guid isPermaLink="false">http://www.boriel.com/2008/03/03/gcc-supports-openmp/</guid>
		<description><![CDATA[Actually, this isn&#8217;t recent news, since GCC supports OpenMP since version 4.2.0 (released on May, 2007). The interesting thing is that it&#8217;s being considered really stable and mature now; on the other hand, a new 4.3.0. branch is about to come. So we&#8217;ll soon see OpenMP support in our favourite Linux distro (it seems Fedora [...]]]></description>
			<content:encoded><![CDATA[<p>Actually, this isn&#8217;t <a href="http://www.internetnews.com/dev-news/article.php/3678421">recent news</a>, since GCC supports <a href="http://www.openmp.org">OpenMP</a> since version <a href="http://gcc.gnu.org/gcc-4.2/changes.html">4.2.0</a> (released on May, 2007). The interesting thing is that it&#8217;s being considered really <b>stable</b> and mature now; on the other hand, a new <a href="http://gcc.gnu.org/gcc-4.3/changes.html">4.3.0.</a> branch is about to come. So we&#8217;ll soon see OpenMP support in our favourite Linux distro <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (it seems Fedora might already have included GCC 4.2.0 with OpenMP support already).</p>
<p>Previously, OpenMP support was done in GCC though the use of <a href="http://gcc.gnu.org/projects/gomp/">GOMP</a> (an OpenMP library), but it has been finally merged into the developing mainline of this <i>compiler collection</i> (yes, Fortran have it too). <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>The specification implemented is <a href="http://www.openmp.org/drupal/mp-documents/spec25.pdf">OpenMP 2.5</a> (there&#8217;s a 3.0 draft on the way), and it seems to be completely supported, as you might read at the <a href="http://gcc.gnu.org/onlinedocs/gcc-4.2.2/libgomp.pdf">GCC Open MP features</a> document.</p>
<div id="tweetbutton120" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2008%2F03%2F03%2Fgcc-supports-openmp%2F%3Flang%3Den&amp;via=boriel&amp;text=GCC%20Compiler%20%26%238220%3Bnow%26%238221%3B%20supports%20OpenMP%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2008%2F03%2F03%2Fgcc-supports-openmp%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2008/03/03/gcc-supports-openmp/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My own compiler written in Python</title>
		<link>http://www.boriel.com/2008/02/06/python-compiler-tool/?lang=en</link>
		<comments>http://www.boriel.com/2008/02/06/python-compiler-tool/?lang=en#comments</comments>
		<pubDate>Wed, 06 Feb 2008 12:05:32 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.boriel.com/2008/02/06/python-compiler-tool/</guid>
		<description><![CDATA[During these last months I&#8217;ve been developing, just for fun and in my *very* little spare time, my own compiler kit tool. More than a compiler, it&#8217;s a tool for building up compilers (there are others over there, like Lex &#038; Yacc, Flex &#038; Bison in its GNU flavour). Mine is called Bparser. I&#8217;ve put [...]]]></description>
			<content:encoded><![CDATA[<p>During these last months I&#8217;ve been developing, just for fun and in my <b>*very*</b> little spare time, my own compiler kit tool.<br />
More than a compiler, it&#8217;s a <b>tool for building up compilers</b> (there are others over there, like <a href="http://dinosaur.compilertools.net/">Lex &#038; Yacc</a>, Flex &#038; Bison in its GNU flavour). Mine is called <abbr title="Boriel Parser">Bparser</abbr>.</p>
<p>I&#8217;ve put it in my <a href="http://www.boriel.com/wiki/en/index.php/Compilers_and_Compiler_Tools">wiki</a>, just in case anybody want to give it a glance.</p>
<p>My tool is better than the LEX &#038; YACC couple in the sense it can parse LR(n) grammars whilst LEX/Yacc only parses <a href="http://en.wikipedia.org/wiki/LALR_parser">LALR</a> ones. It uses a Lookahead and mangle-likes estructures to take some decisions when choosing which grammar rule to use for reduction.</p>
<p>If it eventually cannot decide which rule should be used, it will start to <a href="http://en.wikipedia.org/wiki/Backtracking">backtrack</a> to find out the rule to apply.</p>
<p>This tool allows ambiguos grammars, so it is very similar to a <a href="http://en.wikipedia.org/wiki/GLR_parser">GLR</a> parser. BISON (GNU&#8217;s YACC) can also use the GLR algorithm, but it is less efficient and can take exponential space/time to parse some entries depending on the given grammar.</p>
<p><span id="more-119"></span></p>
<h2>The calculator example</h2>
<p>The calculator example is the <i>&#8220;Hello World&#8221;</i> of parsers generators. Below there is an calculator definition for mathematical expresions. Simply run the program and enter a mathematic expression like <i>1+3.2*5^2</i> and the program will output the result to the screen (with some explanation about rules used). The program will finish when you enter an empty input.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/python
# -*- coding: utf-8 -*-

</span><span class="hl-reserved">import </span><span class="hl-identifier">sys

</span><span class="hl-reserved">from </span><span class="hl-identifier">bparser </span><span class="hl-reserved">import </span><span class="hl-identifier">Token
</span><span class="hl-reserved">from </span><span class="hl-identifier">bparser </span><span class="hl-reserved">import </span><span class="hl-identifier">Symbol
</span><span class="hl-reserved">from </span><span class="hl-identifier">bparser </span><span class="hl-reserved">import </span><span class="hl-identifier">Bparser

</span><span class="hl-reserved">class </span><span class="hl-identifier">Calc</span><span class="hl-brackets">(</span><span class="hl-identifier">Bparser</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-reserved">def </span><span class="hl-identifier">minus_factor</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = -</span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_mul_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> * </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_div_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> / </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_plus_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> + </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_minus_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> - </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value	
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_pow_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> ** </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">print_result</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">Result is</span><span class="hl-quotes">'</span><span class="hl-default">, </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">lprp_action</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol

	</span><span class="hl-reserved">def </span><span class="hl-identifier">default_hook</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
	
		</span><span class="hl-reserved">print </span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">name</span><span class="hl-default">, </span><span class="hl-quotes">'</span><span class="hl-string">--&gt;</span><span class="hl-quotes">'</span><span class="hl-default">, </span><span class="hl-brackets">[</span><span class="hl-identifier">x</span><span class="hl-code">.</span><span class="hl-identifier">name </span><span class="hl-reserved">for </span><span class="hl-identifier">x </span><span class="hl-reserved">in </span><span class="hl-identifier">l</span><span class="hl-brackets">]
		</span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">ValueOf(%s) = </span><span class="hl-quotes">'</span><span class="hl-default"> % </span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">name</span><span class="hl-default">, </span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value 
	
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol

calc</span><span class="hl-default"> = </span><span class="hl-identifier">Calc</span><span class="hl-brackets">()

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">add_token</span><span class="hl-brackets">(</span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-string">[0-9]+(</span><span class="hl-special">\.</span><span class="hl-string">[0-9]+)?([eE][-+]?[0-9]+)?</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">NUMBER</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">add_tokens</span><span class="hl-brackets">(</span><span class="hl-code">{</span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\+</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">PLUS</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\-</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">MINUS</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\*</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">MUL</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">/</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">DIV</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">%</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">MOD</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\^</span><span class="hl-quotes">'</span><span class="hl-code">:</span><span class="hl-quotes">'</span><span class="hl-string">POW</span><span class="hl-quotes">'</span><span class="hl-code">}</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">add_tokens</span><span class="hl-brackets">(</span><span class="hl-code">{</span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\(</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">LP</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\)</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">RP</span><span class="hl-quotes">'</span><span class="hl-code">}</span><span class="hl-brackets">) </span><span class="hl-comment"># Escape PARENTHESIS in Regular Expressions
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">add_token</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">[ </span><span class="hl-special">\t\n</span><span class="hl-string">]+</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">SEPARATOR</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">Token</span><span class="hl-code">.</span><span class="hl-identifier">skip</span><span class="hl-brackets">))

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">%LEFT (PLUS, MINUS)</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">%LEFT (MUL, DIV)</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">%RIGHT (POW)</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Program --&gt; expr .print_result</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">expr --&gt; term</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term PLUS term .term_plus_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term MINUS term .term_minus_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term MUL term .term_mul_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term DIV term .term_minus_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term POW term .term_pow_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; value</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">value --&gt; LP expr RP .lprp_action</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">value --&gt; NUMBER</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">value --&gt; MINUS NUMBER .minus_factor</span><span class="hl-quotes">'</span><span class="hl-brackets">)


</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">'</span><span class="hl-string">__main__</span><span class="hl-quotes">'</span><span class="hl-default">:
	</span><span class="hl-reserved">while True</span><span class="hl-default">:
		</span><span class="hl-identifier">input</span><span class="hl-default"> = </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">stdin</span><span class="hl-default">.</span><span class="hl-identifier">readline</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">strip</span><span class="hl-brackets">()
		</span><span class="hl-reserved">if </span><span class="hl-identifier">input</span><span class="hl-default"> == </span><span class="hl-quotes">''</span><span class="hl-default">: </span><span class="hl-reserved">break
	
		</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">set_buffer</span><span class="hl-brackets">(</span><span class="hl-identifier">input</span><span class="hl-brackets">)
		</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">verbose</span><span class="hl-default"> = </span><span class="hl-reserved">False
		</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">start</span><span class="hl-brackets">()
		</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">storage</span><span class="hl-default">.</span><span class="hl-identifier">pow_level</span><span class="hl-default"> = </span><span class="hl-brackets">[]
	
		</span><span class="hl-reserved">if </span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">()</span><span class="hl-default">:
			</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">verbose</span><span class="hl-default"> = </span><span class="hl-reserved">True
			</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">mimic</span><span class="hl-brackets">()
		</span><span class="hl-reserved">else</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Syntax Error.</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>I&#8217;m using this tool in the <a href="http://www.speccy.org/zxbcompiler/">ZX Spectrum BASIC Compiler</a>, that will serve as a test-language for this tool.</p>
<div id="tweetbutton119" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2008%2F02%2F06%2Fpython-compiler-tool%2F%3Flang%3Den&amp;via=boriel&amp;text=My%20own%20compiler%20written%20in%20Python%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2008%2F02%2F06%2Fpython-compiler-tool%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2008/02/06/python-compiler-tool/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

