mercredi 29 avril 2015

java token recognition program

i am new in java world and i am trying to read data from file then recognize each char token according to a specific grammer then recognize each word token here is my non-complete code but i can't even read chars from text file and recognize the chars tokens i hope to find some help

package compilers_project;
import java.util.*;
import java.io.*;
import java.util.regex.*;
import java.util.function.*;
public class compiler {
public static Scanner scan;
public  static int lines_num=0;
public static void main(String args[]){
    count_lines("E:\\source.txt");
    element elements[][]= new element[lines_num][150];
    elements = read_file("E:\\source.txt");
    for (int i=0;i<lines_num;i++){
        System.out.println(elements[i][3].value);
    }

}


public static void open_file(String path){
    try{
        scan = new Scanner (new File(path));
    }
    catch(Exception ex){
        System.out.print("couldn't find "+ ex.getMessage());    
    }

}

static int count_lines(String path){
    open_file(path);
    int count = 0;
    while (scan.hasNextLine()) {
        count++;
        scan.nextLine();
    }
    scan.close();
    return count;
}
public static token get_token(element e){
    if(Arrays.asList(letters).contains(e.value)){
    return token.letter;    
    }
    else if(Arrays.asList(digits).contains(e.value)){
        return token.digit; 
    }
    else if(Arrays.asList(symbols).contains(e.value)){  
        return token.special_symbol;
    }
    else 
        return token.undefined;
}

public static token get_token(word w){
    if(Arrays.asList(letters).contains(w.value)){
    return token.letter;    
    }
    else if(Arrays.asList(digits).contains(w.value)){
        return token.digit; 
    }
    else if(Arrays.asList(symbols).contains(w.value)){  
        return token.special_symbol;
    }
    else 
        return token.undefined;
} 

public static element[][] read_file(String path){
    open_file(path);
    int lines_num = count_lines(path);
    element array[][]= new element[lines_num][150];
    int count=0,i;
    String line;
    while (scan.hasNext()){
        line =scan.nextLine();
        for(i = 0 ; i< line.length();i++){
            array[count][i].value=line.charAt(i);
            array[count][i].tokn = get_token(array[count][i]);
        }
        count++;
    }   //now we have array of array in which each element(single digit) have value and token

return array;
}

public static boolean is_special_symbol(String s){
    if(s=="or"|| s== "not"|| s=="and"|| s=="<=" ||s==">=" ||s=="==" || s=="!=" || s=="<*" || s=="*>"){  
    return true;    
    }
    return false;
    }

public static word[][] get_words(element e[][])
{
    int c=0,i=0;
    word words[][]=new word[lines_num][100];
    words[0][0]=new word();
    for(i=0;i<lines_num;i++){
        int current_word=0;
        while(e[i][c].value!='\n'){
            while(e[i][c].value!=' ' && e[i][c].value!='\n' ){              
            }
            c++;
            if(e[i][c].value !='\n' && e[i][c].tokn !=token.special_symbol && e[i][c].value !=' ' ){
        words[i][current_word].tok= find_word_tokn(words[i][current_word]);
        current_word++;
        words[i][current_word]=new word();
        }

        }
    }
scan.close();
return words;
}
public static token find_word_tokn(word w){
if(w.value.length()==1){
    return get_token(w);    
}
else if( is_id(w)){
    return token.ID;
}
else if(is_str(w)){
    return token.STR;
}
else if(is_int(w)){
    return token.INT_NUM;
}   
else if(is_float(w)){
    return token.FLOAT_NUM;
}
else 
    return token.undefined;

} 

public static boolean is_str(word w){
    for (int i=0;i< w.elements_tok.length;i++){
        if(w.elements_tok[i] ==token.digit || w.elements_tok[i] == token.digit || w.value.charAt(i)=='_' ){
        //go on 
        }
        else{
        return false;   
        }   
    }
    return true;
}

public static boolean is_id(word w){
    if(w.elements_tok[0]==token.letter && is_str(w)){
        return true;
    }

    else
        return false;
}

public static boolean is_float(word w){
    int count=0;
    for (int i=0;i< w.elements_tok.length;i++){
        if(w.elements_tok[i] ==token.digit|| w.value.charAt(i)=='.' ){
        //go on 
        if(w.value.charAt(i)=='.'){
            count++;
        }
        }
        else if(i==0 && (w.value.charAt(0)=='+'|| w.value.charAt(0)=='-' ))
        {
            //go on
        }
        else{
        return false;   
        }   
    }
    if(count <2){
    return true;
    }
    return false;
}

public static boolean is_int(word w){
    for (int i=0;i< w.elements_tok.length;i++){
        if(w.elements_tok[i] ==token.digit){
        //go on 
        }
        else if(i==0 && (w.value.charAt(0)=='+'|| w.value.charAt(0)=='-' ))
        {
            //go on
        }
        else{
        return false;   
        }   
    }
    return true;


}
}

Aucun commentaire:

Enregistrer un commentaire