Converting Simple BNF grammar to DTD or schema


C

Chris

I have a requirement to have an XML document
that consists of an arbitrarily large expression.
For simplicity's sake, assume I have the following grammar:

expr => term rest
term => ID relop ID
rest => (conjunction term) | e
conjunction => AND|OR
relop => EQ|NE|GT|GE|LT|LE

Is it possible to convert this grammar to a DTD or schema?
I've tried the following DTD without success:
<!ELEMENT expr (term,rest)>
<!ELEMENT term (id,relop,id)>
<!ELEMENT rest ((conjunction,term)|EMPTY)>
<!ELEMENT conjunction (EMPTY)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT relop (EMPTY)>
<!ATTLIST conjunction type (AND|OR)>
<!ATTLIST relop type (EQ|NE|GT|GE|LT|LE)>

Any pointers appreciated.
Thanks in advance.
 
Ad

Advertisements

P

Patrick TJ McPhee

% I have a requirement to have an XML document
% that consists of an arbitrarily large expression.

You might want to sketch out what you want your XML to look like
before you write the DTD.

% expr => term rest
% term => ID relop ID
% rest => (conjunction term) | e

You probably want to use Kleene closure for this kind of production
what you've written will require

<expr><term><id>i</id><relop type='EQ'/><id>7</id></term><rest/></expr>

to represent

i = 7

To represent

i = 7 and j < 4

you'd need

<expr><term><id>i</id><relop type='EQ'/><id>7</id></term>
<rest><conjunction conjunction type='AND'/>
<term><id>i</id><relop type='EQ'/><id>7</id></term></rest>
</expr>

While what you probably want is this, without a `rest' element.

<expr><term><id>i</id><relop type='EQ'/><id>7</id></term>
<conjunction conjunction type='AND'/>
<term><id>i</id><relop type='EQ'/><id>7</id></term>
</expr>


You can get that by changing the definition of expr

<!ELEMENT expr (term,(conjunction,term)*>

and removing the definition of rest.
 

Top