Contents
This appendix is normative.non-normative.
The grammar below defines the syntax of CSS2.CSS 2.1. It is in some sense,
however, a superset of CSS2CSS 2.1 as this specification imposes additional
semantic constraints not expressed in this grammar. A conforming UA
must also adhere to the
forward-compatible parsing rules, the selectors notation, the property and value notation,
and the unit notation. In addition,However, not all syntactically correct CSS can take
effect, since the document language may impose restrictions, e.g.restrictions that are
not in CSS, e.g., HTML imposes restrictions on the possible values of
the "class" attribute.
The grammar below is LL(1)LALR(1) (but note that most UA's should not use it
directly, since it doesn'tdoes not express the parsing conventions, only the
CSS2CSS 2.1 syntax). The format of the productions is optimized for human
consumption and some shorthand notation beyond Yacc (see [YACC]) is
used:
The productions are:
stylesheet : [ CHARSET_SYMS*STRINGS*';' ]? [S|CDO|CDC]* [ import[S|CDO|CDC]*[ CDO S* | CDC S* ]* ]* [ [ ruleset | media | page| font_face][S|CDO|CDC]*[ CDO S* | CDC S* ]* ]* ; import : IMPORT_SYM S* [STRING|URI] S*[ medium [ ',' S* medium]* ]?media_list? ';' S* ; media : MEDIA_SYM S*medium [ ',' S* medium ]*media_list '{' S* ruleset* '}' S* ; media_list : medium [ COMMA S* medium]* ; medium : IDENT S* ; page : PAGE_SYM S*IDENT?pseudo_page?S*'{' S*declarationdeclaration? [ ';' S*declarationdeclaration? ]* '}' S* ; pseudo_page : ':' IDENT; font_face : FONT_FACE_SYM S* '{' S* declaration [ ';' S* declaration ]* '}'S* ; operator : '/' S* | ',' S*| /* empty */; combinator : '+' S* | '>' S*| /* empty */ ; unary_operator : '-' | '+'; property : IDENT S* ; ruleset : selector [ ',' S* selector ]* '{' S*declarationdeclaration? [ ';' S*declarationdeclaration? ]* '}' S* ; selector : simple_selector [ combinatorsimple_selector ]*selector | S+ [ combinator? selector ]? ]? ; simple_selector :element_name?element_name [ HASH | class | attrib | pseudo ]*S*| [ HASH | class | attrib | pseudo ]+ ; class : '.' IDENT ; element_name : IDENT | '*' ; attrib : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S* [ IDENT | STRING ] S* ]? ']' ; pseudo : ':' [ IDENT | FUNCTION S*IDENT S*[IDENT S*]? ')' ] ; declaration : property ':' S* expr prio?| /* empty */; prio : IMPORTANT_SYM S* ; expr : term [operatoroperator? term ]* ; term :unary_operator?[ NUMBER S* | PERCENTAGE S* | LENGTH S* | EMS S* | EXS S* | ANGLE S* | TIME S* | FREQ S*| function] | STRING S* | IDENT S* | URI S* |RGB S* | UNICODERANGE S* |hexcolor | function ; function : FUNCTION S* expr ')' S* ; /* * There is a constraint on the color that it must * have either 3 or 6 hex-digits (i.e., [0-9a-fA-F]) * after the "#"; e.g., "#000" is OK, but "#abcd" is not. */ hexcolor : HASH S* ;
The following is the tokenizer, written in Flex (see [FLEX]) notation. The tokenizer is case-insensitive.
The two occurrences of"\377" representrepresents the highest character
number that current versions of Flex can deal with (decimal 255). TheyIt
should be read as "\4177777" (decimal 1114111), which is the highest
possible code point in Unicode/ISO-10646.
%option case-insensitive h [0-9a-f] nonascii[\200-\377][\240-\377] unicode\\{h}{1,6}[ \t\r\n\f]?\\{h}{1,6}(\r\n|[ \t\r\n\f])? escape{unicode}|\\[ -~\200-\377]{unicode}|\\[^\r\n\f0-9a-f] nmstart[a-z]|{nonascii}|{escape}[_a-z]|{nonascii}|{escape} nmchar[a-z0-9-]|{nonascii}|{escape}[_a-z0-9-]|{nonascii}|{escape} string1\"([\t !#$%&(-~]|\\{nl}|\'|{nonascii}|{escape})*\"\"([^\n\r\f\\"]|\\{nl}|{escape})*\" string2\'([\t !#$%&(-~]|\\{nl}|\"|{nonascii}|{escape})*\'\'([^\n\r\f\\']|\\{nl}|{escape})*\' badstring1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\\? badstring2 \'([^\n\r\f\\']|\\{nl}|{escape})*\\? badcomment1 \/\*[^*]*\*+([^/*][^*]*\*+)* badcomment2 \/\*[^*]*(\*+[^/*][^*]*)* baduri1 {U}{R}{L}\({w}([!#$%&*-\[\]-~]|{nonascii}|{escape})*{w} baduri2 {U}{R}{L}\({w}{string}{w} baduri3 {U}{R}{L}\({w}{badstring} comment \/\*[^*]*\*+([^/*][^*]*\*+)*\/ ident{nmstart}{nmchar}*-?{nmstart}{nmchar}* name {nmchar}+ num[0-9]+|[0-9]*"."[0-9]+[-+]?[0-9]+|[-+]?[0-9]*"."[0-9]+ string {string1}|{string2} badstring {badstring1}|{badstring2} badcomment {badcomment1}|{badcomment2} baduri {baduri1}|{baduri2}|{baduri3} url ([!#$%&*-~]|{nonascii}|{escape})*ws [\t\r\n\f]*\t\r\n\f]+ w {s}? nl \n|\r\n|\r|\frange \?{1,6}|{h}(\?{0,5}|{h}(\?{0,4}|{h}(\?{0,3}|{h}(\?{0,2}|{h}(\??|{h})))))A a|\\0{0,4}(41|61)(\r\n|[ \t\r\n\f])? C c|\\0{0,4}(43|63)(\r\n|[ \t\r\n\f])? D d|\\0{0,4}(44|64)(\r\n|[ \t\r\n\f])? E e|\\0{0,4}(45|65)(\r\n|[ \t\r\n\f])? G g|\\0{0,4}(47|67)(\r\n|[ \t\r\n\f])?|\\g H h|\\0{0,4}(48|68)(\r\n|[ \t\r\n\f])?|\\h I i|\\0{0,4}(49|69)(\r\n|[ \t\r\n\f])?|\\i K k|\\0{0,4}(4b|6b)(\r\n|[ \t\r\n\f])?|\\k L l|\\0{0,4}(4c|6c)(\r\n|[ \t\r\n\f])?|\\l M m|\\0{0,4}(4d|6d)(\r\n|[ \t\r\n\f])?|\\m N n|\\0{0,4}(4e|6e)(\r\n|[ \t\r\n\f])?|\\n O o|\\0{0,4}(4f|6f)(\r\n|[ \t\r\n\f])?|\\o P p|\\0{0,4}(50|70)(\r\n|[ \t\r\n\f])?|\\p R r|\\0{0,4}(52|72)(\r\n|[ \t\r\n\f])?|\\r S s|\\0{0,4}(53|73)(\r\n|[ \t\r\n\f])?|\\s T t|\\0{0,4}(54|74)(\r\n|[ \t\r\n\f])?|\\t U u|\\0{0,4}(55|75)(\r\n|[ \t\r\n\f])?|\\u X x|\\0{0,4}(58|78)(\r\n|[ \t\r\n\f])?|\\x Z z|\\0{0,4}(5a|7a)(\r\n|[ \t\r\n\f])?|\\z %%[ \t\r\n\f]+{s} {return S;}\/\*[^*]*\*+([^/][^*]*\*+)*\/\/\*[^*]*\*+([^/*][^*]*\*+)*\/ /* ignore comments */ {badcomment} /* unclosed comment at EOF */ "<!--" {return CDO;} "-->" {return CDC;} "~=" {return INCLUDES;} "|=" {return DASHMATCH;} {string} {return STRING;} {badstring} {return BAD_STRING;} {ident} {return IDENT;} "#"{name} {return HASH;}"@import"@{I}{M}{P}{O}{R}{T} {return IMPORT_SYM;}"@page"@{P}{A}{G}{E} {return PAGE_SYM;}"@media"@{M}{E}{D}{I}{A} {return MEDIA_SYM;}"@font-face" {return FONT_FACE_SYM;} "@charset""@charset " {return CHARSET_SYM;}"@"{ident} {return ATKEYWORD;} "!{w}important""!"({w}|{comment})*{I}{M}{P}{O}{R}{T}{A}{N}{T} {return IMPORTANT_SYM;}{num}em{num}{E}{M} {return EMS;}{num}ex{num}{E}{X} {return EXS;}{num}px{num}{P}{X} {return LENGTH;}{num}cm{num}{C}{M} {return LENGTH;}{num}mm{num}{M}{M} {return LENGTH;}{num}in{num}{I}{N} {return LENGTH;}{num}pt{num}{P}{T} {return LENGTH;}{num}pc{num}{P}{C} {return LENGTH;}{num}deg{num}{D}{E}{G} {return ANGLE;}{num}rad{num}{R}{A}{D} {return ANGLE;}{num}grad{num}{G}{R}{A}{D} {return ANGLE;}{num}ms{num}{M}{S} {return TIME;}{num}s{num}{S} {return TIME;}{num}Hz{num}{H}{Z} {return FREQ;}{num}kHz{num}{K}{H}{Z} {return FREQ;} {num}{ident} {returnDIMEN;}DIMENSION;} {num}% {return PERCENTAGE;} {num} {return NUMBER;}"url("{w}{string}{w}")"{U}{R}{L}"("{w}{string}{w}")" {return URI;}"url("{w}{url}{w}")"{U}{R}{L}"("{w}{url}{w}")" {return URI;} {baduri} {return BAD_URI;} {ident}"(" {return FUNCTION;}U\+{range} {return UNICODERANGE;} U\+{h}{1,6}-{h}{1,6} {return UNICODERANGE;}. {return *yytext;}
There are some differences in the syntax specified in the CSS1
recommendation ([CSS1]), and the one above. Most of these are due
to new tokens in CSS2 that didn'tdid not exist in CSS1. Others are because
the grammar has been rewritten to be more readable. However, there are
some incompatible changes, that were felt to be errors in the CSS1
syntax. They are explained below.
The lexical scanner for the CSS core syntax in section 4.1.1 can be implemented as a scanner without back-up. In Lex notation, that requires the addition of the following patterns (which do not change the returned tokens, only the efficiency of the scanner):
{ident}/\\ return IDENT;
#{name}/\\ return HASH;
@{ident}/\\ return ATKEYWORD;
#/\\ return DELIM;
@/\\ return DELIM;
@/- return DELIM;
@/-\\ return DELIM;
-/\\ return DELIM;
-/- return DELIM;
\</! return DELIM;
\</!- return DELIM;
{num}{ident}/\\ return DIMENSION;
{num}/\\ return NUMBER;
{num}/- return NUMBER;
{num}/-\\ return NUMBER;
[0-9]+/\. return NUMBER;
u/\+ return IDENT;
u\+[0-9a-f?]{1,6}/- return UNICODE_RANGE;