Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
3  
5. Take an existent parser, already unit-tested. –  MainMa Aug 21 '12 at 19:01
    
I'm not sure what you are trying to do with the parser, but I do know from experience that many browsers don't agree with the w3c's idea of CSS, either in grammar or in action. In most cases the whole "how does the browser parse this" is the most important bit. –  Wyatt Barnett Aug 21 '12 at 21:01
add comment

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
1  
That's a fair assessment. Analyzing the grammar, it seems to be kind of a gray area - just complicated enough to make hand coding a pain, but just simple enough to make a p/g seem a bit heavy handed. –  Kevin Hsu May 21 '11 at 7:03
add comment

I converted the published CSS grammar to a syntax that can be handled by the GOLD Parser.

My reasoning was that that my using a machine-generated parser derived from the formal grammar was the most likely way to get an accurate and maintainable solution.

I am happy with the result.

The main defect (that I want to work on next) is the error-recovery (as mentioned by Dean Harding in his answer): my current implementation can only parse correct CSS.

share|improve this answer
1  
imho, we shouldn't let bad css work anyhow. –  CaffGeek Aug 21 '12 at 19:43
add comment

I just did this and I simply wrote C# statements to process the rules of CSS.

My code was very simple but it did what I needed. It starts by stripping out all the comments. Then I just look for the next { character and assume the selectors came before, and the declarations are between that character and the next } character.

I also added simple support for the @media rule.

My code was published in the article A Simple CSS Parser.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.