
234 E. Homework Assignments
E.2 Homework Assignment E.2
Your assignment for Homework E.2 is to implement a stack to parse simpliļ¬ed
XML input and verify that the input consitutes well formed XML. This is quite
similar to parsing an algebraic expression to ensure that the open and closing
parentheses have been done correctly. Indeed, many algebraic expressions are
written with three diļ¬erent opening and closing characters: parentheses (and
), āsquare bracketsā [and ], and ācurly bracesā {and }. XML just allows a
user to generate an essentially inļ¬nite variety of opening and closing tokens as
<tag> and </tag> with the string used for tag being almost any text string at
all.
This programming assignment will require you to do two diļ¬erent programs.
The ļ¬rst is the code for creating and managing a stack as a data structure, and
the second is the code that uses the stack to parse XML.
Your main class need only declare and then invoke an XLMParser object.
This object should contain a checkValidXML method that takes a Scanner as
a parameter and then returns a boolean
You should use the parsing code done in Lab Assignment C.3 to be able to
separate the tokens in the input ļ¬le into open tokens, close tokens, and data
tokens.
The way to test for valid XML using a stack is this. As you parse the
input ļ¬le, all open tokens and data tokens should be pushed onto the stack.
When you encounter a close token, you should pop the zero or more data
tokens, saving them in a string, and the ļ¬rst open token that is popped oļ¬
the stack should be the open token that matches the close token. Should you
encounter the one exceptional case <whatever /> then you should push the
string NULL (opening and closing underscores intentional) onto the stack as
the data element. Yes, this does mean that this particular tag cannot be used
for other purposes, but thatās the way that a number of such software tools
work.
You should implement the stack object using the interface IStack given
here and found on the Moodle website.
The actual Stack class can use your DLL from Homework 1. By doing this,
the push method can simply use the existing addAtHead method to push a
node onto the stack, and the pop method can simply return the head node of
the DLL. (Remember to unlink the head node as well.) Creating a stack is thus
the easy part of this program; the hard part of the program is to use the stack
to verify correctness of the input ļ¬le.
Note that you will have to open up your DLL code slightly to make this
work. There is a valid question that can be raised about which methods to
add, delete, modify nodes should be public and which should be private.In