Posts Tagged ‘actionscript’

GOLD Parser for AS3

Monday, June 9th, 2008

LALR anyone? If you don’t know what I’m talking about, I’ll give you a little background.

Back in 2002, I was introduced to the GOLD Parser while doing some Pocket PC development with the .NET Compact framework. The app I was working on was essentially an assembler, and I needed some way to parse and tokenize the assembly source so it could be assembled. The GOLD Parser, written by a clever chap named Devin Cook, seemed to be the perfect solution. It uses a system of creating compiled “grammars” via a builder application. Those grammars can then be loaded at runtime, and input can be parsed against them. It’s a pretty elegant solution that minimizes the time consuming nastiness of processing the grammar, by doing it once and persisting it.

It was love at first sight. However at that time, the Compact Framework was pretty limited, and there was really no way for my .NET app to take advantage of the VB code that the GOLD Parser was written in.

So, I ported it.

Fast forward several years; I’m now planning a similar assembler type project, but this time using Flex and ActionScript to bring it to life. My first thought was to do a quick search on the net for LALR parsers written for flex, thinking it would be pretty easy to find.

Hmmm.

I’m normally pretty keen using “the Internets”, but, I couldn’t find it. I found similar monsters, such as ANTLR, which “emits” parser code for specific targets. They’re pretty close to a solution for emitting ActionScript (currently Alpha-quality release), but it’s still not what I wanted.

After doing a little more research, I found that some people might think ActionScript is too slow to handle it. For instance, see the comments to this post.

Hmmm.

Well, I’m always up for a challenge, and with a new baby, I’m always up anyway :)

So, I chose the fastest implementation of the GOLD Parser I could find, one by Vladimir Morozov, and ported it to AS3.

Overall, I’m pretty impressed. The only drawback is that, right now, I’m loading the compiled grammars as XML. As we all know, XML can be quite chunky, and as it’s a pure ActionScript implementation, the grammars have to be tossed down the wire to the client for processing. Complex grammars which in compiled format are on the order of 80K or so, can end up being around 4mb.

Yuck.

This issue is mainly due to my own laziness and will be addressed soon with a couple more methods that load the actual compiled grammars. But hey, it’s a start.

So, is it too slow? Is ActionScript able to handle it? Well, I’ll let you answer that on your own.

Check out the GOLD Parser Workbench application I wrote to demonstrate the library.

But, I will say this; the C# I ported from parsed a test file in 552 ms. The same test file parsed via the AS3 library? 596 ms.