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 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 ExceptionThis 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
In the zip file along with this file there is a program
Turn in a zip file called 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. |