nunojob:~ dscape/08$ echo The Black Sheep

I think making a tool like ANTLR is a non-dynamic language is a shot in the foot!

It just makes sense to use dynamic languages to create meta-language tools. Would facilitate the work of the developer and of the ones writing grammars taking away all that java trash that helps in nothing to make out stuff work. ANTLR is great, but all the negative aspects (from end-user point of view) arise from the fact of having all that unnecessary information and needing to declare types and other stuff that ARE NOT NATURAL for a grammar to have. And the way it’s implemented in Java is just a way of copying the way (bad) dynamic languages work.

grammar Expr; 

options {
  output = AST;
  ASTLabelType = CommonTree;
} 

prog
     : ( stat
         { System.out.println( $stat.tree.toStringTree() ); }
       )+
     ;

stat
     : expr NEWLINE -> expr
     | ID '=' expr NEWLINE -> ^('=' ID expr)
     | NEWLINE ->
     ; 

expr
     : multExpr ( ( '+'^ | '-'^ ) multExpr )*
     ;

multExpr
     : atom ( '*'^ atom )*
     ;

atom
     : INT
     | ID
     | '('! expr ')'!
; 

ID
     : ( 'a'..'z' | 'A'..'Z' )+
     ;

INT
     : '0'..'9'+
     ; 

NEWLINE
     : '\r'? '\n'
     ; 

WS
     : ( ' ' | '\t' | '\n' | '\r' )+
       { $channel=HIDDEN; }
     ;
grammar Expr; 

options {
  output = AST;
} 

prog
     : ( stat
         { puts $stat.to_tree }
       )+
     ;

stat
     : expr NEWLINE
     | ID '=' expr NEWLINE -> ^ // mark the root somehow
     | NEWLINE
     ; 

expr
     : multExpr ( ( '+'^ | '-'^ ) multExpr )*
     ;

multExpr
     : atom ( '*'^ atom )*
     ;

atom
     : INT
     | ID
     | '('! expr ')'!
; 

ID
     : ( 'a'..'z' | 'A'..'Z' )+
     ;

INT
     : '0'..'9'+
     ; 

NEWLINE
     : '\r'? '\n'
     ; 

WS
     : ( ' ' | '\t' | '\n' | '\r' )+
       { skip }
     ;

Just wish I had the time to implement it in Ruby and prove my point! Maybe in the masters thesis..

By the way, the ANTLR Book is great. If you want to learn about grammars and creating DSL’s you will love it. But be advised, you’ll hate the java trash that comes with it.

By the way Google changed their icon.

But once again, who gives a fuck? I sure don’t.

Comments on: "Wouldn’t ANTLR be better if written in a dynamic language?" (3)

  1. I really don’t get your argumentation here. Are you talking about the ANTLR grammars, or the ANTLR tool or both?

    You can use ANTLR to generate grammars in a number of dynamic languages – the tool is not constrained with respect to the languages you can output.

    As for your example at the end, you might find this interesting: http://www.hokstad.com/strawman-for-a-new-parser-generator.html

    I wrote it to just play with some ideas for a syntax for a parser generator. There are a number of errors and inconsistencies in the grammar example (seeing as it was written without a tool to test it with…), but I like the concise feel to it, and I’m considering writing a small tool in Ruby to generate actual parsers from it.

    You might also want to look at Ragel (http://research.cs.queensu.ca/~thurston/ragel/)

  2. I was talking about porting ANTLR to a dynamic language. The features would make possible stuff that is being emulated in java as if it was a dynamic language and duck typing would remove all that trash java has.

    The best place i can give you to discover great dynamic techniques in ruby in Ola Bini blog. [http://ola-bini.blogspot.com/]

    After you are aware of how things can be done and how some design patterns don’t exist in ruby cause it simplifies stuff so much you will probably get my point on how a ANTLR would be more focused on the grammar and would have less messy incorporated code.

    Thanks for the comment and the help. I will check that up asap :) Cheers

  3. I know the features of Ruby very well – I write Ruby code most days, and have read through most of the code for the interpreter.

    But frankly, having written several parser generators myself, I’m not sure it matters much for ANTLR any more than for any other type of program. That is, I’m sure it’d be nicer in Ruby, but I don’t see a reason to focus on ANTLR versus any other Java app.

    That ANTLR is written in Java doesn’t affect the generated parsers at all, which is what matters unless you actually want to modify ANTLR itself. The grammar syntax for ANTLR for example is completely independent of Java, since it needs to support generating parsers in other languages (including Ruby).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: