An experience with antlr, java and python

I just wanted to share a little experience with generating an AS3 parser using antlr and python.
I was trying first to create the parser using GNU Flex and Bison in C, probably the best way for a very performancing parser.
Yeah, that’s right.. but looking at the antlr syntax I realized that’s easier and easier.
Moreover I start using this very useful eclipse plugin for antlr debugging which made my life easier!

The grammar file I created is a compromise between the asdt grammar file and the ECMA-262 grammar specification.

Once finished working on my eclipse project I’ve managed to parse succesfully all the adobe corelibs files using this java test file:
package org.sepy.core.parsers.as3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import antlr.CommonAST;
import antlr.RecognitionException;
import antlr.TokenStreamException;
public class Application {
public static void main(String argv[])
{
if(argv.length > 0)
{
File file = new File(argv[0]);
if(file.exists())
{
FileInputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AS3Lexer L = new AS3Lexer(is);
AS3Parser P = new AS3Parser(L);
try {
P.compilationUnit();
} catch (RecognitionException e) {
// TODO Auto-generated catch block
System.out.println(" line=" + e.line + ", column="+ e.column);
System.out.println(e.getMessage());
e.printStackTrace(System.err);
} catch (TokenStreamException e) {
// TODO Auto-generated catch block
System.out.println(" line=" + L.getLine() + ", column="+ L.getColumn());
System.out.println(L.getGuessInfo());
System.out.println(e.getMessage());
e.printStackTrace(System.err);
}
CommonAST.setVerboseStringConversion(false, P.getTokenNames());
CommonAST ast = (CommonAST) P.getAST();
System.out.println("Tree:");
System.out.println(ast.toStringTree());
}
}
}
}

Ok, done that I decided to export the grammar file for python (thanks to antrl python export feature).
Everything works fine also for python, but I realized that the python script were so much slower than the java one!
import sys
import antlr
import AS3Parser
import AS3Lexer
L = AS3Lexer.Lexer(filename);
P = AS3Parser.Parser(L);
P.setFilename(filename)
try:
P.compilationUnit();
ast = P.getAST();
except:
pass

On a 75Kb actionscript file the python script took about 7 seconds to run, while the java application only 2 seconds. I know python interpreter caould be slower than many other languages, but I never thought so much slower.
So I run the python hotshot profiler to see which could be the bottleneck in the python script and I found most of the problems were due to unuseless antlr (the python module) method’s calls.
After making corrections to the antlr.py file the same script took exactly half of the time. Now 3 seconds. Wow 🙂
But not fast enough.
So I enabled for the antlr python script psyco module and this time the same script took only 1.6 seconds.
Now the python script is fast enough, even if I’m sure I can make more optimizations in the antlr module…

Find programming documentation

I found two very useful reference sites which aggregate various documentation within an ajax suggestion search textbox (like google suggest).

QuickRef.org [view]
Very fast an easy, includes C, C++, CSS, HTML, HTML DOM, Java, JavaScript, MySQL, Perl, PHP, Python, and Ruby references. Unfortunely it does not include ActionScript reference. It has also a “add to google” button, to add a widget in your google homepage.

Fast API Search [view]
This includes ActionScript reference too! It allows to select which references to be used in the live search (such as  HTML, CSS, CSS2, Javascript, Google code, XML, XSL, XPath, XSD, PHP, Ruby, Perl, AS, ColdFusion, C, C++, MySQL, PostgreSQL, Oracle, Java…). Unfortunately this one does not include Python and it seems to be slower than quickref.org

Anyway, I think a couple of useful bookmark links.

Ben Forta presents ColdFusion Powered Flex and Scorpio (CF8)

On 6th of November actionscript.it staff will present a new event about Ajax, ColdFusion, Flex2, Flash Remoting and more. The speakers will be: Ben Forta, Vince Bonfanti, Charlie Arehart and Giorgio Natili.
In particular Ben Forta will present all the new features of ColdFusion 8 (codename Scorpio, planned for released mid-2007) and its integration with Flex2.

These are the other seminars:

  • AJAX & CFML Applications
  • Accessibility & AJAX
  • CFML Developement on .NET & J2EE platform
  • Flex Data Services & Coldfusion – CF8 sneaks
  • Coldfusion debug; Flex, Ajax& Flash Remoting with Fusion Debug
  • FusionReactor

The event will be in Rome at the Ritz hotel.
For more info visit the event page at actionscript.it

Flash, Internet Explorer and Firefox new releases

We had so many milestone releases today!
Flash player 9 for linux is probably the most exciting news.
Second, Microsoft announced Internet Explorer 7. (even if I dont like at all the new look).
Last but not least Firefox 2 is on the way!
Today the Firefox 2 Release Candidate 3 have been released (with Javascript 1.7 support and with Google suggest integrated).

MDM “Designer Vs Developer” Contest

MDM has lauched a flash contest for both Designers and Developers.
The prize is a Flash Goodie Bag with $1000 of Flash Software for each winner.

Check out the Designer Brief and the Developer Brief.
The two Contest winners (one from each category) will each receive a Flash Goodie Bag which includes over $1000 worth of Flash software including:

One Runner up from each contest will also receive a prize of MDM Zinc v2.5.

Launch Date: Monday 24 July 2006
Closing Date: Thursday 31 August 2006

Programming logic

During my tests with Flex2 I encountered a strange behavior with the “for” statement and I first thought about a bug of the player.
This simple script was crashing the player (it enters in an infinite loop):
<?xml version="1.0" encoding="utf-8"?>
private function test():void
{
for(var a:uint = 10; a >= 0; a--)
{
trace(a);
}
}

Once I changed the type of the “a” variable into a “Number” and an “int” I understand what I was missing…
The script worked well after the type change. But to be sure I did also a little test in C which gave me the same results as the flex2 one (infinite loop):

int main(int argc, char *argv[])
{
unsigned int a;
for(a = 10; a >= 0; a--)
{
printf("a = %d\n", a);
}
system("PAUSE");
return 0;
}

Yes, it’s absolutely logical!
After the last step, when a is 0, the program loses next value because it cannot be a negative value and then the loop continues to cicle.

Ok, it’s not a player bug… it’s me 🙁