Advertisement
hallucinogenic

Exercicio E (quem me dera que tivesse feito)

Jul 8th, 2020
3,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 4.37 KB | None | 0 0
  1. open Printf;;
  2. open Scanf;;
  3. open String;;
  4. open List;;
  5.  
  6. (* Problema E: Null, First e Follow de Gramáticas Algébricas *)
  7.  
  8. let rec explode s =
  9.     match s with
  10.     | "" -> []
  11.     | n -> explode n
  12.  
  13. let rec insert_at_end l i =
  14.     match l with
  15.     | [] -> [i]
  16.     | h::t -> h:: (insert_at_end t i)
  17.  
  18. (*
  19.     Função auxiliar simples para retornar todos os elementos da lista, começando pela cabeça;
  20. *)
  21. let rec print_list_string lista =
  22.     match lista with
  23.     | [] -> ()
  24.     | head::body ->
  25.         begin
  26.             print_endline head;
  27.             print_list_string body
  28.         end;;
  29.  
  30. let isUpperCase c =
  31.     let cInt = int_of_char c in
  32.         if cInt > 64 && cInt < 91 then
  33.             true
  34.         else
  35.             false
  36.  
  37. let findFirst first n producoes c q1 q2 =
  38.     if (isUpperCase c) then
  39.         (*first.[n] <- c;*)
  40.         1
  41.     else
  42.         (*
  43.             Este código é um pseudocódigo do ocaml, contém autismo gravíssimo!
  44.             Por favor consultar um psicólogo depois de rever este código!
  45.  
  46.             BRB, antes que parta o pc!
  47.         *)
  48.         for j = 0 to n-1 do
  49.             let result = (String.get (List.nth producoes j) 2) in
  50.                 if(result == c) then
  51.                     1
  52.  
  53.         done;
  54.  
  55.         0
  56.  
  57.  
  58.  
  59. (*
  60.     Lê recursivamente as linhas correspondentes ao número de produções que foi mencionado anteriormente;
  61.     Começa com o valor de tamLista igual ao número de produções que é suposto preencher na lista.
  62.     Caso o valor seja igual a 0, significa que já leu a última linha pretendida,
  63.         logo retorna a lista que foi armazenada até agora.
  64.     Caso não seja, lê a linha preenchida pelo input, e retorna novamente a função,
  65.         mas com o valor do tamanho das linhas decrementado por 1, tal como incrementa a linha na lista contida no argumento;
  66. *)
  67. let rec leituraLinhas tamLista lista =
  68.     if tamLista = 0 then
  69.         lista
  70.     else
  71.         let linha = read_line() in
  72.             leituraLinhas (tamLista-1) (linha::lista)
  73.  
  74.  
  75. (*
  76.     Função simples para ler os inputs que são pretendidos.
  77.     Neste caso, os inputs correspondem ao seguinte:
  78.         - 1 linha que corresponde ao número "N" de produções para efetuar;
  79.         - "N" linhas que correspondem às produções que vão ser utilizadas;
  80.    
  81.     Exemplo:
  82.         - Input:
  83.             S -> A
  84.             S -> B D e
  85.             A -> A e
  86.             A -> e
  87.             B -> d
  88.             B -> C C
  89.             C -> e C
  90.             C -> e
  91.             C -> _
  92.             D -> a D
  93.             D -> a
  94.  
  95.         - Output:
  96.             NULL(S) = False
  97.             NULL(A) = False
  98.             NULL(B) = True
  99.             NULL(C) = True
  100.             NULL(D) = False
  101.             FIRST(S) = a d e
  102.             FIRST(A) = e
  103.             FIRST(B) = d e
  104.             FIRST(C) = e
  105.             FIRST(D) = a
  106.             FOLLOW(S) = #
  107.             FOLLOW(A) = # e
  108.             FOLLOW(B) = a
  109.             FOLLOW(C) = a e
  110.             FOLLOW(D) = e
  111. *)
  112. let leitura() =
  113.     let nProducoes = sscanf (read_line()) "%d" (fun x->x) in
  114.         let producoes = ref [] in
  115.             producoes := (leituraLinhas nProducoes []);
  116.             (!producoes)
  117.  
  118.  
  119. let main() =
  120.     let producoes = leitura() in
  121.         let first = Array.make  (List.length producoes) "0" in
  122.         let n = 0 in
  123.         let calcFirst = Array.make_matrix (List.length producoes) (100) "!" in
  124.         let calcFollow = Array.make_matrix (List.length producoes) (100) "!" in
  125.             for i = 0 to ((List.length producoes) - 1) do
  126.                 begin
  127.                     let doneLetters = String.make (List.length producoes) 'N' in
  128.                         let c = String.get (List.nth producoes i) 0 in
  129.                         let xxx = ref 0 in
  130.                         let ptr = ref (-1) in
  131.                             for j = 0 to !ptr do
  132.                                 if (c == doneLetters.[j]) then
  133.                                     xxx := 1;
  134.                             done;
  135.  
  136.                             if(!xxx == 0) then
  137.                             begin
  138.                                 print_int(findFirst (calcFirst) (first) (n) (producoes) (c) 0 0);
  139.                                 print_char(c);
  140.                             end
  141.                            
  142.                 end
  143.             done;;
  144.         0
  145.    
  146.  
  147. let _inicio = main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement