Search code examples
cssparsingbrowserwebkitgrammar

Where can I find Webkit's CSS grammar files?


Unlike HTML, CSS is a context free grammar. Syntax grammar is usually described in BNF or EBNF notation.

According to Tali Garsiel's articke How Browsers Work, WebKit uses Flex and Bison parser generators to create parsers automatically from CSS grammar files.

As Webkit is an open source project, I would expect these grammar files can be found somewhere in its source code. I haven't been able to find it, though. Can anyone help me out here?


Note 1 :

I'm NOT asking how browsers parse/render CSS / how browsers read and interpret CSS!!

I know that already. In fact, it's explained pretty well in the aforementioned article How Browsers Work. So no, it's not a duplicate of that question.

What I'm looking for, is the specific CSS grammar files that Webkit uses to parse/render CSS, which I haven't been able to find anywhere.


Note 2 :

I'm also NOT asking anyone to recommend or find a book, tool, software library, tutorial or other off-site... at all.

I'm just asking for where to find some files that Webkit uses (or used) for parsing CSS, because I've been unable to find them myself.


Note 3 :

I'm asking this because I'm looking for a convenient way to parse CSS. I want to base this approach on the way a browser parses CSS (more specifically = by analysing how it is parsed by Webkit browsers).

So yes, this question really is a programming question and totally on topic here at Stack Overflow.


Solution

  • The bison file (or at least, a version of it) is in the css directory as CSSGrammar.y.in. That file is preprocessed and then run through bison using a perl script.

    Apparently the flex scanner was rewritten in 2011/12; see this webkit bug. The handbuilt scanner is part of CSSParser.cpp.

    By looking back in the git history, you could find older versions. For example, in the safari-4-branch, I found the following bison and flex files.