Advertisement
Guest User

Untitled

a guest
May 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. from linkedQFile import *
  2. import unittest
  3. import string
  4. import sys
  5. import re
  6.  
  7. # <formel>::= <mol> \n
  8. # <mol>   ::= <group> | <group><mol>
  9. # <group> ::= <atom> |<atom><num> | (<mol>) <num>
  10. # <atom>  ::= <LETTER> | <LETTER><letter>
  11. # <LETTER>::= A | B | C | ... | Z
  12. # <letter>::= a | b | c | ... | z
  13. # <num>   ::= 2 | 3 | 4 | ...
  14.  
  15. #Eventuell kolla tom input
  16. def formel_check(q):
  17.     mol_check(q)
  18.     if q.peek() != '#':
  19.         raise SyntaxError('Felaktig gruppstart')
  20.  
  21. def mol_check(q):
  22.     if q.peek() is '(' or check(q.peek()):
  23.         group_check(q)
  24.         mol_check(q)
  25.     return
  26.  
  27. def group_check(q):
  28.     if q.peek() is '#':
  29.         return
  30.  
  31.     if q.peek() is '(': #----------------------     <(mol)>
  32.         q.dequeue()
  33.         if q.peek() is ')':
  34.             raise SyntaxError('gg')
  35.         mol_check(q)
  36.         if q.peek() is not ')':
  37.             raise SyntaxError('gg')
  38.         q.dequeue()
  39.         if not is_num(q.peek()):
  40.             raise SyntaxError('saknar siffra')
  41.         number_check(q)
  42.         return #-------------------------------     <(mol)>
  43.  
  44.     atom_check(q) #----------------------------     <atom>
  45.     number_check(q) #--------------------------     <atom><number>
  46.     return
  47.  
  48. def atom_check(q):
  49.     if is_num(q.peek()):
  50.         raise SyntaxError('Felaktig gruppstart')
  51.     if not Letter_check(q.peek()):
  52.         raise SyntaxError('Felaktig Bokstav')
  53.     atom=q.dequeue() #------------------------      <A>
  54.     if letter_check(q.peek()):
  55.         a=q.dequeue()
  56.         atom+=a #-----------------------------      <Aa>
  57.     if atom in atomlista:
  58.         return
  59.     else:
  60.         raise SyntaxError('Felaktig atom')
  61.  
  62. def number_check(q):
  63.     if is_num(q.peek()):
  64.         num = q.dequeue()
  65.         num_collect=num
  66.         if num is '0':
  67.             raise SyntaxError('Bitch')
  68.         num=q.peek()
  69.         if q.isEmpty() is False:
  70.             while is_num(num) is True:
  71.                 num_collect += q.dequeue()
  72.                 if q.isEmpty() is True:
  73.                     break
  74.                 num=q.peek()
  75.         if num_collect is '1':
  76.             raise SyntaxError('Bitch')
  77.  
  78. def Letter_check(l):
  79.     if l is not None:
  80.         if l in string.ascii_uppercase:
  81.             return True
  82.     return False
  83.  
  84. def letter_check(l):
  85.     if l is not None:
  86.         if l in string.ascii_lowercase:
  87.             return True
  88.     return False
  89.  
  90. def is_num(n):
  91.     # print(n, 'kommer in i is_num')
  92.     if n is not None:
  93.         if re.search('^[0-9]$', n):
  94.             # print(n, 'passerade')
  95.             return True
  96.     # print(n, 'kuggades')
  97.     return False
  98.  
  99. def check(char):
  100.     if not (letter_check(char) or Letter_check(char) or is_num(char)):
  101.         return False
  102.     else:
  103.         return True
  104.  
  105. def rest_of_list(q):
  106.     remaining=""
  107.     while q.isEmpty() is False:
  108.         remaining += q.dequeue()
  109.     return remaining.strip('#')
  110.  
  111. def que(lista):
  112.     if len(lista) > 0:
  113.         q=LinkedQ()
  114.         for i in lista:
  115.             q.enqueue(i)
  116.         return q
  117.     else:
  118.         raise SyntaxError("Tom input")
  119.  
  120. def controll(test):
  121.     formel_check(test)
  122.     return True
  123.  
  124.  
  125. if __name__ == '__main__':
  126.     atomlista = ["H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y"," Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U"," Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr","Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Fl","Lv"]
  127.    
  128.     for line in sys.stdin:
  129.         input_line=line.strip('\n')
  130.         if input_line is "#":
  131.             break
  132.         try:
  133.             q=que(str(input_line+'#'))
  134.             if controll(q) is True:
  135.                 print("Formeln รคr syntaktiskt korrekt")
  136.         except SyntaxError as fel:
  137.             error_message=str(fel) + ' vid radslutet ' + rest_of_list(q)
  138.             print(error_message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement