Question: You are given a driver for the XHTML validator. The code you are provided with will read an XHTML file and scan for any tags.

  1. You are given a driver for the XHTML validator. The code you are provided with will read an XHTML file and scan for any tags. It passes each tag it encounters to a Balancer whose job is to make sure that the tags are occurring in properly balanced pairs.

    The driver is designed to take the path to the file to be checked as a command line parameter.

    ./xmlcheck test0.html 
  2. You must supply the Balancer class. This class must use a std::stack as its primary data member. (You may have additional simple data members, bools or ints, but no containers other than the stack.) The two most critical operations the Balancer class must support are

    • tag(string atag): indicates that the driver has encountered a tag. The full text of the tag is passed as the parameter atag. This may be an opening tag, a closing tag, or a singleton tag.

    • status(): returns a single int indicating the status of what has been observed so far:

      • -1 indicates that an error has been detected.
      • 0 indicates that no error has been detected, but that one or more opening tags have yet to be matched
      • 1 indicates that no error has been detected and there are no opening tags that have yet to be matched against a closing tag.
  3. As in the prior assignments, you also will be provided with unit tests for the class that you are working on (Balancer);

    There is also a pair of test files test0.dat and test1.dat that you can use to test the full xmlcheck program. test0.html should be reported as OK, but test1.html has mismatched tags. As always, you should conduct additional systems testing with your own test cases.

this is the files

xmlcheck.cpp

#include #include #include #include

#include "balancer.h" #include "FlexLexer.h" #include "scanner.h"

using namespace std;

std::string lowercase(std::string s) { using namespace std; for (string::size_type i = 0; i = 'A') && (s[i]

string location;

bool checkStatus(const Balancer& bal, const string& lastTag) { if (bal.status()

void collectFromFile (const string& fileName) { ifstream inFile (fileName);

Balancer balancer;

if (!inFile) { cout

yyFlexLexer scanner (&inFile);

cout

location = fileName;

bool done = false;

while (!done) { int tokenKind = scanner.yylex(); // cerr

switch (tokenKind) { case WORD: { break; }

case NAME: case LINK: case TAG: { // A tag has been detected.

//cerr

balancer.tag(lexeme); break; }

case ENDOFFILE: { done = true; } }

if (done) break; done = checkStatus(balancer, lexeme); } if (balancer.status() == 0) { cout

}

int main (int argc, char** argv) { if (argc != 2) { cerr

string fileName = argv[1];

collectFromFile (fileName);

return 0; }

--------------------------------------------------------------------

testBalancer.cpp

#include

#include "unittest.h"

using namespace std;

UnitTest (BalancerDefaultConstructor) { Balancer bal;

assertThat (bal.status(), is(1)); }

UnitTest (BalancerOneSingleton) { Balancer bal;

bal.tag(""); assertThat (bal.status(), is(1)); }

UnitTest (BalancerOneOpener) { Balancer bal;

bal.tag("

"); assertThat (bal.status(), is(0)); }

UnitTest (BalancerOneCloser) { Balancer bal;

bal.tag("

"); assertThat (bal.status(), is(-1)); }

UnitTest (BalancerSimpleMismatch) { Balancer bal;

bal.tag(""); assertThat (bal.status(), is(0)); bal.tag(""); assertThat (bal.status(), is(0)); bal.tag(""); assertThat (bal.status(), is(-1)); }

UnitTest (BalancerAlternation) { Balancer bal;

for (int i = 0; i "); } else { bal.tag(""); } assertThat(bal.status(), is(0));

bal.tag(""); assertThat(bal.status(), is(0));

bal.tag(""); assertThat(bal.status(), is(0));

if (i % 2 == 0) { bal.tag(""); } else { bal.tag(""); } assertThat(bal.status(), is(1)); } bal.tag(""); assertThat(bal.status(), is(-1)); }

UnitTest (BalancerNesting) { Balancer bal;

for (int i = 0; i "); } else { bal.tag(""); } assertThat(bal.status(), is(0));

bal.tag(""); assertThat(bal.status(), is(0));

bal.tag(""); assertThat(bal.status(), is(0)); }

for (int i = 0; i "); assertThat(bal.status(), is(0));

bal.tag(""); assertThat(bal.status(), is(0));

if (i % 2 == 0) { bal.tag(""); } else { bal.tag(""); } int expected = (i

UnitTest (BalancerMixed) { Balancer bal; for (int k = 0; k "); } else { bal.tag(""); } assertThat(bal.status(), is(0));

bal.tag(""); assertThat(bal.status(), is(0));

bal.tag(""); assertThat(bal.status(), is(0)); }

for (int i = 0; i "); assertThat(bal.status(), is(0));

bal.tag(""); assertThat(bal.status(), is(0));

if (i % 2 == 0) { bal.tag(""); } else { bal.tag(""); } assertThat(bal.status(), is(0)); } } for (int k = 0; k

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!