Trying Marpa to parse CSS

Some days ago, Marpa::XS reached version 1.0. Jeffrey Kegler, Marpa's godfather already wrote a some blogs about the algorithm operating behind.

In order to get a feeling on how to use Marpa, I decided to try it for parsing CSS. Thankfully, the official grammar for CSS is available. So I tried to become a friend of Marpa::XS. I was used to work with Parse::RecDescent in the past, so I could not imagine to get into trouble. However, using both modules is different. Parse::RecDescent handles the scanning process by itself and allows various shortcuts for repeating elements inside a grammar. Marpa on the other hand needs a separate scanner to feed it with tokens and only allows very simple grammar rules. Parse::RecDescent combines parser code and grammar rules in one string. Marpa keeps the grammar clean and calls action methods based on rules defined inside the grammar.

After figuring out how to write grammars and having coded a simple tokenizer I finally got a parser working. Maybe, some rules might need expansion because of erroneous white-space handling or other flaws, but the result impressed me. Defining the grammar as a data structure makes it very good readable. Getting action methods called for processing the parse results keeps code separate from the grammar which keeps the entire code readable.

What are the next steps? Maybe a compressor or a sass-like processor? Maybe.

My quick and dirty try can be seen here.

1 Comment

Really nice example. I've never seen an asterisk referred to as a "splash" before, but I like it. :)

Leave a comment

About Wolfgang Kinkeldei

user-pic I blog about Perl.