Parsing Javascript Parser 6

Posted by Aaron Patterson on December 24, 2007

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!

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Chris Anderson Wed, 26 Dec 2007 14:14:37 UTC

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

  2. Aaron Patterson Wed, 26 Dec 2007 14:26:16 UTC

    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.

  3. Andrew Dupont Thu, 03 Jan 2008 15:40:31 UTC

    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.

  4. Aaron Patterson Thu, 03 Jan 2008 16:30:05 UTC

    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

  5. [...] 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 [...]

  6. kampers.net » Blog Archive Wed, 03 Dec 2008 03:37:27 UTC

    [...] Kelly is a fictional project that I made up so that I could name my project RKelly.” —Aaron Patterson on his new Javascript [...]

Comments

Check Spelling
Activate Spell Check while Typing