📄 grammar.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html lang="en"><HEAD><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><TITLE>Appendix D: The grammar of CSS2</TITLE><link rel="stylesheet" href="style/default.css" type="text/css"><link rel="prev" href="notes.html"><link rel="next" href="refs.html"><link rel="contents" href="cover.html#minitoc"><link rel="CSS-properties" href="propidx.html" title="properties"><link rel="index" href="indexlist.html" title="index"></HEAD><BODY><div class="navbar" align="center"><p><a href="notes.html">previous</a> <a href="refs.html">next</a> <a href="cover.html#minitoc">contents</a> <a href="propidx.html">properties</a> <a href="indexlist.html">index</a> </div><hr class="navbar"><H1 align="center">Appendix D. The grammar of CSS2</H1><div class="subtoc"><p><strong>Contents</strong> <ul class="toc"> <li class="tocline2"><a href="grammar.html#q1" class="tocxref">D.1 Grammar</a> <li class="tocline2"><a href="grammar.html#q2" class="tocxref">D.2 Lexical scanner</a> <li class="tocline2"><a href="grammar.html#tokenizer-diffs" class="tocxref">D.3 Comparison of tokenization in CSS2 and CSS1</a> </ul></div><P><em>This appendix is normative.</em><P>The grammar below defines the syntax of CSS2. It is in some sense,however, a superset of CSS2 as this specification imposes additionalsemantic constraints not expressed in this grammar. A conforming UAmust also adhere to the <a href="./syndata.html#syntax">forward-compatible parsing rules</a>, the <ahref="./about.html#property-defs">property and value notation</a>,and the unit notation. Inaddition, the document language may impose restrictions, e.g. HTMLimposes restrictions on the possible values of the "class" attribute.<h2><a name="q1">D.1 Grammar</a></h2><P> The grammar below is <a name="x0"><span class="index-inst"title="LL(1)">LL(1)</span></a> (but note that most UA's should not use itdirectly, since it doesn't express the <ahref="syndata.html#parsing-errors">parsing conventions</a>, only theCSS2 syntax). The format of the productions is optimized for humanconsumption and some shorthand notation beyond Yacc (see <a href="refs.html#ref-YACC" rel="biblioentry" class="noxref"><span class="normref">[YACC]</span></a>) isused:</P><ul><li><strong>*</strong>: 0 or more<li><strong>+</strong>: 1 or more<li><strong>?</strong>: 0 or 1 <li><strong>|</strong>: separates alternatives <li><strong>[ ]</strong>: grouping</ul><p>The productions are:<pre>stylesheet : [ CHARSET_SYM S* STRING S* ';' ]? [S|CDO|CDC]* [ import [S|CDO|CDC]* ]* [ [ ruleset | media | page | font_face ] [S|CDO|CDC]* ]* ;import : IMPORT_SYM S* [STRING|URI] S* [ medium [ ',' S* medium]* ]? ';' S* ;media : MEDIA_SYM S* medium [ ',' S* medium ]* '{' S* ruleset* '}' S* ;medium : IDENT S* ;page : PAGE_SYM S* IDENT? pseudo_page? S* '{' S* declaration [ ';' S* declaration ]* '}' 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* declaration [ ';' S* declaration ]* '}' S* ;<a name="x1"><span class="index-inst" title="selector">selector</span></a> : simple_selector [ combinator simple_selector ]* ;simple_selector : element_name? [ HASH | class | attrib | pseudo ]* S* ;class : '.' IDENT ;element_name : IDENT | '*' ;attrib : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S* [ IDENT | STRING ] S* ]? ']' ;pseudo : ':' [ IDENT | FUNCTION S* IDENT S* ')' ] ;declaration : property ':' S* expr prio? | /* empty */ ;prio : IMPORTANT_SYM S* ;expr : term [ operator 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 S* expr ')' S* ;/* * There is a constraint on the <a name="x2"><span class="index-inst" title="color">color</span></a> 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* ;</pre><h2><a name="q2">D.2 Lexical scanner</a></h2><p> The following is the <a name="x3"><span class="index-def"title="tokenizer">tokenizer</span></a>, written in Flex (see <a href="refs.html#ref-FLEX" rel="biblioentry" class="noxref"><span class="normref">[FLEX]</span></a>)notation. The tokenizer is case-insensitive.<p>The two occurrences of "\377" represent the highest characternumber that current versions of Flex can deal with (decimal 255). Theyshould be read as "\4177777" (decimal 1114111), which is the highestpossible code point in <a name="x4"><span class="index-inst"title="unicode">Unicode</span></a>/<a name="x5"><span class="index-inst"title="iso-10646">ISO-10646</span></a>.<pre>%option case-insensitiveh [0-9a-f]nonascii [\200-\377]unicode \\{h}{1,6}[ \t\r\n\f]?escape {unicode}|\\[ -~\200-\377]nmstart [a-z]|{nonascii}|{escape}nmchar [a-z0-9-]|{nonascii}|{escape}string1 \"([\t !#$%&(-~]|\\{nl}|\'|{nonascii}|{escape})*\"string2 \'([\t !#$%&(-~]|\\{nl}|\"|{nonascii}|{escape})*\'ident {nmstart}{nmchar}*name {nmchar}+num [0-9]+|[0-9]*"."[0-9]+string {string1}|{string2}url ([!#$%&*-~]|{nonascii}|{escape})*w [ \t\r\n\f]*nl \n|\r\n|\r|\frange \?{1,6}|{h}(\?{0,5}|{h}(\?{0,4}|{h}(\?{0,3}|{h}(\?{0,2}|{h}(\??|{h})))))%%[ \t\r\n\f]+ {return S;}\/\*[^*]*\*+([^/][^*]*\*+)*\/ /* ignore comments */"<!--" {return CDO;}"-->" {return CDC;}"~=" {return INCLUDES;}"|=" {return DASHMATCH;}{string} {return STRING;}{ident} {return IDENT;}"#"{name} {return HASH;}"@import" {return IMPORT_SYM;}"@page" {return PAGE_SYM;}"@media" {return MEDIA_SYM;}"@font-face" {return FONT_FACE_SYM;}"@charset" {return CHARSET_SYM;}"@"{ident} {return ATKEYWORD;}"!{w}important" {return IMPORTANT_SYM;}{num}em {return EMS;}{num}ex {return EXS;}{num}px {return LENGTH;}{num}cm {return LENGTH;}{num}mm {return LENGTH;}{num}in {return LENGTH;}{num}pt {return LENGTH;}{num}pc {return LENGTH;}{num}deg {return ANGLE;}{num}rad {return ANGLE;}{num}grad {return ANGLE;}{num}ms {return TIME;}{num}s {return TIME;}{num}Hz {return FREQ;}{num}kHz {return FREQ;}{num}{ident} {return DIMEN;}{num}% {return PERCENTAGE;}{num} {return NUMBER;}"url("{w}{string}{w}")" {return URI;}"url("{w}{url}{w}")" {return URI;}{ident}"(" {return FUNCTION;}U\+{range} {return UNICODERANGE;}U\+{h}{1,6}-{h}{1,6} {return UNICODERANGE;}. {return *yytext;}</pre><h2>D.3 <a name="tokenizer-diffs">Comparison of tokenization in CSS2 andCSS1</a></h2><p>There are some differences in the syntax specified in the CSS1recommendation (<a href="refs.html#ref-CSS1" rel="biblioentry" class="noxref"><span class="informref">[CSS1]</span></a>), and the one above. Most of these are dueto new tokens in CSS2 that didn't exist in CSS1. Others are becausethe grammar has been rewritten to be more readable. However, there aresome incompatible changes, that were felt to be errors in the CSS1syntax. They are explained below.<ul><li>CSS1 style sheets could only be in 1-byte-per-characterencodings, such as ASCII and ISO-8859-1. CSS2 has no suchlimitation. In practice, there was little difficulty in extrapolatingthe CSS1 tokenizer, and some UAs have accepted 2-byte encodings.<li>CSS1 only allowed four hex-digits after the backslash (\) to referto Unicode characters, CSS2 <ahref="syndata.html#escaped-characters">allows six</a>. Furthermore,CSS2 allows a whitespace character to delimit the escapesequence. E.g., according to CSS1, the string "\abcdef" has 3 letters(\abcd, e, and f), according to CSS2 it has only one (\abcdef).<li>The tab character (ASCII 9) was not allowed in strings. However,since strings in CSS1 were only used for font names and for URLs, theonly way this can lead to incompatibility between CSS1 and CSS2 is ifa style sheet contains a font family that has a tab in its name.<li>Similarly, newlines (<a href="syndata.html#strings">escaped with abackslash</a>) were not allowed in strings in CSS1.<li>CSS2 parses a number immediately followed by an identifier as aDIMEN token (i.e., an unknown unit), CSS1 parsed it as a number and anidentifier. That means that in CSS1, the declaration 'font:10pt/1.2serif' was correct, as was 'font: 10pt/12pt serif'; in CSS2, aspace is required before "serif". (Some UAs accepted the firstexample, but not the second.)<li>In CSS1, a class name could start with a digit (".55ft"), unlessit was a dimension (".55in"). In CSS2, such classes are parsed asunknown dimensions (to allow for future additions of new units). Tomake ".55ft" a valid class, CSS2 requires the first digit to beescaped (".\55ft")</ul><hr class="navbar"><div class="navbar" align="center"><p><a href="notes.html">previous</a> <a href="refs.html">next</a> <a href="cover.html#minitoc">contents</a> <a href="propidx.html">properties</a> <a href="indexlist.html">index</a> </div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -