27 February 2013

Meet the grammars

The idea of this research was inspired by two general ideas that take place in my university:
  1. C++ is canonical object oriented language (that is used to teach 1st year students an OO programing)
  2. Smalltalk is a hard (esoteric?) language and should be approached with caution
Lately I was working with ASTs a lot. After completing full Smalltalk AST meta-mode with ease, upcoming work on Java's abstract syntax tree looks like a big pain in the ass. So I decided to do some fun stuff for a while like generating dependency graphs of language grammars and comparing them.

First of all lets take a look at Pharo Smalltalk:

you can understand what's going on after looking at it for some time. Yes the diagram may be missing a node or two, because I've just copied grammar from petit parser last night. But in general this graph should represent a full grammar of Pharo.

Now let's look on something more popular like Java:

in particular Java SE 6 grammar. You just can't handle it easily. Not mentioning that bitmap version had too high resolution for Google to store. Java's grammar is quite huge, and has so many entities that edges between them start to clutter a lot.

But let's get back to our first competitor: C++

it's just a monster. And you probably do not want to go over all the details :)

Of course grammar complexity does not define the general complexity of the language, as for example Brainfuck has only 8 commands available at all. But if we want to talk in general about the choice of teaching C++ as a basic OO language, then this fact is one among the advantages of Smalltalk over C++. Also we can mention that C++ is C with classes glued on top of it and is not an OO language in general. One last thing: you have to manage memory yourself in C++, it's great despite the fact that nobody does it properly in the end.

If you are curious about the other diagrams, here are also Ruby:

and Javascript:

found on Nick Sieger's blog.

Also let's take a look at some funny metrics:

and diagram bitmap images size comparison :)
* all bitmap images were created from their vector representation with the same resolution setup.

Ahhh… messing with grammars was fun (even if not very productive). Anyway it's nice to know how simple is Pharo's grammar. Maybe some time I'll be able to show AST meta-models diagrams, they should be more interesting.

Have a nice day, and code well!


Java and C++ grammars were taken from ANTLRv3 website, Pharo grammar can profit from your contributions on GitHub. Graph visualizations done by Graphviz.


No comments:

Post a Comment