Java Reference
In-Depth Information
}
}
while(have(LBRACK)){
if(!have(RBRACK)){
scanner.returnToPosition();
returnfalse;
}
}
if(!have(RPAREN)){
scanner.returnToPosition();
returnfalse;
}
scanner.returnToPosition();
returntrue;
}
Here, seeCast() must look ahead in the token stream to consider a sequence of tokens
in making its decision. But our lexical analyzer Scanner keeps track of only the single
incoming symbol. For this reason, we define a second lexical analyzer LookaheadScanner ,
which encapsulates our Scanner but provides extra machinery that allows one to look ahead
in the token stream. It includes a means of remembering tokens (and their images) that have
been scanned, a method recordPosition() for marking a position in the token stream, and
returnToPosition() for returning the lexical analyzer to that recorded position (that is, for
backtracking). Of course, calls to the two methods may be nested, so that one predicate (for
example, seeCast() ) may make use of another (for example, seeBasicType() ). Therefore,
all of this information must be recorded on a pushdown stack.
Error Recovery
What happens when the parser detects an error? This will happen when mustBe() comes
across a token that it is not expecting. The parser could simply report the error and quit.
But we would rather have the parser report the error, and then continue parsing so that
it might detect any additional syntax errors. This facility for continuing after an error is
detected is called error recovery.
Error recovery can be dicult. The parser must not only detect syntax errors but it must
suciently recover its state so as to continue parsing without introducing additional spurious
error messages. Many parser generators 4 provide elaborate machinery for programming
effective error recovery.
For the j-- parser, we provide limited error recovery in the mustBe() method, which was
proposed by [Turner, 1977]. First, consider the definitions for see() and have() .
privatebooleansee(TokenKindsought){
return(sought==scanner.token().kind());
}
privatebooleanhave(TokenKindsought){
if(see(sought)){
scanner.next();
returntrue;
}else{
returnfalse;
}
}
4 Aparsergeneratoris a program that will take a context-free grammar (of a specified class) as input
and produce a parser as output. We discuss the generation of parsers in subsequent sections, and we discuss
a particular parser generator, JavaCC, in Section 3.5.
 
Search WWH ::




Custom Search