Homogeneous ASTs for XML

Skip to end of metadata
Go to start of metadata

In lab Lexer for XML we read in some simple XML for a catalog of books and tokenized it. Now, we want to build a tree of the input and check to see if it's well-formed. So, for example, for input:

We want to create a tree looks like:

The output of your test rig should be the text equivalent with OK indicating well-formedness:

$ java XMLAST < catalog.xml
OK
( <catalog>
  ( <book> (<id> 101) (<genre> Computer) (<author> Jim Cortez)
    (<title> XML for dummies) (<price> 44.95)
    (<description> An in-depth look at creating mashed potatoes
      with XML.)
  )
  ( <book> (<id> 102) (<author> George Bush) (<title> I'm the decider)
    (<genre> Fantasy) (<price> 0.95) (<description> I like milk and cookies.)
  )
)

Upon invalid input, the program should indicate what the problem is:

$ java XMLAST
<catalog>
   <book>
      <id>101</id>
</catalog>
error: expecting </book>, found </catalog>
error: missing end tag for <catalog>
(<catalog> (<book> (<id> 101)))
$

Support code

You'll need your solution to the previous XML lab as a foundation for this lab: TagScanner.java, Token.java, TokenStream.java. Now, you need to build XMLAST.java, which has the main method that will have your tree building and well formedness check. For your convenience, here is your homogeneous AST node that you will use for both tag and text nodes:

Here is the infrastructure for your main program:

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.