Java Reference
In-Depth Information
if(token.kind==skipTo[i]){
loop=false;
break;
}
}
}while(loop);
}
First, the function, from the token that was found and the token that was sought,
constructs and displays an appropriate error message. Second, it recovers by skipping to
the nearest token in the skipto list of tokens.
In the current implementation of the parser for j--, all non-terminals specify SEMI and
EOF as skipTo tokens. This error recovery scheme could be made more sophisticated by
specifying the follow of the non-terminal as skipTo tokens.
Note that when ParseException is raised, control is transferred to the calling non-
terminal. Thus when an error occurs within higher non-terminals, the lower non-terminals
go unparsed.
Generating a Parser Versus Hand-Writing a Parser
If you compare the JavaCC specification for the parser for j-- with the hand-written parser,
you will notice that they are very much alike. This would make you wonder whether we are
gaining anything by using JavaCC. The answer is, yes we are. Here are some of the benefits:
Lexical structure is much more easily specified using regular expressions.
EBNF constructs are allowed.
Lookahead is easier; it is given as a function and takes care of backtracking.
Choice conflicts are reported when lookahead is insucient
Sophisticated error recovery mechanisms are available.
Other parser generators, including ANTLR 9 for Java, also offer the above advantages
over hand-written parsers.
3.6 Further Readings
For a thorough and classic overview of context-free parsing, see [Aho et al., 2007].
The context-free syntax for Java may be found in [Gosling et al., 2005]; see chapters 2
and 18. This topic is also published online at http://docs.oracle.com/javase/specs/ .
LL(1) parsing was introduced in [Lewis and Stearns, 1968] and [Knuth, 1971b]. Recursive
descent was introduced in [Lewis et al., 1976]. The simple error-recovery scheme used in our
parser comes from [Turner, 1977].
See Chapter 5 in [Copeland, 2007] for more on how to generate parsers using JavaCC.
See Chapter 7 for more information on error recovery. See Chapter 8 for a case study|
parser for JavaCC grammar. JavaCC itself is open-source software, which may be obtained
from https://javacc.dev.java.net/ . Also, see [van der Spek et al., 2005] for a discussion
of error recovery in JavaCC.
9 A parser generator; http://www.antlr.org/ .
 
Search WWH ::




Custom Search