Parsing Javascript Parser

I've been working hard on RKelly lately. RKelly is a ruby implementation of Kelly. Kelly is a fictional project that I made up so that I could name my project RKelly. Anyway, RKelly is a javascript parser, and will be an interpreter someday. Today I was able to get RKelly to parse and produce a parse tree of prototype.js. I couldn't get GraphViz to export the file, but you can download the dot file here.

Here is a parse tree of a function that calculates the fibonacci sequence:
fibonacci parse tree

Here is the javascript code:

function f(n) {
  var s = 0;
  if(n == 0) return(s);
  if(n == 1) {
    s += 1;
    return(s);
  } else {
    return(f(n - 1) + f(n - 2));
  }
}

And the ruby code that produced that graph:

require 'rkelly'

parser = RKelly::Parser.new
puts parser.parse(File.read(ARGV[0])).to_dots

I need a new computer..... It took 2 minutes to render the prototype graph! Ugh!

Posted by Aaron PattersonPermalinkLeave your Comment »

5 Comments for “Parsing Javascript Parser”

  1. Chris Anderson says:

    Does RKelly output the same as ParseTree? Maybe you could teach it to, and then execute the Javascript as Ruby. How wicked is that?

    Wednesday, 26 December 2007 @ 2:14pm

  2. Aaron Patterson says:

    Not yet. The parse tree is not quite the same. I may get it to output the same thing as ParseTree, but for now I’m focusing on writing an interpreter.

    Wednesday, 26 December 2007 @ 2:26pm

  3. Andrew Dupont says:

    Great work, Aaron. Think you could generate a chart of some small portion of Prototype, like the $ function?

    We’ve been looking at rbNarcissus as a possible foundation for some internal tools that would make the Prototype Core team’s lives easier, but it looks like RKelly might be the better way to go.

    Thursday, 3 January 2008 @ 3:40pm

  4. Aaron Patterson says:

    Thanks! Absolutely. All nodes in the parse tree implement the to_dots method. So if you find your node, you can call .to_dots on it and get the parse tree below it.

    Here is the code:

    parser = RKelly::Parser.new
    tree = parser.parse(File.read(ARGV[0]))
    puts tree.value.find { |x| x.value == ‘$’ }.to_dots

    Here is the dot file. Here is the image:
    prototype dollar function

    Thursday, 3 January 2008 @ 4:30pm

  5. Ruby Tutorial: Scraping Muxtape for mp3 + iTunes playlist creation « Leveraging Visionary Paradigms says:

    [...] since the JavaScript to parse is not all that hard. To do the parsing, I briefly considered RKelly, which looks very interesting, but there is no gem yet, so I passed. I just use Hpricot to parse [...]

    Sunday, 20 April 2008 @ 4:50pm

Leave your Respond

Check Spelling
Activate Spell Check while Typing

RSS Feed for this EntryTrackBack URL