Homework 9XML and EBNF |
Modified: |
After taking over the operation of the exotic Syntactic Expression train, your company received considerable media coverage and a calamitous drop in passengers following several money saving moves by management. The media drew attention to several mistakes where train cars were mixed in some embarrassing ways; local news reports gleefully carried stories where passenger and dining cars were placed behind (downwind) of circus cars of smelly elephants; passenger trains with no dining cars that had to call ahead for pizzas delivered at road crossings; and on at least one occasion engines placed at opposite ends of the train that played tug-of-war over the passenger cars. Management, proclaiming on the 6 o'clock news to be capable of thinking outside of the boxcar, decided to give someone else the responsibility (you) for solving the problem.
The railroad had five rules to form trains. The rules are stated where each letter stands for a car on the train: E for Engine, C for Caboose, B for a regular Boxcar, P for Passenger, S for a smelly boxcar, and D for Dining car.
|
After pondering the problem and recognizing that checking the trains with your own eyeballs would be boring, time consuming, and miserable in times of bad weather, you conclude to send a computer to do the job in your place. To explain the rules to the computer you determine that:
You decide as a basic evaluation for management to define and test each of the above a-l train configurations.
The computer portion requires:
train.dtd - The DTD below defines the rules for a very simple train
having only an engine followed by a single caboose.
| <!ELEMENT train (E,C)> <!ELEMENT E EMPTY> <!ELEMENT C EMPTY> |
train.xml - The XML defines a train that follows the rules of the DTD
above.
| <!DOCTYPE train SYSTEM "train.dtd"> <train> <E/> <C/> </train> |
train.htm - The JavaScript below parses any well-formed XML and
displays the node names of the parse tree. The example works on IE browsers; if
using another browser see the discussion at:
http://www.w3schools.com/dom/dom_parser.asp.
<SCRIPT LANGUAGE="JavaScript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
try { xmlDoc.load("train.xml");
document.write("<pre>");
traverse(xmlDoc.documentElement,"");
document.write("</pre>");
}
catch(e) { document.write(
"URL "+xmlDoc.parseError.url+" Line "+xmlDoc.parseError.line+
" position "+xmlDoc.parseError.linepos+" "+
xmlDoc.parseError.srcText + " " + xmlDoc.parseError.reason);
}
function traverse(node,indent) {
var i, children, type = node.nodeTypeString;
if (type == "element") {
document.write("<br>" + indent + node.nodeName);
children = node.childNodes;
if (children != null)
for (i=0; i<children.length; i++)
traverse (children.item(i), indent + "| ");
}
}
</SCRIPT>
|
Internet Explorer - IE5 has an XML parser so can be used to
execute the train.htm JavaScript program above. The following illustrates IE5 used to execute the
train.htm with all files (train.htm, train.dtd and
train.xml) stored in the C:\temp directory. The first example is a valid
train while the second example has two engines when only one is allowed by the
DTD definitions given in train.dtd above.
| <!DOCTYPE train SYSTEM "train.dtd"> <train> <E/> <C/> </train> |
<!DOCTYPE train SYSTEM "train.dtd"> <train> <E/> <E/> <C/> </train> |
![]() |
![]() |
To make assembling and disassembling trains more efficient by forming a train from several other complete trains, the company managers developed two additional rules:
A train can follow a caboose.
For example: ESBCEPDC
A train can follow a dining car.
For example: EPDEBBCC
Note that a valid result of these rules is that a train with a smelly boxcar can be inserted in front of a train with passenger cars.
Document last modified: