Reputation: 51
I have the following grammar:
gui ::=
Window String widgets
widgets ::=
Button STRING ';'|
radio_button End ';'
radio_button ::=
Radio String ';'
gui, widgets and radio_button are non-terminal. Others are terminal.
The statement I need to parse is:
Window "test" Radio "r1";
I use streamtokenizer in java to do the parsing. When it comes to widgets, I need to test the first line and then the second line. I use nextToken() to get the tokens of the statement. I want to save a copy of the tokenizer before I parse widgets so that if the first line fails, I can use the copy to parse the second line. How to save the copy?
Thanks a lot. I really appreciate your help.
Upvotes: 0
Views: 413
Reputation: 31300
The StreamTokenizer
has a method pushBack
which restores the tokenizer's state so that you can call nextToken()
again, without skipping the token not matched on the first try.
if( st.nextToken() == st.TT_WORD && "Button".equals( st.sval ){
// deal with Button STRING ';'
} else {
st.pushBack();
if( radioButton() ){ // call method to recognize Radio String ';'
// handle End ;
// ...
} else {
// error handling
}
}
boolean radioButton(){
if( st.nextToken() == st.TT_WORD && "Radio".equals( st.sval ){
// continue to match String ';'
// ...
return true;
} else {
st.pushBack();
return false;
}
}
Upvotes: 1