Hey there good people. well i have implemented the whole code of compiler phase 1. Have implemented al the DFAs and most are working fine. But this comments DFA is giving me grief. What it should do is, it should ignore anything written between { } . but it's not doing it. What it does is remove the first character after { and that's it. I am posting the code snippet here. Any kind of help will be greatly appreciated!

e.g.. it should ignore:

{integer program var INTEGER PROGRAM
dfjdfj dhvdfhvk dufhvkhdfv
dkvhnfdk
fkhvkdf
}

but what it does is this:

nteger,      Identifier //Notice the missing I 
program      Reserved words
var      Reserved words
INTEGER,      Identifier
program      Reserved words
dfjdfj,      Identifier
dhvdfhvk,      Identifier
dufhvkhdfv,      Identifier
dkvhnfdk,      Identifier
fkhvkdf,      Identifier

This is the DFA.

        dfa1[0][0]=0;
        dfa1[1][1]=2;
        dfa1[1][2]=-1;
        dfa1[2][1]=-1;
        dfa1[2][2]=3;
        dfa1[3][1]=-1;
        dfa1[3][2]=-1;

This is the code i have implemented:
all the initializations etc have been done. This is just the part of whole phase 1.

dfavalue=1;
            accept1=0;
            while(dfavalue!=-1){
                if(tokenclass.chartoken(t)=='{'){
                    dfavalue=dfa1[dfavalue][1];           
                    if(dfavalue!=-1){
                        acceptedtoken1[accept1]=tokenclass.chartoken(t);
                        accept1++;
                        t++;
                    }
                }
                if(tokenclass.chartoken(t)!='}'){
                    dfavalue=dfa1[dfavalue][2];           
                        if(dfavalue!=-1){
                            t++;
                        }
                }
                if(tokenclass.chartoken(t)=='}'){
                    dfavalue=dfa1[dfavalue][2];                  
                        if(dfavalue==3 ){
                            acceptedtoken1[accept1]='}';
                            accept1++;                        
                            acceptedtoken1[accept1]=',';
                            accept1++;
                            for(int j=0;j<accept1;j++){
                                out.print(acceptedtoken1[j]);
                               if(acceptedtoken1[j]==','){
                                   out.println("  Comments");
                               }
                            }
                        }
                        else{
                            errortoken[error]=tokenclass.chartoken(t);
                            error++;
                         }                         
                         dfavalue=-1;
                         accept1=0;
                }
            }

        //End implementation Question 1

Any kind of help is greatly appreciated.
Thanks!

Member Avatar for iamthwee

You need to shove it onto the stack so you can push and pop the curly brackets.

Well, your DFA is unknown to others. In other words, I am not sure what you are doing in each of the DFA state.

Anyway, there is a questionable call tokenclass.chartoken(t) which gives no information to us. Does the method return and consume the character which is being looked at? It looks like it does, but I am still not sure.

Also, you need to explain/comment your code!. I am not sure why you are using a while loop to check for dfavalue=-1 because you assign the value to -1 at the end inside the while loop anyway?

I thought what you need to do is to iterate through each character of a given string. Then parse whatever you need to parse. In other words, you need to write your own parser first. Is that what you are trying to do?

PS: Looking at your result... Is String PROGRAM equal to program? The reason I ask because you treat String INTEGER to be different from integer (which is not uniform with program & PROGRAM).

Can you explain a bit what you code does any why you think it should do more than discarding one character after it sees a {?

You need to shove it onto the stack so you can push and pop the curly brackets.

DFAs don't have stacks, so I assume the comments are supposed to be non-nested (which is pretty standard in programming languages).

Sorry people i didn't explain before.. i could have posted the whole code but it's like 650 lines of code so it would have been a problem for anyone to go through. It needs to ignore EVERYTHING between curly braces. and i put while loop to check because -1 means that the state is blank, so i want to run the code only when the state is not blank... I'll post the tokenclass code too..

private static class Tokenclass {
            int i=0, q=0, l=0;
    FileReader fr;
    BufferedReader rr;
    ArrayList<Character> tokench= new ArrayList<Character>();
    String tokenfile[]=new String[100];

    public Tokenclass(){
        try{
            fr=new FileReader("input.txt");
            rr=new BufferedReader(fr);
            String s=rr.readLine();
         while(s!=null){
            StringTokenizer st2 = new StringTokenizer(s); 
            while (st2.hasMoreElements()) {
               tokenfile[i]=st2.nextToken();
                i++;
                }
            s=rr.readLine();
           }
        fr.close();
    rr.close();
        }        
        catch(IOException ex){
    System.out.println(ex);
        }//input file end
    }
        public String stringtoken(int e){
            return tokenfile[e];
            }
        public void tokenize(){
        for(int j=0;j<i;j++){
           l=tokenfile[j].length();
           for(int k=0;k<l;k++){
               tokench.add(q,tokenfile[j].charAt(k));
               q++;}
           tokench.add(q,'~');
           q++;}}
        public char chartoken(int t){return tokench.get(t);}
    int tokenlenght(){
        return q;
    }}}

My DFA works like this.. on

[1][1] there is a {  

on

    [2][2] there is a }

and the rest are blank (it's an implementation of transition table)

int dfa1[][] = new int[4][3]; // my DFA transition table's 2D array
char acceptedtoken1[]=new char[100]; //acceptedtoken1 char array


and tokenclass.chartoken(t) == {  //means if the chartoken(t) is found to be { then execute the following part of code

and yes i am using stack and queue but only in parsing phase. Don't really need to use it here.

Can you explain something for me please?
I thought the transition table for a DFA looks somnething like

table[current state][next token] = next state

so conceptually I'd expect to see something like

// state n is inside a a comment, n+1 is normal parsing
table[*]['{'] = n;    // { takes you to state n from anywhere
table[n]['}'] = n+1;  // } takes you out
table[n][*]   = n;    // anything else leaves you in state n

yet you have a nx3 array, and it contains values like -1

I never did compiler design at college, so maybe I'm just missing the point, but if someone can explain this for me I'd be grateful.

thanks for the input guys but i have fixed it. was missing some states and that. Thanks again all :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.