Advertisement
jsprieto10

Finite Automaton

May 1st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. // -----------------------
  2. //
  3. // UNIVERSIDAD DE LOS ANDES
  4. //Juan Sebastian Prieto
  5. // -----------------------
  6.  
  7. using gold.**
  8.     var posCadena:int
  9.     var longCadena:int
  10. procedure main(args:String[]) begin
  11.     posCadena := 0
  12.     longCadena := 0
  13.     GAutomataFrame.show(createTransducer())
  14.     testTransducer(createTransducer())
  15. end
  16. procedure testTransducer(M:ITransducer) begin
  17.     var sc:java.util.Scanner(System.$in)
  18.     while true do
  19.         print "----------------------------"
  20.         print "Digite la cadena de entrada:"
  21.         string:=sc.nextLine()
  22.         result:=M.acceptsString(string)
  23.         print "La cadena fue "+(result?"aceptada":"rechazada")+". Resultado:"
  24.         print M.getOutputString()
  25.     end
  26. end
  27. function createTransducer():ITransducer begin
  28.         //Definicion de un estado= ((longitudCadena%10)X(posCadena%3)X(Sigma_1))
  29.     Q:={"inic", "error","v", "nombre", "variable", "n", "q", "q-", "<",
  30.     "x", "y", "z", "f", "simbolo", "espacio", "a", "numero", "<-",
  31.     "fa", "fai", "fail", "i", "is", "isV","isVa", "isVar", "isA",
  32.     "isAt", "isAto", "isAtom", "isN", "isNu", "isNum", "isNum",
  33.     "isNumb", "isNumbe", "isNumber"}
  34.     Σ:=('a''z')('A''Z')('0''9'){' ','.','(' ,')','[',']','?','-',',','!','<',':','|' }
  35.     Σ´:=Σ
  36.     q₀:="inic"
  37.     F:=Q
  38.     return  GDeterministicTransducer(Q,Σ,Σ´,q₀,F,δ,g,h)
  39. end
  40. //Funcion de inicio de automata
  41. //(estado, símbolo leído.)
  42. function δ(q,σ) begin
  43.     if q = "error" then
  44.         return "error"
  45.     elseif σ = 'i' && q!= "fa" then
  46.         return "i"
  47.     elseif (σ = 's' && q = "i") then
  48.         return "is"
  49.     elseif (σ = 'V' && q = "is") then
  50.         return "isV"
  51.     elseif (σ = 'a' && q = "isV") then
  52.         return "isVa"
  53.     elseif (σ = 'r' && q = "isVa") then
  54.         return "isVar"
  55.     elseif (σ = 'A' && q = "is") then
  56.         return "isA"
  57.     elseif (σ = 't' && q = "isA") then
  58.         return "isAt"
  59.     elseif (σ = 'o' && q = "isAt") then
  60.         return "isAto"
  61.     elseif (σ = 'm' && q = "isAto") then
  62.         return "isAtom"
  63.     elseif (σ = 'N' && q = "is") then
  64.         return "isN"
  65.     elseif (σ = 'u' && q = "isN") then
  66.         return "isNu"
  67.     elseif (σ = 'm' && q = "isNu") then
  68.         return "isNum"
  69.     elseif (σ = 'b' && q = "isNum") then
  70.         return "isNumb"
  71.     elseif (σ = 'e' && q = "isNumb") then
  72.         return "isNumbe"
  73.     elseif (σ = 'r' && q = "isNumbe") then
  74.         return "isNumber"
  75.     elseif σ = 'f' then
  76.         return "f"
  77.     elseif (σ = 'a' && q = "f") then
  78.         return "fa"
  79.     elseif (σ = 'i' && q = "fa") then
  80.         return "fai"
  81.     elseif (σ = 'l' && q = "fai") then
  82.         return "fail"
  83.     elseif (σ = '?') then
  84.         return "q-"
  85.     elseif (σ = '-' && q = "q-") then
  86.         return "q"
  87.     elseif (σ ∈('a''z') && q ∉ {"nombre", "variable", "a", "v"} ) then
  88.             return "nombre"
  89.     elseif (σ ∈('a''z')('A''Z')('0''9') && q = "nombre") then
  90.             return "a"
  91.     elseif (σ ∈('a''z')('A''Z')('0''9') && q = "a") then
  92.             return "a"
  93.     elseif (σ ∈('A''Z') && q != "variable") then
  94.         return "variable"
  95.     elseif (σ ∈('a''z')('A''Z')('0''9') && q = "variable") then
  96.             return "v"
  97.     elseif (σ ∈('a''z')('A''Z')('0''9') && q = "v") then
  98.             return "v"
  99.     elseif (σ =' ') then
  100.             return "espacio"
  101.     elseif (σ ∈ {'.','(',')','[',']',',','|','!'}) then
  102.             return "simbolo"
  103.     elseif q="numero" then
  104.         return "numero"
  105.     elseif (σ ∈ ('0''9') && q!= "numero") then
  106.         return "numero"
  107.     elseif (σ = ':') then
  108.         return "<-"
  109.     elseif (σ = '-' && q="<-") then
  110.         return "<"
  111.     else
  112.     return "error"
  113. end
  114. end
  115.  
  116.  
  117. function g(q) begin
  118.     if q = "nombre" then
  119.         return "a"
  120.     elseif q="variable" then
  121.         return "v"
  122.     elseif (q="a") then
  123.         return λ
  124.     elseif (q="v") then
  125.         return λ
  126.     elseif q = "q" then
  127.         return "q"
  128.     elseif q="espacio" then
  129.         return λ
  130.     elseif q="numero" then
  131.         return λ
  132.     elseif q="<" then
  133.         return "<"
  134.     elseif q="isVar" then
  135.         return "x"
  136.     elseif q="isAtom" then
  137.         return "y"
  138.     elseif q="isNumber" then
  139.         return "z"
  140.     elseif q="fail" then
  141.         return "f"
  142.     else
  143.         return λ
  144.     end
  145. end
  146.  
  147.  
  148. function h(q,σ) begin
  149.     if (σ ∈{'.','(',')','[',']',',','|','!'}) then
  150.         return σ
  151.     elseif (σ ∈ ('0''9') && q != "numero") then
  152.         return "n"
  153.     else
  154.         return λ
  155.     end
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement