Nada

Laboration 5 - Formula checking

Your program starts by building a database of atomic weights of the elements and then using it for analysis of personality chemistry. Example:
Name:Henrik
H 1.0079
E nonexistent
N 14.0067
R nonexistent
I 126.9054
K 39.098
Your total atomic weight is 181.018
The main loop of the program asks you for a molecular formula and checks its syntactical correctness. This all takes place in the terminal window.
   Molecule: Si(C3(COOH)2)4(H2O)7
   The formula is syntactically correct
   Molecule: Ag7(FyOj6)17
   Unknown element Fy at line ending Oj6)17
   Molecule: H1SO4
   Too small number at line ending SO4
   Molecule: Ca(OH)NO3
   Missing number at line ending NO3
   Molecule: Na(OH
   Missing right parenthesis at line ending
   Molecule: Nacl
   Missing capital letter at line ending cl
When the user hits return on an empty formula, the program finishes.

Database of elements as a hashtable

The file /info/grudat06/atoms.dat consists of lines of the following type
   Ag  107.868
You shall input the file into a hashtable atomlist with the atom name as the key and the atomic weight as the value, or as expressed in python:
   atomlist["Ag"]="107.868"
A few helpful hints for the personality chemistry application:

Syntax analysis

The syntax for molecular formulas has the following specification in BNF-notation:
<formel>::= <mol> \n
<mol>   ::= <group> | <group><mol>
<group> ::= <atom> |<atom><num> | (<mol>) <num>
<atom>  ::= <LETTER> | <LETTER><letter>
<LETTER>::= A | B | C | ... | Z
<letter>::= a | b | c | ... | z
<num>   ::= 2 | 3 | 4 | ...
Your program is to read the formula character by character and check the the syntas by recursive descent. It is simplest to read the complete line, put its characters in a queue and end with the newline character, like this:
   rad=raw_input("Molecule:)
   for tkn in rad: q.put(tkn)
   q.put("\n")
Recursive descent means that the main program calls readformel(), that readformel() calls readmol() which calls readgroup() and then possibly calls itself, that readgroup() calls either readatom() or reads a parenthesis and calls readmol() etc. -- all according to the syntax. When a violation of syntax is discovered, an exception is generated (raise Exception,msg) which is caught in the main program, where the error message and what is left of the queue are printed.

You often have to peek at the next character to decide which branch of the syntax tree to follow. Therefore your Queue-class should include a peek()-method that returns the first value but leaves the queue unchanged.

Extra tasks for higher marks (choose one)

The molecular weight can be computed while checking the syntax of the formula, if you let all your methods return a number, like this:

Now, the session should look like this.
Molecule:Si(C3(COOH)2)4(H2O)7
The formula is syntactically correct
Molecular weight: 658.466

Write your own hash module and use it instead of python's built-in dictionary. Your main program has
from hash import Hash and the hash module includes the following.

As there is now no has_key, the main program must have try: - - - except Exception: - - -

En jätteprestation av................................. enligt............................ den ...............


Sidansvarig: <henrik@nada.kth.se>
Senast ändrad 24 januari 2007
Tekniskt stöd: <webmaster@nada.kth.se>