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!

4 Comments

  1. Posted December 26, 2007 at 2:14 pm | Permalink

    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. Posted December 26, 2007 at 2:26 pm | Permalink

    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. Posted January 3, 2008 at 3:40 pm | Permalink

    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. Posted January 3, 2008 at 4:30 pm | Permalink

    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

One Trackback

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

Post a Comment

Your email is never shared. Required fields are marked *

*
*
Check Spelling
Activate Spell Check while Typing