A group of architects have devised a domain-specific language for describing buildings with multiple floors where each floor has one or more rooms. They have asked you to build a system which reads plans for complexes (one or more buildings) and displays the total area and the area for each building. The grammar the use is described as follows, where non-terminals are capitalized and terminals are between quotes:
Plans -> (Floor)+ Complex Floor -> 'floor' Name 'has' ('rooms'|'room') RoomList Complex -> 'complex' (Building)+ Building -> 'building' Name 'with' ('floors'|'floor') FloorList FloorList -> '{' FloorReference (',' FloorReference)* '}' FloorReference -> Name RoomList -> '[' Room (',' Room)* ']' Room -> Number 'by' Number Name -> [A-Z_]+ Number -> [0-9]+For example, a sample complex is
floor DINING has rooms [80 by 40, 10 by 20, 10 by 20] floor CUBICALS has rooms [20 by 20, 20 by 20, 20 by 20, 20 by 20] floor GRAND_OFFICE has rooms [40 by 30, 20 by 20, 15 by 15, 30 by 30] floor HALL has room [500 by 500] complex building A with floors {DINING, CUBICALS, CUBICALS} building B with floors {DINING, GRAND_OFFICE} building C with floor {DINING, HALL}Note the indentation and use of whitespace is immaterial. A simple way to break this input into tokens is to use string substitution to put spaces around commas, brackets, and braces (for example, convert "," to " , "), then use the Python string split operation to split on whitespace. You could also use regular expressions with split operations to break the input into tokens.
You are to write a recursive descent parser for this language and augment it with code to compute (and display) the total area of the complex. Assume all units are measurements in feet. For example, the program would print
Area for building A: 6800 square feet. Area for building B: 6325 square feet. Area for building C: 253600 square feet. Total area: 266725 square feet.If the input file has a syntax error, display a message indicating which token is missing; for example, if the input has four lines of floors (and no complex keyword), you would display
Syntax error in plan: no more tokens, but expected complexand terminate without computing the area. Similarly for other syntax errors such as missing "with" after a building name, not having commas between floor names, etc. For example, missing a '{' after a
floors
keyword (and having "CAFETERIA" in the same place)
would result in the message
Syntax error in plan: Expected {, received CAFETERIAThe other errors you must catch are references to unknown floor names. For example:
floor X has rooms [5 by 5] complex building Y with floors {BAD}In this case, the error message would be
Reference to missing floor BADAgain, followed by terminating the program without printing any information. In general, the specifics of the error messages are up to you other than having to match the output in esubmit.
[A-Z_]+
.
readinput.py
.
exit
to quit the program.
esubmit
. There is nothing to hand in
to Canvas.
plans.zip
.