Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Printf;;
- open Scanf;;
- open String;;
- open List;;
- (* Problema E: Null, First e Follow de Gramáticas Algébricas *)
- let rec explode s =
- match s with
- | "" -> []
- | n -> explode n
- let rec insert_at_end l i =
- match l with
- | [] -> [i]
- | h::t -> h:: (insert_at_end t i)
- (*
- Função auxiliar simples para retornar todos os elementos da lista, começando pela cabeça;
- *)
- let rec print_list_string lista =
- match lista with
- | [] -> ()
- | head::body ->
- begin
- print_endline head;
- print_list_string body
- end;;
- let isUpperCase c =
- let cInt = int_of_char c in
- if cInt > 64 && cInt < 91 then
- true
- else
- false
- let findFirst first n producoes c q1 q2 =
- if (isUpperCase c) then
- (*first.[n] <- c;*)
- 1
- else
- (*
- Este código é um pseudocódigo do ocaml, contém autismo gravíssimo!
- Por favor consultar um psicólogo depois de rever este código!
- BRB, antes que parta o pc!
- *)
- for j = 0 to n-1 do
- let result = (String.get (List.nth producoes j) 2) in
- if(result == c) then
- 1
- done;
- 0
- (*
- Lê recursivamente as linhas correspondentes ao número de produções que foi mencionado anteriormente;
- Começa com o valor de tamLista igual ao número de produções que é suposto preencher na lista.
- Caso o valor seja igual a 0, significa que já leu a última linha pretendida,
- logo retorna a lista que foi armazenada até agora.
- Caso não seja, lê a linha preenchida pelo input, e retorna novamente a função,
- mas com o valor do tamanho das linhas decrementado por 1, tal como incrementa a linha na lista contida no argumento;
- *)
- let rec leituraLinhas tamLista lista =
- if tamLista = 0 then
- lista
- else
- let linha = read_line() in
- leituraLinhas (tamLista-1) (linha::lista)
- (*
- Função simples para ler os inputs que são pretendidos.
- Neste caso, os inputs correspondem ao seguinte:
- - 1 linha que corresponde ao número "N" de produções para efetuar;
- - "N" linhas que correspondem às produções que vão ser utilizadas;
- Exemplo:
- - Input:
- S -> A
- S -> B D e
- A -> A e
- A -> e
- B -> d
- B -> C C
- C -> e C
- C -> e
- C -> _
- D -> a D
- D -> a
- - Output:
- NULL(S) = False
- NULL(A) = False
- NULL(B) = True
- NULL(C) = True
- NULL(D) = False
- FIRST(S) = a d e
- FIRST(A) = e
- FIRST(B) = d e
- FIRST(C) = e
- FIRST(D) = a
- FOLLOW(S) = #
- FOLLOW(A) = # e
- FOLLOW(B) = a
- FOLLOW(C) = a e
- FOLLOW(D) = e
- *)
- let leitura() =
- let nProducoes = sscanf (read_line()) "%d" (fun x->x) in
- let producoes = ref [] in
- producoes := (leituraLinhas nProducoes []);
- (!producoes)
- let main() =
- let producoes = leitura() in
- let first = Array.make (List.length producoes) "0" in
- let n = 0 in
- let calcFirst = Array.make_matrix (List.length producoes) (100) "!" in
- let calcFollow = Array.make_matrix (List.length producoes) (100) "!" in
- for i = 0 to ((List.length producoes) - 1) do
- begin
- let doneLetters = String.make (List.length producoes) 'N' in
- let c = String.get (List.nth producoes i) 0 in
- let xxx = ref 0 in
- let ptr = ref (-1) in
- for j = 0 to !ptr do
- if (c == doneLetters.[j]) then
- xxx := 1;
- done;
- if(!xxx == 0) then
- begin
- print_int(findFirst (calcFirst) (first) (n) (producoes) (c) 0 0);
- print_char(c);
- end
- end
- done;;
- 0
- let _inicio = main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement