Hacker News new | past | comments | ask | show | jobs | submit login
Visualization of the Grammars of Ruby, Java and Javascript (nicksieger.com)
56 points by r11t on Dec 6, 2009 | hide | past | favorite | 15 comments



There is far less of a difference here than might appear at first blush. These graphs might be useful for getting an overview of the grammar productions for these languages, but I don't think they are very helpful for comparing them.

The author processed Ruby's YACC file into something that ANTLR would accept and I'm not sure that all of the relevant information made it through. For one, it looks like Ruby's operator precedence is not captured here at all. Between this, and the fact that Ruby doesn't have separate statement and expression concepts, everything that's left ends up in one large highly connected graph.

The Java and Javascript graphs, generated as they are from the EBNF-like ANTLR, strike me as much more true to the productions of their respective grammars. Since they were both generated in the same way, they're also more directly comparable to one another. The most eye-catching thing about these two are of probably the chains of expressions which encode the precedence rules of their operators. Java has a boatload of operators, which account for the top half of its graph. It then has two snarls at the bottom, one for statements on the left, one for declarations on the right. Most modern languages in the algol family will have statement syntax rather similar to Java's here; if you're interested in what sets Java apart syntactically you'd likely find it clustered about the bottom right corner of the graph.

Javascript's operators are only slightly simpler than Java's, and there are two variations, one that allows the 'in' operator, and one that does not. This creates the two chains running down the bottom-right hand side. One interesting thing to note is the lack of tight, complicated snarls in the javascript graph. Javascript's grammar is fairly simple, and ignoring the 'in' operator, quite straightforwards, so I suppose this shouldn't be too surprising.


ANSI C and Python grammars:

Nick said ...

@Adam: check out http://www.flickr.com/photos/nicksieger/281055530/ and http://www.flickr.com/photos/nicksieger/281055485/. I can’t even imagine what a Perl grammar would look like


I can’t even imagine what a Perl grammar would look like

Since there's no BNF for Perl it would be hard to visualize its grammar.


perly.y might help (sadly it recently went from yacc to bison).


Well, you could use perl6's grammar, which is the closest you can get. Comparing it to python is rather funny:

http://www.python.org/doc/2.5.1/ref/grammar.txt

http://svn.pugscode.org/pugs/src/perl6/STD.pm

If you do a wc, perl6 is about 12-20 times the size of python (depending if you use lines or chars). It would be funny to see the per6 graph...


> [Perl 6] is about 12-20 times the size of python....

The Perl 6 grammar also includes comments and is itself written in Perl 6. A metacircular Python grammar would look different from the existing Python grammar.


Both of those are fairly simple! It's refreshing. I know some python people claim that the more rigid syntax of Python helps you code better. But I wouldn't be surprised if there was some motivation from the interpreter writers.


This seems to be an amazingly meaningless image without a lot more context and explanation. I don't see why showing it beside other languages helps at all either.

Maybe we could theorise about what's easier for programmers to understand. I'd suggest in that case that the more hierarchical systems are the hardest as the brain has more difficulty remembering small numbers of connections than a larger contextual web. But that's just an idea I would have looking at the images, I expect anyone who understands these well enough to draw conclusions from them would already be quite familiar with them.


Both Ruby and Javascript seem be from the camp of adding more language features and alternate syntax to make it easier for programmers to express themselves. They are happy to put in multiple ways to do something and support multiple paradigms in an effort to make things as easy as possible for as many programmers as possible.

When contrasted with Python, C, or other languages that have more strict syntax you may realize that there's a cost to having so many features. The interactions between all those parts of the grammar get very complicated.


More than just a visualization of Ruby's grammar, it also has visualizations of Java and JavaScript's grammars. The difference is pretty stark, although I don't know what to make of it yet.


I have edited the title to reflect that it also includes Java and Javascript.


It would be nice to have also there a visualization of Scheme.


Add Smalltalk grammar for a beautiful and stark contrast.


Definitely it needs other grammars. I wish to see Python as well whose grammar is advertised as very light. Also C++ (templates), and Perl (for a contrast with Python).


I'd love to see C++ plus templates visualized.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: