<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description></description><title>Predictably Meta</title><generator>Tumblr (3.0; @zaach)</generator><link>http://zaa.ch/</link><item><title>Algorithms That Govern Our Lives
Turntable.fm defies this trend,...</title><description>&lt;iframe src="http://video.nextconf.eu/v.ihtml?token=41a597e394e5356be2cf8799ed3990d0&amp;photo%5fid=1869141" width="400" height="225" frameborder="0" border="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;strong&gt;Algorithms That Govern Our Lives&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://turntable.fm"&gt;Turntable.fm&lt;/a&gt; defies this trend, and that’s why it’s so good. A minor victory against our algorithmic overlords.&lt;/p&gt;</description><link>http://zaa.ch/post/7288294870</link><guid>http://zaa.ch/post/7288294870</guid><pubDate>Tue, 05 Jul 2011 22:47:26 -0400</pubDate><category>algorithms</category><category>turntable.fm</category><category>last.fm</category><category>pandora</category></item><item><title>Deal with it.</title><description>&lt;img src="http://25.media.tumblr.com/tumblr_lk3b1thydB1qcgorvo1_500.gif"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a title="Dynamic languages are static languages" target="_blank" href="http://existentialtype.wordpress.com/2011/03/19/dynamic-languages-are-static-languages/"&gt;Deal with it.&lt;/a&gt;&lt;/p&gt;</description><link>http://zaa.ch/post/4858488537</link><guid>http://zaa.ch/post/4858488537</guid><pubDate>Sat, 23 Apr 2011 01:25:05 -0400</pubDate></item><item><title>npm install jison</title><description>&lt;p&gt;Tinkering with new projects should be easy. You should be able to click a &lt;a href="http://jison.org/try"&gt;link&lt;/a&gt; or invoke a command and BAM, there goes your Sunday afternoon. I&amp;#8217;m not sure how many people build parsers on the weekend, but hey, Jison is easy to install now anyway.&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;re using Node.js you should already have the excellent &lt;a href="http://github.com/isaacs/npm"&gt;&lt;code&gt;npm&lt;/code&gt;&lt;/a&gt; installed. If not, that&amp;#8217;s easy:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;curl -L &lt;a href="http://github.com/isaacs/npm/tarball/master"&gt;http://github.com/isaacs/npm/tarball/master&lt;/a&gt; | tar xz --strip 1
cd npm
sudo make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now install jison:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;sudo npm install jison
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Crazy easy. Now go read the docs for &lt;a href="http://zaach.github.com/jison/docs/"&gt;challenges&lt;/a&gt;.&lt;/p&gt;</description><link>http://zaa.ch/post/918946923</link><guid>http://zaa.ch/post/918946923</guid><pubDate>Mon, 14 Jun 2010 22:02:32 -0400</pubDate><category>jison</category><category>node.js</category><category>commonjs</category></item><item><title>Lambda Calculus Evaluator</title><description>&lt;p&gt;After writing &lt;a href="http://jison.org"&gt;Jison&lt;/a&gt;, I began more experimentation with other facets of programming language design, namely interpreters. I was first exposed to their workings two years ago in a programming languages course at my university, where we &lt;a href="http://github.com/zaach/sml-hw"&gt;wrote an interpreter&lt;/a&gt; for a minimal functional language using SML. As an experiment, I used the same technique to &lt;a href="http://github.com/zaach/sml-hw/blob/master/lambda/eval.sml"&gt;evaluate lambda calculus expressions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Well, that was fun and all, but since I&amp;#8217;m doing most of my language experimentation in JavaScript these days, I decided to port the lambda calculus evaluator to &lt;a href="http://github.com/zaach/lambdacalc"&gt;JavaScript&lt;/a&gt; (and put up a &lt;a href="http://zaach.github.com/lambdacalc/"&gt;web interface&lt;/a&gt;.) It uses call-by-value evaluation semantics, as did the SML original. I used Jison for parsing, naturally.&lt;/p&gt;

&lt;p&gt;Sure, it&amp;#8217;s Turing Complete, but I doubt you would want to (or could) code your next blogging engine using it.&lt;/p&gt;

&lt;p&gt;Anyway, &lt;a href="http://zaach.github.com/lambdacalc/"&gt;have fun&lt;/a&gt;!&lt;/p&gt;</description><link>http://zaa.ch/post/918947149</link><guid>http://zaa.ch/post/918947149</guid><pubDate>Sun, 25 Apr 2010 17:52:57 -0400</pubDate><category>programming-languages</category><category>javascript</category><category>jison</category><category>lambda-calculus</category></item><item><title>Replication Speed: CouchDB vs. MongoDB</title><description>&lt;p&gt;&lt;em&gt;I&amp;#8217;m posting this because I hadn&amp;#8217;t seen this comparison done before. Therefore, it might be useful to someone else. Maybe.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This was a rough test to see how long a slave would take to catch up with its master, which was sustaining about 500 document insertions per second with about 5 million documents already stored. The general verdict was that MongoDB replication is &lt;em&gt;much&lt;/em&gt; faster, at least for &lt;em&gt;this particular data model&lt;/em&gt; (i.e. logging data.) For CouchDB, inserting millions of small documents is probably not the most efficient way to do things.&lt;/p&gt;

&lt;p&gt;I set up Ubuntu 9.10 VMs with 2&amp;#160;GB of RAM each for the master and two slaves. The first slave was set to replicate with the master from the beginning, to see if it ever lags (it was negligible for both DBMSs.) The other slave was used to see how long replication took to catch up.&lt;/p&gt;

&lt;h2&gt;CouchDB (v0.11)&lt;/h2&gt;

&lt;p&gt;For CouchDB, after 5,015,630 documents had been inserted over the course of 2 hours and 48 minutes, I started continuous replication on the third node and waited for it to catch up.&lt;/p&gt;

&lt;p&gt;At 9,266,860 documents total, the third node had finally caught up with the master, after another &lt;strong&gt;2 hours and 47 minutes&lt;/strong&gt;, almost the same amount of time it had been offline.&lt;/p&gt;

&lt;p&gt;For shorter intervals, the rate of replication was better. For example, it took 16 minutes for an empty database to catch up to a master that had been running for 30 minutes sustaining 500/document insertions per second.&lt;/p&gt;

&lt;h2&gt;MongoDB (v1.4)&lt;/h2&gt;

&lt;p&gt;MongoDB took considerably less time, completing the sync of 5,015,639 documents in under &lt;strong&gt;7 minutes&lt;/strong&gt;. Roughly half of the time was spent creating the new databases on disk and filling them with zeros, and the other half applying document inserts.&lt;/p&gt;

&lt;p&gt;If shutdown improperly the &lt;code&gt;mongod&lt;/code&gt; node needs to be repaired (with &lt;code&gt;mongod --repair&lt;/code&gt;), which took around 11 minutes for 5 million records, ~4&amp;#160;GB of data.  There really isn&amp;#8217;t an improper way to shutdown CouchDB, so you can boot and start replicating immediately.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We&amp;#8217;ll probably use MongoDB for dumping log data. It&amp;#8217;s simple enough and the data is not exactly mission critical, so MongoDB&amp;#8217;s shortcomings (durability, master-master limitations) aren&amp;#8217;t enough of a deterrent.&lt;/p&gt;

&lt;p&gt;I really like CouchDB&amp;#8217;s replication system. It reminds me of git and allows for the same fully distributed model, but our cluster is not large or diverse enough to really take advantage of those features. I would have picked CouchDB if not for the dramatic difference in replication speed. But hey, no one DBMS is a hammer for all nails.&lt;/p&gt;

&lt;h2&gt;Extra Notes on Size&lt;/h2&gt;

&lt;p&gt;Documents were all clones of this JSON:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;{"play_time":"19:02:20","file_name":"8821_20100224-1100.xml","playlist":"none","event":"playlist","player_id":9861,"play_date":"2010-03-02"}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;CouchDB&lt;/h3&gt;

&lt;ul&gt;&lt;li&gt;Master: 5,015,630 docs, 5.0&amp;#160;GB&lt;/li&gt;
&lt;li&gt;Slave: 5,015,630 docs, 9.3&amp;#160;GB&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;CouchDB after sync completed (compacted)&lt;/h3&gt;

&lt;ul&gt;&lt;li&gt;Master: 9,266,860 docs, 7.4&amp;#160;GB (3.6&amp;#160;GB)&lt;/li&gt;
&lt;li&gt;Slave: 9,266,860 docs, 18.6&amp;#160;GB (3.6&amp;#160;GB)&lt;/li&gt;
&lt;li&gt;Slave2: 9,266,860 docs, 13.9&amp;#160;GB (3.6&amp;#160;GB)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;MongoDB was the same size on all nodes, or just under 4&amp;#160;GB each after 5 million+ documents (as you may know, MongoDB adds new database chunks up to 2&amp;#160;GB as needed.)&lt;/p&gt;</description><link>http://zaa.ch/post/918947197</link><guid>http://zaa.ch/post/918947197</guid><pubDate>Tue, 20 Apr 2010 16:03:39 -0400</pubDate><category>databases</category><category>nosql</category><category>mongodb</category><category>couchdb</category></item><item><title>Slides and code from "Build Your Own Programming Language with JavaScript"</title><description>&lt;p&gt;I gave a Track B presentation this year at JSConf titled &amp;#8220;Build Your Own Programming Language with JavaScript&amp;#8221;. The conference was incredible, but I&amp;#8217;ll post about that later, perhaps. I&amp;#8217;ve pushed the code and slides from the talk to &lt;a href="http://github.com/zaach/zii-jsconf2010-talk"&gt;github&lt;/a&gt; for people to play with.&lt;/p&gt;</description><link>http://zaa.ch/post/918947232</link><guid>http://zaa.ch/post/918947232</guid><pubDate>Tue, 20 Apr 2010 10:09:10 -0400</pubDate><category>javascript</category><category>programming</category><category>jison</category><category>jsconf</category></item><item><title>A Common, Universal Scripting Language</title><description>&lt;p&gt;&lt;a href="http://twitter.com/antirez/status/11318287304"&gt;Where is it&lt;/a&gt;? Hint: JavaScript. There&amp;#8217;s an interpreter on every consumer platform and it&amp;#8217;s already the most widely used scripting language in the world&lt;sup&gt;[citation needed]&lt;/sup&gt;.
The core language is also very simple, and beginner friendly by design. Heck, every programmer already knows about half of the language through dealing with JSON. Throw in functions and you can get by.&lt;/p&gt;

&lt;p&gt;It may not be the ideal scripting language, but it is the closest we have to a universal one.&lt;/p&gt;</description><link>http://zaa.ch/post/918947327</link><guid>http://zaa.ch/post/918947327</guid><pubDate>Tue, 30 Mar 2010 13:37:47 -0400</pubDate><category>programming</category><category>javascript</category></item><item><title>Localized JSON Lint</title><description>&lt;p&gt;I stumbled upon &lt;a href="http://jsonlint.com"&gt;jsonlint.com&lt;/a&gt; recently, a service that sends your JSON data to a server for validation and returns the results. I decided to create a &lt;a href="http://zaach.github.com/jsonlint/"&gt;pure JavaScript version&lt;/a&gt;, which makes it is easy to host your self. It also comes as a &lt;a href="http://github.com/zaach/jsonlint/tree/master"&gt;CommonJS module&lt;/a&gt; in case you want to include it in some other tool.&lt;/p&gt;</description><link>http://zaa.ch/post/918947377</link><guid>http://zaa.ch/post/918947377</guid><pubDate>Sat, 13 Mar 2010 18:10:33 -0500</pubDate><category>javascript</category><category>json</category><category>jsonlint</category><category>parser</category></item><item><title>CoffeeScript compiler now uses Jison</title><description>&lt;p&gt;The new &lt;a href="http://github.com/jashkenas/coffee-script"&gt;CoffeeScript&lt;/a&gt; self compiler uses Jison for generating the parser. The whole build script runs on Node.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://github.com/jashkenas"&gt;Jeremy&lt;/a&gt;, the creator of CoffeeScript, spurred me to make some quick performance improvements (and bug fixes) to Jison so that the CoffeeScript grammar could build in a timely fashion. And while there are still many improvements to be made, handling such a large and complex grammar is quite a feat!&lt;/p&gt;

&lt;p&gt;Jeremy also reported that parsing times are quick, though that&amp;#8217;s more of a testament to v8, I&amp;#8217;m sure.&lt;/p&gt;</description><link>http://zaa.ch/post/918947442</link><guid>http://zaa.ch/post/918947442</guid><pubDate>Wed, 24 Feb 2010 20:48:45 -0500</pubDate><category>jison</category><category>coffee-script</category><category>compiler</category></item><item><title>Jison, now with more Bison flavor.</title><description>&lt;p&gt;I pushed a &lt;a href="http://github.com/zaach/jison"&gt;new version&lt;/a&gt; of &lt;a href="http://zaa.ch/past/2009/12/28/jison_an_api_for_creating_parsers_in_javascript_/"&gt;Jison&lt;/a&gt; (0.1.5) the other day that adds some interesting features, namely the ability to use Bison-like grammar definitions and Flex-like lexer definitions.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.flickr.com/photos/field_museum_library/3404663689/"&gt;&lt;img src="http://farm4.static.flickr.com/3418/3404663689_068596e2c9.jpg" alt="Bison"/&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;Bison flavored grammars&lt;/h1&gt;

&lt;p&gt;The following is a valid Bison grammar:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;%token NUMBER

%%

E
    : E '+' T
    | T
    ;

T
    : NUMBER
    ;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now it is also a valid Jison grammar. You can generate a parser using it as you would with the JSON format:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;narwhal bin/jison examples/calculator.jison
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will generate just the parser, so you won&amp;#8217;t be able to parse anything with it just yet (we&amp;#8217;ll need a lexer too, which I detail later.) And as you can see, the file format ends with a &lt;code&gt;.jison&lt;/code&gt; extension to distinguish it, as it is not fully compatible with Bison.&lt;/p&gt;

&lt;p&gt;Jison will ignore &lt;code&gt;%token&lt;/code&gt; declarations, as seen in the above grammar. It only recognizes &lt;code&gt;%left&lt;/code&gt;, &lt;code&gt;%right&lt;/code&gt;, &lt;code&gt;%nonassoc&lt;/code&gt;, &lt;code&gt;%start&lt;/code&gt;, and &lt;code&gt;%prec&lt;/code&gt; declarations, which carry the same semantics as with Bison.&lt;/p&gt;

&lt;h2&gt;Semantic Actions&lt;/h2&gt;

&lt;p&gt;Semantic actions should look familiar:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;/* description: Parses end executes mathematical expressions. */

%left '+' '-'
%left '*' '/'
%left '^'
%left UMINUS

%%

S
    : e EOF
        {print($1); return $1;}
    ;

e
    : e '+' e
        {$$ = $1+$3;}
    | e '-' e
        {$$ = $1-$3;}
    | e '*' e
        {$$ = $1*$3;}
    | e '/' e
        {$$ = $1/$3;}
    | e '^' e
        {$$ = Math.pow($1, $3);}
    | '-' e
        {$$ = -$2;} %prec UMINUS
    | '(' e ')'
        {$$ = $2;}
    | NUMBER
        {$$ = Number(yytext);}
    | E
        {$$ = Math.E;}
    | PI
        {$$ = Math.PI;}
    ;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is valid for both Bison and Jison. The difference is that Jison does not attempt to match braces inside of actions like Bison does, so you&amp;#8217;ll have to wrap your action in a different set of braces if they contain a closing brace, e.g.:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;nonterminal
    : TOKEN
        {{$$ = '}';}}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this case we wrapped double braces around the action so it knows the single brace is not the end of the action.&lt;/p&gt;

&lt;p&gt;The nicest thing about this format over JSON is that porting Bison grammars is much simpler, though you&amp;#8217;ll still have to remove all of the C code and duplicate the logic in a separate JavaScript module. Jison also doesn&amp;#8217;t handle &lt;a href="http://www.gnu.org/software/bison/manual/bison.html#Value-Type"&gt;semantic value data types&lt;/a&gt; (not really necessary with a dynamically typed language like JavaScript,) &lt;a href="http://www.gnu.org/software/bison/manual/bison.html#Mid_002dRule-Actions"&gt;mid-rule actions&lt;/a&gt; and &lt;a href="http://www.gnu.org/software/bison/manual/bison.html#Error-Recovery"&gt;error recovery&lt;/a&gt;, &lt;em&gt;yet&lt;/em&gt;, among other things. Feel free to post an &lt;a href="http://github.com/zaach/jison/issues"&gt;issue&lt;/a&gt; requesting your favorite missing feature.&lt;/p&gt;

&lt;p&gt;A bit of an aside, but I found the process of bootstrapping this really interesting. You end up with a grammar that can &lt;a href="http://github.com/zaach/jison/blob/master/src/bnf.jison"&gt;parse itself&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Named Semantic Values&lt;/h2&gt;

&lt;p&gt;I decided to add some sugar for accessing semantic values within actions &lt;em&gt;by name&lt;/em&gt; instead of position. Normally, you&amp;#8217;d have to use the position of the corresponding nonterminal in the production, prefixed by a dollar sign &lt;code&gt;$&lt;/code&gt;, e.g.:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt; exp:    ...
         | '(' exp ')'
             { $$ = $2; }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, you can also access the value by using the name of the nonterminal instead of its position, e.g.:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt; exp:    ...
         | '(' exp ')'
             { $$ = $exp; }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If the rule is ambiguous (the nonterminal appears more than once,) append a number to the end of the nonterminal name to disambiguate the desired value:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt; exp:    ...
         | exp '+' exp
             { $$ = $exp1 + $exp2; }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Association by name leads to a looser coupling (and is easier to grok.)&lt;/p&gt;

&lt;h1&gt;Defining Lexers&lt;/h1&gt;

&lt;p&gt;Note that the new grammar format does not allow embedded lexer definitions. This is because there&amp;#8217;s a new, separate format for defining lexers, which also serves to keep things more modular.&lt;/p&gt;

&lt;p&gt;The format has a more restricted form of regular expression syntax, but no less expressive. The main difference is that strings that should match exactly must be wrapped in quotes. Here&amp;#8217;s an example of the format:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;%%
\s+                   {/* skip whitespace */}
[0-9]+("."[0-9]+)?\b  {return 'NUMBER';}
"*"                   {return '*';}
"/"                   {return '/';}
"-"                   {return '-';}
"+"                   {return '+';}
"^"                   {return '^';}
"("                   {return '(';}
")"                   {return ')';}
"PI"                  {return 'PI';}
"E"                   {return 'E';}
&amp;lt;&amp;lt;EOF&amp;gt;&amp;gt;               {return 'EOF';}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also use macros:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;D                     [0-9]

%%
\s+                   {/* skip whitespace */}
{D}+("."{D}+)?\b      {return 'NUMBER';}
"*"                   {return '*';}
"/"                   {return '/';}
"-"                   {return '-';}
"+"                   {return '+';}
"^"                   {return '^';}
"("                   {return '(';}
")"                   {return ')';}
"PI"                  {return 'PI';}
"E"                   {return 'E';}
&amp;lt;&amp;lt;EOF&amp;gt;&amp;gt;               {return 'EOF';}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The format ultimately compiles to JavaScript regular expressions, so use the appropriate escape sequences and such. The file extension for this format is &lt;code&gt;.jisonlex&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;JSON-to-Jison&lt;/h1&gt;

&lt;p&gt;The update includes a utility for converting grammars from the old JSON format to the new Jison format. Just run your JSON file through &lt;code&gt;json2jison&lt;/code&gt; to perform the conversion, e.g.:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;narwhal bin/json2jison examples/basic.json
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will create a file called &lt;code&gt;basic.jison&lt;/code&gt; in the current directory, which should be ready to run through Jison as-is.&lt;/p&gt;

&lt;p&gt;If the JSON grammar has an embedded lex specification, &lt;code&gt;json2jison&lt;/code&gt; will create a &lt;code&gt;.jisonlex&lt;/code&gt; file also, though it will likely need correction (the conversion is lossy, to say the least.) E.g, running:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;narwhal bin/json2jison examples/calculator.json
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;will create &lt;code&gt;calculator.jison&lt;/code&gt; and &lt;code&gt;calculator.jisonlex&lt;/code&gt;. Compare the generated &lt;code&gt;calculator.jisonlex&lt;/code&gt; to the correct one found in &lt;code&gt;examples/calculator.jisonlex&lt;/code&gt; to see the difference. Once the syntax of the lex specification is corrected, you can generate a complete parser like so:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;narwhal bin/jison calculator.jison calculator.jisonlex
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;passing both the grammar and lexer specs.&lt;/p&gt;

&lt;h1&gt;Node Compatible&lt;/h1&gt;

&lt;p&gt;I should note that Jison is now compatible with node.js when included using &lt;code&gt;require&lt;/code&gt; from a module. The examples from the &lt;a href="http://github.com/zaach/jison/blob/master/README.md"&gt;README&lt;/a&gt; should work on any CommonJS system that implements the &lt;a href="http://commonjs.org/specs/modules/1.0.html"&gt;Module specification&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Tim Caswell used Jison as part of his &lt;a href="http://github.com/creationix/coffeepot"&gt;CoffeeScript&lt;/a&gt; compiler that runs on Node.&lt;/p&gt;

&lt;h1&gt;In the future&lt;/h1&gt;

&lt;p&gt;There&amp;#8217;s tons more to implement from Bison, and the grammar should also become more compatible over time, in terms of syntax &lt;em&gt;and&lt;/em&gt; features that are supported by Jison.&lt;/p&gt;

&lt;p&gt;I also have many topics to cover on using Jison, which I hope to write about more.&lt;/p&gt;

&lt;p&gt;Until then, &lt;em&gt;stay parsey, my friends.&lt;/em&gt;&lt;/p&gt;</description><link>http://zaa.ch/post/918947520</link><guid>http://zaa.ch/post/918947520</guid><pubDate>Fri, 29 Jan 2010 15:16:50 -0500</pubDate><category>javascript</category><category>programming</category><category>jison</category><category>bison</category><category>language</category><category>parser</category><category>lexer</category></item><item><title>Orderly.js Web Version</title><description>&lt;p&gt;I&amp;#8217;ve commited a &lt;a href="http://github.com/zaach/orderly.js/tree/master/web"&gt;minified, dependency-free version* of orderly.js&lt;/a&gt; for use on the web. It introduces an &lt;code&gt;orderly&lt;/code&gt; object, with &lt;code&gt;parse&lt;/code&gt; and &lt;code&gt;compile&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;Usage is straight forward:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;&amp;lt;script src="orderly.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
    var orderlySource = "array {};";

    var jsonSchemaSource = orderly.compile(orderlySource);

    var jsonSchemaObject = orderly.parse(orderlySource);
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Edit: View the online demo &lt;a href="http://zaach.github.com/jison/demo/orderly/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;*Excpet for a &lt;a href="http://json.org/json2.js"&gt;JSON&lt;/a&gt; stringifier, to account for browsers that don&amp;#8217;t include native JSON yet.&lt;/p&gt;</description><link>http://zaa.ch/post/918975719</link><guid>http://zaa.ch/post/918975719</guid><pubDate>Thu, 31 Dec 2009 13:23:59 -0500</pubDate><category>javascript</category><category>json</category><category>jsonschema</category><category>orderly</category></item><item><title>Jison: An API for creating parsers in JavaScript </title><description>&lt;h2&gt;Bottom-up parsing&lt;/h2&gt;

&lt;p&gt;When you hear of someone writing a parser, it is usually of the top-down, recursive descent variety. Visualizing the source as an abstract syntax tree (AST), this means the parser tries to match the root, and by doing so, may recursively try to match branches and stems of the AST as it works its way &lt;em&gt;down&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bottom-up&lt;/strong&gt; parsing is different:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Bottom-up parsing (also known as
shift-reduce parsing) is a strategy
for analyzing unknown data
relationships that attempts to
identify the most fundamental units
first, and then to infer higher-order
structures from them. It attempts to
build trees upward toward the start
symbol. It occurs in the analysis of
both natural languages and computer
languages. - &lt;a href="http://en.wikipedia.org/wiki/Bottom-up_parsing"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Because most bottom-up parsers require a parse table (a complex matrix of instructions for the parser PDA) they are usually not handwritten. Instead, a parser generator such as &lt;a href="http://dinosaur.compilertools.net/#yacc"&gt;Yacc&lt;/a&gt;, &lt;a href="http://dinosaur.compilertools.net/#bison"&gt;Bison&lt;/a&gt;, &lt;a href="http://plus.kaist.ac.kr/~shoh/ocaml/ocamllex-ocamlyacc/ocamlyacc-tutorial/"&gt;ocamlyacc&lt;/a&gt;, etc are used to generate the parser in the target language. For Bison and Yacc, the language is C, for ocamlyacc, the language is, well, ocaml.&lt;/p&gt;

&lt;h1&gt;Jison&lt;/h1&gt;

&lt;p&gt;&lt;a href="http://github.com/zaach/jison"&gt;Jison&lt;/a&gt; generates bottom-up parsers in JavaScript. Its API is similar to Bison&amp;#8217;s, hence the name. It supports many of Bison&amp;#8217;s major features, plus some of its own. If you are new to parser generators such as Bison, and Context-free Grammars in general, a &lt;a href="http://dinosaur.compilertools.net/bison/bison_4.html"&gt;good introduction&lt;/a&gt; is found in the Bison manual. If you already know Bison, Jison should be easy to pickup.&lt;/p&gt;

&lt;p&gt;A brief warning before proceeding: &lt;strong&gt;the API is ridiculously unstable&lt;/strong&gt; right now. The goal is to mirror Bison where it makes sense, but we&amp;#8217;re not even there yet. Also, optimization has not been a main focus as of yet.&lt;/p&gt;

&lt;p&gt;Briefly, Jison takes a JSON encoded grammar specification and outputs a JavaScript file capable of parsing the language described by that grammar specification. You can then use the generated script to parse inputs and accept, reject, or perform actions based on the input.&lt;/p&gt;

&lt;h2&gt;Installation&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite&lt;/strong&gt;: To run Jison from the command line, you&amp;#8217;ll need to have &lt;a href="http://github.com/280north/narwhal"&gt;Narwhal&lt;/a&gt; installed and available from your &lt;code&gt;PATH&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Clone the github repository:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;git clone git://github.com/zaach/jison.git
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Using Jison from the command line&lt;/h2&gt;

&lt;p&gt;Now you&amp;#8217;re ready to generate some parsers:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;cd jison
narwhal bin/jison examples/calculator.json
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will generate a &lt;code&gt;calculator.js&lt;/code&gt; file in your current working directory. This file can be used to parse an input file, like so:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;echo "2^32 / 1024" &amp;gt; testcalc
narwhal calculator.js testcalc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will print out &lt;code&gt;4194304&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Using Jison from a CommonJS module&lt;/h2&gt;

&lt;p&gt;You can generate parsers programatically from JavaScript as well. Assuming Jison is in your commonjs environment&amp;#8217;s load path:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;// mygenerator.js
var Parser = require("jison").Parser;

var grammar = {
    "lex": {
        "rules": [
           ["\\s+", "/* skip whitespace */"],
           ["[a-f0-9]+", "return 'HEX';"]
        ]
    },

    "bnf": {
        "hex_strings" :[ "hex_strings HEX",
                         "HEX" ]
    }
};

var parser = new Parser(grammar);

// generate source, ready to be written to disk
var parserSource = parser.generate();

// you can also use the parser directly from memory

// returns true
parser.parse("adfe34bc e82a");

// throws lexical error
parser.parse("adfe34bc zxg");
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Using the generated parser&lt;/h2&gt;

&lt;p&gt;So, you have generated your parser through the command line or JavaScript API and have saved it to disk. Now it can be put to use.&lt;/p&gt;

&lt;p&gt;As demonstrated before, the parser can be used from the command line:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;narwhal calculator.js testcalc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Though, more ideally, the parser will be a dependency of another module. You can require it from another module like so:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;// mymodule.js
var parser = require("./calculator").parser;

function exec (input) {
    return parser.parse(input);
}

var twenty = exec("4 * 5");
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or, more succinctly:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;// mymodule.js
function exec (input) {
    return require("./calculator").parse(input);
}

var twenty = exec("4 * 5");
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Specifying a language&lt;/h2&gt;

&lt;p&gt;The process of parsing a language involves two phases: &lt;strong&gt;lexical analysis&lt;/strong&gt; (tokenizing) and &lt;strong&gt;parsing&lt;/strong&gt;, which the Lex/Yacc and Flex/Bison combinations are famous for. Both lexing and parsing information can be included in the Jison grammar specification for a language.&lt;/p&gt;

&lt;p&gt;For example, here is &lt;code&gt;calculator.json&lt;/code&gt;:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;{
    "comment": "Parses end executes mathematical expressions.",

    "lex": {
        "rules": [
           ["\\s+",                    "/* skip whitespace */"],
           ["[0-9]+(?:\\.[0-9]+)?\\b", "return 'NUMBER';"],
           ["\\*",                     "return '*';"],
           ["\\/",                     "return '/';"],
           ["-",                       "return '-';"],
           ["\\+",                     "return '+';"],
           ["\\^",                     "return '^';"],
           ["\\(",                     "return '(';"],
           ["\\)",                     "return ')';"],
           ["PI\\b",                   "return 'PI';"],
           ["E\\b",                    "return 'E';"],
           ["$",                       "return 'EOF';"]
        ]
    },

    "operators": [
        ["left", "+", "-"],
        ["left", "*", "/"],
        ["left", "^"],
        ["left", "UMINUS"]
    ],

    "bnf": {
        "S" :[[ "e EOF",   "print($1); return $1;"  ]],

        "e" :[[ "e + e",   "$$ = $1+$3;" ],
              [ "e - e",   "$$ = $1-$3;" ],
              [ "e * e",   "$$ = $1*$3;" ],
              [ "e / e",   "$$ = $1/$3;" ],
              [ "e ^ e",   "$$ = Math.pow($1, $3);" ],
              [ "- e",     "$$ = -$2;", {"prec": "UMINUS"} ],
              [ "( e )",   "$$ = $2;" ],
              [ "NUMBER",  "$$ = Number(yytext);" ],
              [ "E",       "$$ = Math.E;" ],
              [ "PI",      "$$ = Math.PI;" ]]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is all that is needed for Jison to generate a parser capable of recognizing the language and executing the semantic actions.&lt;/p&gt;

&lt;p&gt;More examples can be found in the examples/ directory.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;JSON isn&amp;#8217;t so bad, but a more author friendly format is in the works. It will be similar to Bison&amp;#8217;s .y files.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Sharing scope&lt;/h2&gt;

&lt;p&gt;In Bison, code is expected to be lexically defined within the scope of the semantic actions. E.g., chunks of code may be included in the generated parser source, which are available from semantic actions.&lt;/p&gt;

&lt;p&gt;Jison is more modular. Instead of pulling code into the generated module, the generated module is expected to be required and used by other modules. This means that if you want to expose functionality to the semantic actions, you can&amp;#8217;t rely on it being available through lexical scoping. Instead, the parser has a &lt;code&gt;yy&lt;/code&gt; property which is exposed to actions as the &lt;code&gt;yy&lt;/code&gt; free variable. Any functionality attached to this property is available in both lexical and semantic actions through the &lt;code&gt;yy&lt;/code&gt; free variable.&lt;/p&gt;

&lt;p&gt;An example from orderly.js:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;var parser = require("./orderly/parse").parser;

// set parser's shared scope
parser.yy = require("./orderly/scope");

// returns the JSON object
var parse = exports.parse = function (input) {
    return parser.parse(input);
};
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;scope&lt;/code&gt; module contains logic for building data structures, which is used within the semantic actions.&lt;/p&gt;

&lt;h2&gt;Lexical Analysis&lt;/h2&gt;

&lt;p&gt;Jison includes a rather rudimentary lexer, though &lt;strong&gt;any module that supports the basic lexer API could be used&lt;/strong&gt; in its place. Jison&amp;#8217;s lexer uses the &lt;code&gt;lex&lt;/code&gt; key of the JSON grammar spec, where the rules for matching a token are defined along with the action to execute on a match. Usually, the action will return the token which is used by the Jison parser. A custom lexer could be used instead with it&amp;#8217;s own methods of tokenizing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;TODO: More on this.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Parsing algorithms&lt;/h2&gt;

&lt;p&gt;Like Bison, Jison can recognize languages described by LALR(1) grammars, though it also has modes for LR(0), SLR(1), and LR(1). It also has a special mode for generating LL(1) parse tables (requested by my professor,) and could be extended to generate a recursive descent parser for LL(k) languages in the future. But, for now, Jison is geared toward bottom-up parsing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;*LR(1) mode is currently not practical for use with anything other than toy grammars, but that is entirely a consequence of the algorithm used, and may change in the future.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Real world example&lt;/h2&gt;

&lt;p&gt;I wrote a parser for &lt;a href="http://zaa.ch/past/2009/12/27/orderlyjs_orderly_to_jsonschema_compiler/"&gt;Orderly&lt;/a&gt; using Jison. The benefits I found were:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;If modeled after the normative language grammar, it is guaranteed to recognize the correct language.&lt;/li&gt;
&lt;li&gt;Adding new syntax is straight forward.&lt;/li&gt;
&lt;li&gt;It was much faster to develop than if I were to attempt implementing a (top-down) parser from scratch. But for others not used to grammar specifications, this might not be the case.&lt;/li&gt;
&lt;li&gt;Improvements to Jison mean you only have to regenerate your parsing module to see the benefits.&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Pie-in-the-sky ideas for the future&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;Practical, minimal LR(1) parsing&lt;/li&gt;
&lt;li&gt;GLR parsing, which can recognize ambiguous grammars and natural languages&lt;/li&gt;
&lt;/ul&gt;</description><link>http://zaa.ch/post/918975802</link><guid>http://zaa.ch/post/918975802</guid><pubDate>Mon, 28 Dec 2009 14:39:37 -0500</pubDate><category>javascript</category><category>programming</category><category>jison</category><category>bison</category><category>language</category><category>parser</category><category>lexer</category></item><item><title>Orderly.js: Orderly to JSONSchema Compiler</title><description>&lt;blockquote&gt;&lt;p&gt;&lt;a href="http://orderly-json.org/"&gt;Orderly&lt;/a&gt; is a textual format for describing JSON. Orderly can be compiled into JSONSchema. It is designed to be easy to read and write.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;I decided to give &lt;a href="http://github.com/zaach/jison"&gt;Jison&lt;/a&gt; a real-world spin by creating an Orderly to JSONSchema compiler for JavaScript. After about three days, interspersed with lots of eating and family vists, I have a working implementation, which I call &lt;a href="http://github.com/zaach/orderly.js"&gt;Orderly.js&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;var orderly = require("orderly");

var orderlySource = "array {};";

var jsonSchemaSource = orderly.compile(orderlySource);

var jsonSchemaObject = orderly.parse(orderlySource);

print(JSON.stringify(jsonSchemaObject));
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;An API for building parsers&lt;/h2&gt;

&lt;p&gt;I must say, &lt;a href="http://github.com/zaach/jison"&gt;Jison&lt;/a&gt; made this really simple. I don&amp;#8217;t consider myself a speedy coder in the slightest, probably the opposite, but I was able to crank this out in a (holiday) weekend. Jison takes care of the hard parts of parsing, all you need is a working grammar* and the appropriate hooks. I&amp;#8217;ll be writing more on Jison shortly.&lt;/p&gt;

&lt;p&gt;*This can be non-trivial, though if the language you are parsing already specifies a grammar, as was the case with Orderly, you&amp;#8217;re set!&lt;/p&gt;</description><link>http://zaa.ch/post/918947731</link><guid>http://zaa.ch/post/918947731</guid><pubDate>Sun, 27 Dec 2009 16:19:06 -0500</pubDate><category>javascript</category><category>commonjs</category><category>jison</category><category>orderly</category><category>jsonschema</category><category>json</category><category>compiler</category><category>parser</category></item><item><title>Stay parsey, my friends.</title><description>&lt;p&gt;I hinted on twitter that I was implementing some kind of &lt;a href="http://twitter.com/zii/status/4669062184"&gt;compiler&lt;/a&gt; in JavaScript. It was a project I began to help study for a compilers course I was taking, which also gave me an opportunity to earn extra credit. Though, really, I just thought it was a friggen cool thing to implement. I ended up focusing on the compiler front-end to create a parser generator, which I call &lt;a href="http://github.com/zaach/jison"&gt;Jison&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Anyway, the semester ended, I received the extra credit, and &lt;a href="http://github.com/zaach/jison"&gt;pushed the source to github&lt;/a&gt;. I&amp;#8217;m still working on the initial documentation to include in a more formal announcement this week.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://zaach.github.com/jison/demo/"&gt;demo page&lt;/a&gt; that was built to showcase some of the features to my professor is also available. It turned out to be a very useful tool for this particular niche in its own right. The page can help you visually debug CFGs, so it is helpful regardless of the parsing tool the grammar is intended for, or would be helpful for simply learning about CFGs and parsing algorithms.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ll post a follow up with documentation and more examples soon. For now, check out the &lt;a href="http://github.com/zaach/jison/blob/master/examples/json.js"&gt;JSON parser&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Stay parsey, my friends.&lt;/p&gt;</description><link>http://zaa.ch/post/918975892</link><guid>http://zaa.ch/post/918975892</guid><pubDate>Wed, 23 Dec 2009 10:44:13 -0500</pubDate><category>javascript</category><category>commonjs</category><category>parser</category><category>lexer</category><category>compilers</category><category>jison</category></item><item><title>Random Notes on Computability</title><description>&lt;p&gt;&lt;em&gt;I felt like venting a bit. Let&amp;#8217;s take a brief stroll through computability land.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;The difference is computability.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Computability_theory_%28computation%29"&gt;Computability&lt;/a&gt; is important to a programmer. It&amp;#8217;s fundamental to the act, even. Let&amp;#8217;s just say it&amp;#8217;s basically everything. Why waste cycles trying to compute the uncomputable! Or better yet, why introduce bugs trying to compute a problem using an insufficient computation model!&lt;/p&gt;

&lt;h1&gt;Computation models&lt;/h1&gt;

&lt;p&gt;You already know about these, even if you don&amp;#8217;t realize it. There are three we encounter frequently:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;DFAs&lt;/li&gt;
&lt;li&gt;PDAs&lt;/li&gt;
&lt;li&gt;Turing Machines&lt;/li&gt;
&lt;/ol&gt;&lt;h2&gt;Deterministic Finite Automata&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Automata_theory"&gt;DFAs&lt;/a&gt; can be specified using regular expressions. Yes, a regular expression is just a convenient DSL for specifying a state machine. The regex engine in your favorite programming language adds on more power than a vanilla regular expression, but there are still some fundamental limitations to consider.&lt;/p&gt;

&lt;p&gt;For instance, whenever you have to count, &lt;em&gt;dynamically&lt;/em&gt;, you&amp;#8217;ll need something more powerful. You&amp;#8217;ll need some memory to go along with that state.&lt;/p&gt;

&lt;p&gt;Say, you want to match all strings with the same number of leading 0s as trailing 1s. Informally specified as:&lt;/p&gt;

&lt;p&gt;0&lt;sup&gt;n&lt;/sup&gt;1&lt;sup&gt;n&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Remember, &lt;code&gt;n&lt;/code&gt; is dynamic, it depends on the string. Now, think of a simple regular expression to match that. No, DFAs are not powerful enough to recognize this pattern.&lt;/p&gt;

&lt;p&gt;It also &lt;a href="http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454"&gt;can&amp;#8217;t recognize HTML or XML&lt;/a&gt;, for this same reason. You can get away with a limited subset of HTML or XML, but not the full language.&lt;/p&gt;

&lt;p&gt;For that, you need at least a PDA.&lt;/p&gt;

&lt;h2&gt;Push-Down Automata&lt;/h2&gt;

&lt;p&gt;A &lt;a href="http://en.wikipedia.org/wiki/Pushdown_automaton"&gt;PDA&lt;/a&gt; is like a DFA, but it can track where it&amp;#8217;s been using a stack. The stack, along with the string input, is used to determine the next transition. A stack machine!&lt;/p&gt;

&lt;p&gt;PDAs can be specified using a Context-free Grammer.&lt;/p&gt;

&lt;p&gt;Okay, maybe you don&amp;#8217;t encounter these frequently. That&amp;#8217;s because a more powerful model is usually available.&lt;/p&gt;

&lt;h2&gt;Turing Machines&lt;/h2&gt;

&lt;p&gt;If you&amp;#8217;re using a programming language, one that is &lt;a href="http://en.wikipedia.org/wiki/Turing_completeness"&gt;Turing complete&lt;/a&gt;, well, you&amp;#8217;re building &lt;a href="http://en.wikipedia.org/wiki/Turing_machine"&gt;Turing machines&lt;/a&gt;. A Turing Machine is an algorithm. A program is just a big (or little) algorithm. This is the most sophisticated level of computation you&amp;#8217;ll encounter.&lt;/p&gt;

&lt;p&gt;Turing machines can be specified using, well, Turing-complete programming languages.&lt;/p&gt;

&lt;h2&gt;Compilers&lt;/h2&gt;

&lt;p&gt;The stages of a compiler require progressively higher levels of computation:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Lexer: DFA&lt;/li&gt;
&lt;li&gt;Parser: PDA&lt;/li&gt;
&lt;li&gt;Type Checker,
Code Gen: Turing Machine&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Of course, you&amp;#8217;d only actually need a TM to implement all stages, but as good software engineering principles hold, less is more.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I&amp;#8217;ll work  on writing more coherent blog posts in the future. Not saying that&amp;#8217;s my new years resolution though.&lt;/em&gt;&lt;/p&gt;</description><link>http://zaa.ch/post/918947824</link><guid>http://zaa.ch/post/918947824</guid><pubDate>Mon, 23 Nov 2009 21:13:36 -0500</pubDate><category>compsci</category><category>programming</category><category>automata</category><category>thinkingoutloud</category></item><item><title>Momentum and The Web</title><description>&lt;p&gt;Recent events have my mind spinning (pun semi intended,) so excuse the perspective-rather-than-directive here.&lt;/p&gt;

&lt;p&gt;Ahem.&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s nothing stopping the web now. Not to say that there was doubt before, but man, there&amp;#8217;s &lt;em&gt;nothing&lt;/em&gt; stopping it now. This is &lt;em&gt;the fastest&lt;/em&gt; I&amp;#8217;ve ever seen it move (granted, my observing eye awoke to a stagnant age where the blue E ruled all.) The momentum of the Web has become the momentum of the Software industry. It&amp;#8217;s &lt;em&gt;really&lt;/em&gt; exciting from where I&amp;#8217;m sitting.&lt;/p&gt;

&lt;p&gt;RIAs are a nice gimmick. Flash was always annoying. Silverlight almost elicits rage (the tech is nice, but, urghh, why???) Now the days of every proprietary platform having it&amp;#8217;s own dedicated app store &lt;a href="http://joehewitt.com/post/on-middle-men/"&gt;seem&lt;/a&gt; &lt;a href="http://paulgraham.com/apple.html"&gt;numbered&lt;/a&gt;. The web already has the largest catalog of applications of any platform today, and it&amp;#8217;s free and open for anyone to participate in.&lt;/p&gt;

&lt;p&gt;Now to organize it.&lt;/p&gt;

&lt;h2&gt;An App Store for the Open Web&amp;#160;?&lt;/h2&gt;

&lt;p&gt;Search engines are great for finding things you already know you want, but there are tons of useful web applications out there that you&amp;#8217;ll never see because you never knew they were exactly what you needed. And it&amp;#8217;s a shame, because both parties are missing out: the creators and the consumers. The need for a better catalog is &lt;a href="http://andrewwooldridge.com/blog/2009/11/16/open-letter-to-joe-hewitt-become-a-gardener/"&gt;obvious&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another interesting sub plot is the proposed &lt;a href="http://factoryjoe.com/blog/2009/11/16/the-death-of-the-url/"&gt;Death of the URL&lt;/a&gt;. This would indeed lessen the freedom of the web, and its appeal to many. So, an app store for the open web that abstracts away the URL might not be so great&amp;#8230;&lt;/p&gt;

&lt;p&gt;But, well, URLs are still vastly useful on the modern web, right? People exchange them all the time over Twitter, Digg, Reddit, ect. There&amp;#8217;s already a tremendous amount of data about web apps based on the links that are traded. Let&amp;#8217;s build an app store with that in mind.&lt;/p&gt;

&lt;p&gt;The way things are going, you are either with the web, or you are being deprecated by it. Choose wisely!&lt;/p&gt;</description><link>http://zaa.ch/post/918976002</link><guid>http://zaa.ch/post/918976002</guid><pubDate>Thu, 19 Nov 2009 21:58:00 -0500</pubDate><category>web</category></item><item><title>Number.prototype</title><description>&lt;p&gt;Monkey-patching the &lt;code&gt;Number&lt;/code&gt; global in JavaScript seems innocuous, and usually, that would be correct. I mean, who actually uses methods on &lt;code&gt;Number&lt;/code&gt; instances, right? Your only worry is that someone else had the same idea in a &lt;a href="http://github.com/sstephenson/prototype/blob/master/src/lang/number.js#L69"&gt;another library&lt;/a&gt;, with different semantics. Other than that, you&amp;#8217;re fine. Anyway, this is my favorite &lt;code&gt;Number&lt;/code&gt; trick:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;Number.prototype.times = function(funct){
  if(typeof funct === 'function') {
    for(var i = 0;i &amp;lt; Math.floor(this);i++) {
      funct(i);
    }
  }
  return this;
}

(20).times(function(){
    print("You're a jerk.");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Reads better than a &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Bonus.&lt;/em&gt; Repeat a string 20 times:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;new Array(21).join("I know.");
&lt;/code&gt;&lt;/pre&gt;</description><link>http://zaa.ch/post/918976168</link><guid>http://zaa.ch/post/918976168</guid><pubDate>Fri, 13 Nov 2009 00:59:21 -0500</pubDate><category>javascript</category><category>monkeypatch</category><category>prototype</category></item><item><title>Flip Text Jetpack</title><description>&lt;p&gt;I was surprised when I didn&amp;#8217;t find a Jetpack that already did this. Who doesn&amp;#8217;t love the flipped-text Unicode trick? ˙op ǝɹns I &lt;a href="http://jetpackgallery.mozillalabs.com/jetpacks/79"&gt;Now you can flip text at will&lt;/a&gt;.&lt;/p&gt;</description><link>http://zaa.ch/post/918976223</link><guid>http://zaa.ch/post/918976223</guid><pubDate>Wed, 11 Nov 2009 20:22:05 -0500</pubDate><category>jetpack</category><category>firefox</category><category>javascript</category><category>mozilla</category></item><item><title>Another Way to Run Narwhal</title><description>&lt;p&gt;What&amp;#8217;s Narwhal? Ahem:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Narwhal is a cross-platform,
multi-interpreter, general purpose
JavaScript platform. It aims to
provide a solid foundation for
building JavaScript applications,
primarily outside the web browser.
Narwhal includes a package manager,
module system, and standard library
for multiple JavaScript interpreters.
- &lt;a href="http://narwhaljs.org/"&gt;narwhaljs.org&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;It basically enables you to do things in JavaScript you might do in other scripting languages, like Python or Ruby. It&amp;#8217;s one implementation of the &lt;a href="http://wiki.commonjs.org/wiki/CommonJS"&gt;CommonJS&lt;/a&gt; specification. If you&amp;#8217;re in to JavaScript, you should get acquainted with it.&lt;/p&gt;

&lt;p&gt;The default engine uses Rhino, but there&amp;#8217;s also a XULRunner engine, allowing you to use Narwhal from within Firefox extensions and other XUL applications. Really exciting stuff!&lt;/p&gt;

&lt;p&gt;I wanted a quick way test Narwhal/XPCOM modules, so I made an &lt;a href="http://github.com/zaach/narwhal-xpcshell"&gt;engine&lt;/a&gt; using &lt;a href="https://developer.mozilla.org/en/xpcshell"&gt;xpcshell&lt;/a&gt; to do just that. It shares most of its libs with the xulrunner engine.&lt;/p&gt;

&lt;p&gt;To set it up:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;git clone git://github.com/tlrobinson/narwhal.git narwhal
cd narwhal/engines
git clone git://github.com/Gozala/narwhal-xulrunner.git xulrunner
git clone git://github.com/zaach/narwhal-xpcshell.git xpcshell
cd xpcshell
bin/install
cd ../..
NARWHAL_PLATFORM=xpcshell bin/narwhal
js&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It should work on Linux and OS X. &lt;em&gt;Windows support is certainly possible. I&amp;#8217;ll have a hack at it one day.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can learn more about what can be done with &lt;a href="http://narwhaljs.org/"&gt;Narwhal&lt;/a&gt; on their site.&lt;/p&gt;</description><link>http://zaa.ch/post/918948090</link><guid>http://zaa.ch/post/918948090</guid><pubDate>Wed, 07 Oct 2009 14:49:10 -0400</pubDate><category>javascript</category><category>narwhal</category><category>mozilla</category><category>xulrunner</category><category>xpcshell</category><category>commonjs</category></item><item><title>Cross-site HTTP Request for jsonip</title><description>&lt;p&gt;I&amp;#8217;ve added &lt;a href="https://developer.mozilla.org/En/HTTP_Access_Control"&gt;cross-site HTTP request&lt;/a&gt; support for jsonip, a micro-service I introduced &lt;a href="http://zaa.ch/past/2008/9/22/jsonip_retrieve_the_clients_ip_address_via_jsonp/"&gt;previously&lt;/a&gt; that retrieves a client&amp;#8217;s IP address. Now, with &lt;a href="https://developer.mozilla.org/En/HTTP_Access_Control"&gt;HTTP access controls&lt;/a&gt; and a browser that supports them (such as Firefox 3.5,) any site can use a regular &lt;code&gt;XmlHTTPRequest&lt;/code&gt; to utilize the service instead of JSONP. This might not be terribly useful &lt;em&gt;today&lt;/em&gt;, as most browsers don&amp;#8217;t support HTTP access controls, but the arrival of Firefox 3.5 beacons the future of possibilities.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s a new cross-site HTTP request usage example:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
  var req = new XMLHttpRequest();  
  req.open('GET', 'http://jsonip.appspot.com', true);  
  req.onreadystatechange = function (e) {  
    if (req.readyState === 4) {  
      if(req.status === 200) { 
        var ip = JSON.parse(req.responseText); 
        alert(ip.address);
      } else {  
        alert("Error loading page\n");  
      }
    }  
  };  
  req.send(null); 
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You&amp;#8217;ll notice it looks just like a regular &lt;code&gt;XmlHTTPRequest&lt;/code&gt;, except the domain does not have to be your own. My service at &lt;code&gt;jsonip.appspot.com&lt;/code&gt; is using special headers that allow this to work instead of greeting you with the usual permission errors.&lt;/p&gt;

&lt;p&gt;The source code of the python script I use for the service is below. You&amp;#8217;ll notice where the special headers are added:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;import os
import cgi

form = cgi.FieldStorage()
callback = form.getvalue('callback','')

address = cgi.escape(os.environ["REMOTE_ADDR"])

json = '{"ip": "'+address+'", "address":"'+address+'"}'

#Allow cross domain XHR
print 'Access-Control-Allow-Origin: *'
print 'Access-Control-Allow-Methods: GET'

if callback != '':
  print 'Content-Type: application/javascript'
  result = callback+'('+json+');'
else:
  print 'Content-Type: application/json'
  result = json

print ''
print result
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The magic happens here, with these printed headers:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;print 'Access-Control-Allow-Origin: *'
print 'Access-Control-Allow-Methods: GET'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first header means any domain can use this service cross-domain. Alternatively, you could have a comma seperated list of domains instead of a &lt;code&gt;*&lt;/code&gt; to allow requests only from those domains. I want my service open to all sites, so I use a &lt;code&gt;*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second header is self-explanatory (as if the first wasn&amp;#8217;t:) there&amp;#8217;s no need to support any other HTTP methods for this simple service, so I limit them to &lt;code&gt;GET&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are more access control headers available, so do make sure to read the &lt;a href="https://developer.mozilla.org/En/HTTP_Access_Control"&gt;article&lt;/a&gt; before trying these out. Some configurations could be dangerous, depending on your service.&lt;/p&gt;

&lt;h2&gt;Other jsonip Changes&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;JSON output uses double quotes for property names, as per the JSON specification&lt;/li&gt;
&lt;li&gt;Added &lt;code&gt;address&lt;/code&gt; property to use instead of the old &lt;code&gt;ip&lt;/code&gt; property to retrieve the IP address. I&amp;#8217;ve left the &lt;code&gt;ip&lt;/code&gt; property for backwards compatibility.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The old, JSONP method of use still works as expected:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
// callback function
function getip(jsonip){
  alert(jsonip.address) // alerts the ip address
}
&amp;lt;/script&amp;gt;
&amp;lt;script type="text/javascript" src="http://jsonip.appspot.com/?callback=getip"&amp;gt; &amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;</description><link>http://zaa.ch/post/918948132</link><guid>http://zaa.ch/post/918948132</guid><pubDate>Fri, 03 Jul 2009 17:17:22 -0400</pubDate><category>json</category><category>javascript</category><category>html5</category><category>jsonp</category><category>ajax</category></item></channel></rss>

