C# Parsing – Best Strategy for Writing a CSS Parser

cnetparsing

I'm in the middle of writing a CSS parser in C#. I'm well under way, but I also have those times where I wonder if I'm taking the best approach. The things I've considered are:

  1. Feed the CSS grammar from the W3C into a parser generator and working off that.
  2. Hand-code a CSS parser off the grammar.
  3. Use a generated tokenizer, but hand-code the parsing of the productions.
  4. The reverse of (3) – generate the productions, but hand-code the tokenizer.

Without revealing my current approach, I was wondering how others feel about this, and appreciate any comments and guidance from your experience. Part of this is also to see what questions people ask and compare the questions to what I asked myself.

Best Answer

Personally, I think the grammar is simple enough that I'd just do the whole thing by hand. Unless your project is already using a parser-generator for something else, it seems like overkill for something this small.

The other thing to keep in mind is that a conforming CSS parser should be fairly forgiving of syntax (and other) errors, which would make integrating a parser-generator more difficult.

Related Topic