interprete_rpc.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyrigth Rodrigo Garcia strysg@riseup.net
  2. # This program is free software; you can redistribute it and/or modify it
  3. # under the terms of the GNU General Public Licence as published
  4. # by the Free Software Foundation; either version 3 of the Licence,
  5. # or (at your option) any later version.
  6. # Shutter is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. from automatas import automata_cadenas
  13. # declarando automatas reglas de transcision de automatas
  14. d_mostrar = {
  15. 0 : [("mostrar +",1)],
  16. 1 : [("[a-zA-Z][a-zA-Z0-9_]*",6), ('\"',2)],
  17. 2 : [('\w\"', 4)],
  18. 4 : [(r".",5)],
  19. 6 : [(r".",5)],
  20. }
  21. d_declarar_entero = {
  22. 0 : [("entero +",1)],
  23. 1 : [("[a-zA-Z][a-zA-Z0-9]*",2)],
  24. # 2 : [("= +|( =)| = |=",3)],
  25. 2 : [(" *= *",3)],
  26. 3 : [("[0-9]+"),4],
  27. 4 : [(r".",5)],
  28. }
  29. print("Interprete de lenguaje rpc")
  30. print("--------------------------")
  31. print("Escriba sus instrucciones a continuacion")
  32. while True:
  33. m1 = automata_cadenas(d_mostrar, [5])
  34. m2 = automata_cadenas(d_declarar_entero, [5])
  35. entrada = input("--: ")
  36. # evaluando
  37. m1.FTrans(entrada)
  38. m2.FTrans(entrada)
  39. # resultado de la evaluacion
  40. print("--- eval mostrar ---")
  41. if m1.aceptado():
  42. print ("--- recorridos:"+str(m1.estados_recorridos))
  43. print ("--- aceptado!")
  44. # traducir
  45. else:
  46. print ("--- recorridos:"+str(m2.estados_recorridos))
  47. print ("--- final:"+ str(m2.estado))
  48. print ("")
  49. print("--- eval 'declarar entero'")
  50. if m2.aceptado():
  51. print("--- recorridos: "+str(m2.estados_recorridos))
  52. print("--- aceptado!")
  53. else:
  54. # errores
  55. print("--- recorridos: "+str(m2.estados_recorridos))
  56. print("--- final:"+str(m2.estado))
  57. print("------------------")