Jump to content


Photo

Tokenizer De Código-Fonte


  • Faça o login para participar
Nenhuma resposta neste tópico

#1 Giovanna Cóppola

Giovanna Cóppola

    Normal

  • Usuários
  • 116 posts
  • Sexo:Feminino
  • Localidade:São Paulo

Posted 05/03/2012, 17:23

Pessoal,

Estou estudando Java por conta própria e estava lendo agora há pouco sobre tokenizers.

Encontrei o seguinte código:

import java.io.*; 
import java.util.*;

public class TokenizingJavaSourceCode{
  //public static final int TT_EOL; 
  public static void main(String[] args) throws IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Please enter a java file name: ");
  String filename = in.readLine();
  if(!filename.endsWith(".java")){
  System.out.println("This is not a java file.");
  System.exit(0);
  }
  File javaFile = new File(filename);
  if(javaFile.exists()){
  FileReader file = new FileReader(filename);
  StreamTokenizer streamTokenizer = new StreamTokenizer(file);
  // It will go through the file and 
  //gives the number of tokens in the file
  int i=0;
  int numberOfTokensGenerated = 0;
  while(i != StreamTokenizer.TT_EOF){
  i = streamTokenizer.nextToken();
  numberOfTokensGenerated++;
  }
  System.out.println("Number of tokens = " + numberOfTokensGenerated);
  }
  else{
  System.out.println("File does not exist!");
  System.exit(0);
  }
  }
}

Pelo o que entendi, ele vai carregar um arquivo .java e mostrar o número de tokens na tela, certo? É possível ele ler um outro tipo de código fonte, por exemplo, de um arquivo C ou Pascal, e mostrar os tokens também? Se eu alterar alguns detalhes no código, é claro. E se eu quisesse, por exemplo, que ele gerasse um arquivo .txt com o token (não somente o número de tokens encontrados, mas o resultado mesmo), como eu faria?

Obrigada desde já,
Giovanna Cóppola
Pandartt | Redação SEO, Marketing Digital, Sites e Fotos




1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq