CS 316 - Programming Assignment 3

This assignment has two parts.

Problem 1. For the first part, consider the following BNF grammar.

        expr ->  'a' '*' expr | expr '+' 'a' | 'a'
Notice that this grammar describes "expressions" with two operators, one operator that is left associative and the other right associative.

Let L denote the language determined by this grammar. Give a description of the language L. Then show that this grammar is ambiguous. Find an unambiguous grammar for the language L (hint: precedence). Write, and test, a recursive descent parser based on your unambiguous grammar.

Put all of your answers to this problem in a single file called Problem1.java. In a comment at the top of the file answer the questions given above, then below that write your parser as static methods. Be sure to include a main() method that tests your parser.

Problem 2. For this part you will write a recursive descent parser that parses XML data and uses the data to populate a Java object.

Here is a BNF grammar for XML structures that describe addresses.

   addresstag -> '<address>' addresstuff '</address>'
   addressstuff -> nametag locationtag
   nametag -> '<name>' namestuff '</name>'
   namestuff -> firstnametag lastnametag companytag
   namestuff -> lastnametag firstnametag companytag
   namestuff -> firstnametag lastnametag
   namestuff -> lastnametag firstnametag
   namestuff -> lastnametag companytag
   namestuff -> lastnametag
   namestuff -> companytag
   firstnametag -> '<first>' stringdata '</first>'
   lastnametag -> '<last>' stringdata '</last>'
   companytag -> '<company>' stringdata '</company>'
   locationtag -> '<location>' locationstuff '</location>'
   locationstuff -> numbertag streettag citytag statetag ziptag
   numbertag -> '<number>' stringdata '</number>'
   streettag -> '<street>' stringdata '</street>'
   citytag -> '<city>' stringdata '</city>'
   statetag -> '<state>' stringdata '</state>'
   ziptag -> '<zip>' stringdata '</zip>'
   stringdata -> any string that does not contain the characters '<' or '>'
In a Java file called Parser.java, define a function with the declaration
   public static void xml2obj(String addressXML, Address addressObj) throws Exception
This function should begin a recursive descent parse of the string contained in the parameter addressXML. As the parser is parsing the xml data, it should use the data that it finds to populate the fields of the Address object (using the appropriate set-methods from the supplied Address.java file).

Notice that you will need to slightly rewrite the above grammar (in EBNF) in order to write the parser. At the beginning of your Parser.java file, put the EBNF grammar that you are using to write your parser.

In the zip file along with this file there is a program TestParser.java that tests your xml parser. Do not make any changes to the TestParser.java file. When the TestParser.java program runs, it should produce output like that contained in the file test-output.txt.

Turn in a zip file called CS316Hw3Surname.zip containing your two Java source files, Problem1.java and Parser.java, and also the original files contained in h3.zip. This assignment is due Tuesday, February 19.

Your xml parser should give you an idea of what is going on in Ajax type web applications that use XMLHttpRequest to download XML, which then needs to be parsed in order to get at the data contained in the xml tags. The xml could also come from "object serialization," and what your parser is doing is "deserializing" an object.

If you think the xml version of an address is kind of trivial, try to work your way through these references.


Return to the CS 316 home page.


compliments and criticisms