java - Possible to 'look ahead' in a StreamTokenizer? -
working on parser, , if detect keyword want call function , send next token (we can assume input proper , correct).
what tried below not work because cannot dereference int.
what trying:
boolean eof = false; do{ int = 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)); // ahead } break; case streamtokenizer.tt_number: system.out.println("number: " + st.nval); break; case '\'': system.out.println("quoted value " + 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 st.nexttoken()
returns int
, hence compile error on st.nexttoken().sval
. want instead:
system.out.println("---start----"); st.nexttoken(); system.out.println(start(st.sval));
of course, assumes nexttoken()
returned '; more robust do:
system.out.println("---start----"); if(st.nexttoken() == '\'') { system.out.println(start(st.sval)); } else { // handle error system.err.println("expected 'string' after start"); }
Comments
Post a Comment