简体   繁体   中英

Possible to 'look ahead' in a StreamTokenizer?

Working on a parser, and if I detect a certain keyword I want to call a function and send it the next token (we can assume all input is proper and correct).

What I tried below does not work because we cannot dereference an int.

What I am trying:

        boolean eof = false;
        do{
            int i = 0;
            int token = st.nextToken();
            switch (token){
                case StreamTokenizer.TT_EOF:
                    System.out.println("EOF");
                    eof = true;
                    break;
                case StreamTokenizer.TT_EOL:
                    System.out.println("EOL");
                    break;
                case StreamTokenizer.TT_WORD:
                    //System.out.println("Word: " + st.sval);
                    if (st.sval.equals("start")){
                        System.out.println("---START----");
                        System.out.println(start(st.nextToken().sval)); // look ahead
                    }
                    break;
                case StreamTokenizer.TT_NUMBER:
                    System.out.println("Number: " + st.nval);
                    break;
                case '\'':
                    System.out.println("Quoted Value is " + st.sval);
                    break;
                default:
                    System.out.println((char) token + " encountered.");
                    break;

            }
        } while (!eof);

    } catch (Exception ex){
        ex.printStackTrace();
    }
}

public static String start(String in){
    String out = "<start> " + in + " </start>";
    return out;
}

Input

start 'BEGIN HERE' 

Desired Output

----START----
<start> BEGIN HERE </start>

The call to st.nextToken() returns an int , hence the compile error on st.nextToken().sval . You want to do something like this instead:

System.out.println("---START----");
st.nextToken();
System.out.println(start(st.sval));

Of course, this just assumes that nextToken() returned '; it would be more robust to do:

System.out.println("---START----");
if(st.nextToken() == '\'') {
   System.out.println(start(st.sval));
} else {
   // Handle error
   System.err.println("Expected 'string' after start");
}

You should call st.nextToken() in a different step. After the call the sval will be available.

if (st.sval.equals("start")){
    System.out.println("---START----");
    st.nextToken();
    System.out.println(start(st.sval)); // look ahead
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM