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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top