Guest User

Untitled

a guest
Jun 18th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3.  
  4. #############################################
  5. ## Trabalho de Linguagens Formais - MATA50 ##
  6. ## Professora Lais Salvador - 2009.1 ##
  7. ## Kessia Pinheiro - kessia@dcc.ufba.br ##
  8. ## Parte 01 - Análise sintática de regex ##
  9. ## Para executar: python main.py ##
  10. ## Na execucao sao mostradas as instrucoes ##
  11. #############################################
  12.  
  13. import sys
  14.  
  15. class ParserOp:
  16. def __init__(self):
  17. """
  18. Definition of symbols accepted
  19. """
  20. self.alpha = ['a','b','c','d','e',
  21. 'f','g','h','i','j',
  22. 'k','l','m','n','o',
  23. 'p','q','r','s','t',
  24. 'u','v','w','x','y','z']
  25. self.num = ['0','1','2','3','4',
  26. '5','6','7','8','9']
  27. self.symbol = ['(',')', #delimiter
  28. '|', #choice between alternatives
  29. '*', #zero or more
  30. '?', #zero or one
  31. '+'] #one or more
  32. self.alphanum = self.alpha + self.num
  33.  
  34. def verifysymbol(self, alpha=[], regex=[]):
  35. stack = 0
  36. for pos, char in enumerate(regex):
  37. if char in alpha and char in self.alphanum:
  38. continue
  39. #print char, " pertence ", alpha
  40. elif char in self.symbol:
  41. #Parsing parenthesis
  42. #Need check if each parenthesis are closed
  43. #print char, " eh um simbolo"
  44. if char == '(' and regex[pos+1] != ')':
  45. stack += 1
  46. elif char == ')':
  47. if stack >= 0:
  48. stack -= 1
  49. #Parsing asterisc or plus
  50. #Each multipicator are permited only after a char or a right closed expression
  51. elif char == '*' or char == '+' or char == '?':
  52. symbols_permited = [')', '(']
  53. if regex[pos-1] in self.alphanum or regex[pos-1] in symbols_permited[0]:
  54. continue
  55. elif regex[pos-1] in self.alphanum and regex[pos+1] in symbols_permited[1]:
  56. continue
  57. else:
  58. #print char, " nao permitido"
  59. return False
  60. #Parsing pipe
  61. #Pipe are permited if predecessor are a closed parenthesis, char or multiplicator
  62. #and predecessor are a parenthesis or char
  63. elif char == '|':
  64. parenthesis = [')', '(']
  65. symbols_permited = ['*', '+', '?']
  66. if regex[pos-1] in self.alphanum and regex[pos+1] in self.alphanum:
  67. continue
  68. elif regex[pos-1] in parenthesis[0] and regex[pos+1] in parenthesis[1]:
  69. continue
  70. elif regex[pos-1] in self.alphanum and regex[pos+1] in parenthesis[1]:
  71. continue
  72. elif regex[pos-1] in parenthesis[0] and regex[pos+1] in self.alphanum:
  73. continue
  74. elif regex[pos-1] in symbols_permited and regex[pos+1] in self.alphanum:
  75. continue
  76. elif regex[pos-1] in symbols_permited and regex[pos+1] in parenthesis[1]:
  77. continue
  78. else:
  79. #print char, " nao permitido"
  80. return False
  81. else:
  82. #print char, " nao pertence ", alpha
  83. return False
  84. if stack == 0:
  85. #print "parsing correto"
  86. return True
  87. else:
  88. #print "parsing errado"
  89. return False
  90.  
  91. class ProcessIn:
  92. def __init__ (self):
  93. self.inputlist = []
  94.  
  95. def readInput(self, input=[]):
  96. try:
  97. input = input.readline().split()[0]
  98. for char in input:
  99. self.inputlist.append(char)
  100. return True
  101. except:
  102. return False
  103.  
  104. def getInput(self):
  105. return self.inputlist
  106.  
  107. def isEmpty(self):
  108. return (self.inputlist == [])
  109.  
  110. class Input:
  111. def __init__(self):
  112. """
  113. Definition of symbols accepted
  114. """
  115. self.alpha = ['a','b','c','d','e',
  116. 'f','g','h','i','j',
  117. 'k','l','m','n','o',
  118. 'p','q','r','s','t',
  119. 'u','v','w','x','y','z']
  120. self.num = ['0','1','2','3','4',
  121. '5','6','7','8','9']
  122. self.symbol = ['(',')', #delimiter
  123. '|', #choice between alternatives
  124. '*', #zero or more
  125. '?', #zero or one
  126. '+'] #one or more
  127. self.alphanum = self.alpha + self.num
  128.  
  129. def receiveIn(self):
  130. self.alfabeto = ProcessIn()
  131. self.regex = ProcessIn()
  132. print "insira o alfabeto: "
  133. if self.alfabeto.readInput(sys.stdin):
  134. for char in self.alfabeto.getInput():
  135. if char in self.alphanum:
  136. continue
  137. else:
  138. print "alfabeto com simbolos nao permitidos"
  139. return True
  140. #print "alfabeto: ", self.alfabeto.getInput()
  141. print "insira a expressao regular: "
  142. if self.regex.readInput(sys.stdin):
  143. for char in self.regex.getInput():
  144. if char in self.alphanum or char in self.symbol:
  145. continue
  146. else:
  147. print "regex com simbolos nao permitidos"
  148. return True
  149. #print "regex: ", self.regex.getInput()
  150. return True
  151. else:
  152. print "alfabeto em branco"
  153. print "Saindo..."
  154. return False
  155.  
  156.  
  157. if __name__ == "__main__":
  158. empty = '$' #cadeia vazia
  159. input = Input()
  160. print "----------------------------------"
  161. print "Trabalho de Linguages Formais e Automatos"
  162. print "Professora Lais Salvador - MATA50 - Semestre 2009.1"
  163. print "Aluna: Kessia Pinheiro - Matricula: 200522206"
  164. print "----------------------------------"
  165. print
  166. print "Instrucoes:"
  167. print
  168. print "O alfabeto deve ser uma string seguida e somente aceita caracteres de a-z e numeros"
  169. print "A expressao regular segue os seguintes operadores:"
  170. print "| - escolha entre duas alternativas"
  171. print "* - repeticao de zero ou mais vezes"
  172. print "? - repeticao de zero ou uma vez"
  173. print "+ - repeticao de uma ou mais vezes"
  174. print
  175. print "Exemplo de entrada correta:"
  176. print "alfabeto: abcdefghi"
  177. print "expressao regular: a*|b*ab*|c*ab*ab*|d*ab*eb*ab*a(g|b)*"
  178. print
  179. print "Para entrada a partir de arquivo use: python main.py < entrada.txt"
  180. print "O programa pede entradas ate que o alfabeto seja em branco"
  181. print "----------------------------------"
  182. print
  183. while input.receiveIn():
  184. process = ParserOp()
  185. if not input.alfabeto.isEmpty() and not input.regex.isEmpty():
  186. if process.verifysymbol(input.alfabeto.getInput(), input.regex.getInput()):
  187. print "------------------------------------------"
  188. print "alfabeto:", "{", ",".join(map(str, input.alfabeto.getInput())), "}"
  189. print "expressao regular:", "".join(input.regex.getInput())
  190. print '- expressao regular sintaticamente correta.'
  191. print "------------------------------------------"
  192. else:
  193. print "----------------------------"
  194. print '- expressao regular incorreta'
  195. print "----------------------------"
Add Comment
Please, Sign In to add comment