XML technologies / DocBook / Block elements / Production rules (EBNF)
(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)
DocBook 5 allows production rules to be defined in Extended Backus–Naur Form (EBNF). These production rules provide an alternative representation of an algorithm and are needed, for example, in compiler construction. The XML specification also uses EBNF to define documents, prologs, tags, and so on. This is illustrated by a CDATA section. The following production is taken from Extensible Markup Language (XML): CDATA Sections:
EBNF production for a CDATA section
[1] | CDSect | : : = | CDStart CData CDEnd |
|
[2] | CDStart | : : = | '<![CDATA[' |
|
[3] | CDATA | : : = | (Char - (Char* ']]>' Char*)) /* Alle Zeichen außer ']]>' * / | |
[4] | CDEnd | : : = | ']]>' |
In the production rule above, the left-hand side always contains the production’s name and the right-hand side its definition. Thus, a CDATA section consists of three further productions: a start (CDStart), middle (CData), and end (CDEnd). A CDATA section begins with the string <![CDATA[. The middle may contain any characters (Char) except the sequence ]]>. A CDATA section ends with ]]>.
The preceding EBNF production is expressed in DocBook 5 markup as follows:
Example: Markup for an EBNF production for a CDATA section
<productionset>
<title>EBNF-Produktion eines CDATA-Abschnitts</title>
<production xml:id="prset">
<lhs>CDSect</lhs>
<rhs>
<nonterminal def="#pr.cdstart"/>
<nonterminal def="#pr.cdata"/>
<nonterminal def="#pr.cdend"/>
</rhs>
</production>
<production xml:id="pr.cdstart">
<lhs>CDStart</lhs>
<rhs>'<![CDATA['</rhs>
</production>
<production xml:id="pr.cdata">
<lhs>CData</lhs>
<rhs><lineannotation>Alle Zeichen außer ']]>'</lineannotation>
(Char - (Char* ']]>' Char*))
</rhs>
</production>
<production xml:id="pr.cdend">
<lhs>CDEnd</lhs>
<rhs>']]>'</rhs>
</production>
</productionset>Line 1: One or more productions are collected in a productionset. Since our definition of a CDATA section consists of three productions, there must also be three production elements.
Lines 3, 11, 15, and 21: A complete production with left- and right-hand sides, represented by production. Each production must have an anchor so that it can be referenced when needed.
Line 4: The left-hand side contains the production’s name.
Line 5: The right-hand side contains the definition. Other productions are referenced using nonterminal.
Lines 6 to 8: A reference to another production. The attribute def contains a URL pointing to the production. If the production is in the same document, a # character is added before the actual anchor value. The production’s name is then inserted automatically.
Line 17: Annotates a production
Note: Explicit line break
If you have many productions in an rhs element, line breaks can sometimes be confusing. To control the break yourself, insert the empty element <sbr/> at the desired position:
<rhs> A B C <sbr/> D E F ... </rhs>Productions often have additional conditions or constraints. In this case, use constraintdef and contstraint:
Example: Constraints on a production
<productionset>
<title>Division</title>
<production xml:id="pr.divisionexpr">
<lhs>DivExpr</lhs>
<rhs><nonterminal def="#pr.expression"/>'/'<nonterminal def="#pr.expression"/></rhs>
<constraint linkend="cd.div0"/>
</production>
<production xml:id="pr.expression">
<lhs>Expression</lhs>
<rhs>...</rhs>
</production>
</productionset>
<constraintdef xml:id="cd.div0">
<title>Division durch Null</title>
<para>Division durch Null ist ein Fehler.</para>
</constraintdef>Line 6: Reference to an element constraintdef. The attribute linkend points to the anchor of constraintdef.
Line 13: Defines the constraint, which must be outside productionset.
The result looks as follows:
Division
[5] DivExpr : : = Expression '/' Expression [Division durch Null]
[6] Expression : : = ...
Division durch Null
Division durch Null ist ein Fehler.
Figure: Constraints on a production (rendering of the example above)
For more details about EBNF, see the Wikipedia article.
Copyright © 2009 Millin Verlag
You may print the online version for your private use.
Otherwise, this chapter from "DocBook-XML: Medienneutrales und plattformunabhängiges Publizieren" is subject to the same provisions as the printed edition: the work, including all its parts, is protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Millin Verlag, Siebengebirgsring 36, 53797 Lohmar, info(at)millin.de
back to top